Kevin Pang
Kevin Pang

Reputation: 41442

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?

Upvotes: 5

Views: 1027

Answers (1)

Kevin Pang
Kevin Pang

Reputation: 41442

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.

Upvotes: 4

Related Questions