I'm currently using StructureMap to inject instances of NHibernate ISessions using the following code:
ObjectFactory.Initialize(x =>
{
x.ForRequestedType<ISession>()
.CacheBy(InstanceScope.PerRequest)
.TheDefault.Is.ConstructedBy(y => NHibernateSessionManager.Instance.GetSession());
});
I'm assuming that the CacheBy(InstanceScope.PerRequest) will properly dispose of the ISession it creates, but I'd like to make sure. What's the easiest way to test this?
Ok, so according to the StructureMap documentation:
Also note that StructureMap provides no functionality for cleaning up resources of the objects held by the container (Container.EjectAllInstances() will clear out singleton objects). To date, I have not found a need for this behavior or functionality. I generally assume that a combination of basic garbage collection and proper class design is sufficient.
I know it's best practice to call ISession.Dispose() when using NHibernate, so either I need to manually clean it up myself or simply rely on garbage collection to do it for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With