tags:

views:

14

answers:

1

I'm adapting some code originally written for Windsor to use StructureMap. In the Windsor example we release the handler. Is it necessary to do this with StructureMap instances that are cached "per request"? The code is:

        foreach (var handler in ObjectFactory.GetAllInstances<IHandle<TEvent>>()) {
            handler.Handle(@event);
            // do I need to dispose here?
        }
        // or should I do this: 
        ObjectFactory.EjectAllInstancesOf<IHandle<TEvent>>();

Thanks Ben

+1  A: 

StructureMap does not hold on to references to "pre request" instances at all, so you do not have to take any steps to tell StructureMap to release them.

However, if retrieved services expect to be explicitly disposed (because they implements IDisposable), it is still your responsibility to dispose them. StructureMap just gives you the instance, and its up to you to use it appropriately.

With one exception - if you retrieve an IDisposable instance from a nested container, Dispose() will be called on the instance when the nested container is disposed.

Joshua Flanagan