DataSource.initialzie() vs createConnection() in typeorm. Which one to use? - orm

I just started using TypeORM as my main ORM for my server. I have seen some tutorials where they use createConnection() instead of creating a DataSource object and initializing it.
Are they different? Do they have different use cases?
Thanks.

createConnection() is the old way to do it. Since typeorm 0.3.x you should use the DataSource object with DataSource.initialize().

Related

Disable implicit binding/injection of non explicitly bound classes in Ninject 2+

If you request an unbound object from NInject, then the default behaviour is (if a suitable constructor is available) appears to be to create an instance of the appropriate object.
I'd like to disable this behaviour (I had a difficult to debug issue because something was auto-bound instead of picking up my custom binding in a module). This question hints that it is possible, but I'm unable to find the answer from the NInject wiki.
Remove the SelfBindingResolver from the kernel components after creation:
kernel.Components.RemoveAll<IMissingBindingResolver>();
kernel.Components.Add<IMissingBindingResolver, DefaultValueBindingResolver>();
The following is a better, more direct way of removing the SelfBindingResolver, without assuming that the DefaultValueBindingResolver is the only other IMissingBindingResolver component:
kernel.Components.Remove<IMissingBindingResolver, SelfBindingResolver>();
It's possible the Remove<T, TImplementation>() method was only added in a recent version of Ninject, but this works for me using Ninject 3.2.2.0.

Is there an existing SqlConnection that I can use in LinqPad?

I am trying to test out some Dapper stuff using LinqPad.
Dapper needs a SqlConnection (IDbConnection really) to work. I could construct my own in the code window, but I thought if LinqPad already had made one, I would just use that.
Does LinqPad have a SqlConnection object already hanging around that I could use?
Did you try this?
this.Connection
It's a DataContext, after all.

Nhibernate: Get real entity class instead of proxied class [duplicate]

This question already has an answer here:
NHibernate Get objects without proxy
(1 answer)
Closed 2 years ago.
Is there a way to make nhibernate return my class instead of its proxy class? I dont mind if it's not lazy or cant be updated.
You can unproxy class with this code
session.PersistenceContext.Unproxy(proxiedInstance)
You should define this in your mapping, by defining lazy="false"
<class name="MyEntity" table="MyTable" lazy="false">
</class>
You can use following code to get real object
InstanceType instance;
if (proxiedInstance is INHibernateProxy)
{
var lazyInitialiser = ((INHibernateProxy)proxiedInstance).HibernateLazyInitializer;
instance = (InstanceType)lazyInitialiser.GetImplementation();
}
I'm using AutoMapper to achieve something similar in Entity Framework.
var nonProxiedInstance = Mapper.DynamicMap<YourType>(proxiedInstance);
That would work if you do not have navigation properties. Otherwise, you'll need to configure a mapping to ignore those properties.
Note: This is (obviously) an inefficient solution.
You can use the technique described in http://sessionfactory.blogspot.com/2010/08/hacking-lazy-loaded-inheritance.html (you'll need to do it recursively)
session.PersistenceContext.Unproxy(proxiedInstance) will not unproxy the associations. Also the no-proxy lazy loading does the same thing.
Disabling lazy loading is not a good idea and AutoMapper would navigate all the properties and trigger the loading mechanism.
IUnitOfWork.Unproxy from the NHUnit package could be used to unproxy the object and it's relations. This method will not initialize any proxy objects by mistake.

How do I use dynamic objects with asp.net?

I have a dynamic object built inside IronPython and I would like to build controls on my asp.net page dynamically based on what types of objects are nested inside my dynamic object:
dynamic variousComplexObjects = IronPythonApp.GetControls();
repeater.DataSource = variousComplexObjects;
repeater.DataBind();
Can someone write me a quick example of what to do next? I'm sure there is a tutorial out there doing something similar, but I'm having a bit of trouble googling it. Feel free to recommend me the correct keywords or point me in the direction of properly consuming DLR data in an asp.net app.
Thanks!!
Assuming you use .net 4, you can just use dynamic in your databound event.
repeater.ItemDataBound += OnItemDataBound;
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
dynamic dynObj = (dynamic)e.DataItem;
string text = dynObj.Text; // Etc.
}
I'd probably have a type property or similar to check on - otherwise you're stuck with trying to use GetType() which I'm not certain whether works with IronPython.
Here's a thread entitled "Databind object with 'dynamic' properties". It involves creating customer get and set methods on the type.
It doesn't exactly fit with your scenario of C#'s dynamic and sourcing data from the DLR, but it might help.
Here's another thread entitled "Can I databind to a collection of Dynamic Class". The authored created a dynamic class using Reflection.Emit.
Please comment if any of these solutions fit your case. I'm also interested in the solution, but don't have the tolls to test, at the moment.
Thanks.. I guess nobody is coding in traditional asp.net web forms and dynamic objects. There's probably a way to do it.. but I just switched to MVC instead.

Subsonic VB.NET problem

When I'm using VB.NET to use subsonic, It seems to have problem marking records as Old and Clean. Whenever I query using ExecuteSingle or ExecuteTypedList, i need to manually MarkClean and MarkOld, else whenever I save it will save as a new record.
Am I the only one facing this problem ? I'm using SubSonic 2.2 btw.
I checked the source code of SubSonic.. and I found that the VB class generator doesn't implements the IActiveRecord. I think most likely is because VB.Net doesn't seem to support 're-implementation' of inheritance or whatever you call that...
So when I debug, I found that Utility.IsSubSonicType returns false (because the ActiveRecord class returns as IReadOnlyRecord, but IsSubSonicType checks for IActiveRecord and IRecordBase) and thus doesn't call the SetLoadState and MarkClean.
So I'm not sure if this is a bug or it is intentional. Any way to solve this?
When you use ExecuteSingle or ExecuteTypedList, you could be doing with a class that didnt have those properties, I think the intention is that you are populating a POCO and not (necessarily) an Entity or other ORM object.
ExecuteAsCollection and all of the .Load methods behave as you expect because they call SetLoadState() and/or MarkClean().
Personally, I dont face this problem because I use Subsonic purely as a (smart) DAL (CRUD Only) and my own entity layer takes care of things like dirty/new.
Yes, I had the same problem. MarkClean and MarkOld before setting properties and saving fixed the issue. see this