Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify StructureMap is disposing of objects properly

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?

like image 234
Kevin Pang Avatar asked Oct 14 '22 07:10

Kevin Pang


1 Answers

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.

like image 149
Kevin Pang Avatar answered Oct 19 '22 03:10

Kevin Pang