views:

98

answers:

1

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?

+2  A: 

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.

Kevin Pang
Yes, you need to manually clean it up (don't rely on GC). Check out http://trason.net/journal/2009/10/7/bootstrapping-nhibernate-with-structuremap.html
Joshua Flanagan