tags:

views:

81

answers:

1

Why does the ExportLifetimeContext<T> exist? What is it for? And why is it necessary to call Dispose on this objec at all? Do I really need to bother calling it? It doesn't feel very managed if I have to spend time thinking about resource managment, it doesn't feel very managed to me.

Is this dispose in anyway tied to the Value property? Is there a specific problem with just going CreateExport().Value?

+3  A: 

When you ask an ExportFactory to create a new object, MEF might actually also create dependencies, and the dependencies of those dependencies, etcetera. Many objects might be created because you asked for a single object.

Some of these extra objects might be IDisposable, and the container is responsible for disposing those when they are no longer necessary.

To signal to the container that you are done using your object, you call ExportLifetimeContext<T>.Dispose(). The MEF container will then take care of disposing the requested object and its dependencies if necessary. If you don't do this, MEF will keep references to these objects forever, waiting for a signal that it can dispose them.

In Autofac 2, a very similar mechanism exists. Instead of ExportFactory, they use Func<Owned<T>> and you need to call Owned<T>.Dispose() to clean up.


edit: The MEF documentation has a Parts lifetime section where it is described in which cases exactly the container keeps references to exports. It does not yet mention ExportLifetimeContext.Dispose but I imagine this is similar to CompositionContainer.ReleaseExport.
edit: note that ExportFactory is intended for cases where you have a clearly defined lifetime. If this is not the case (or you know that clean-up is never necessary), then you should create your own factory implementation. Of course, it is then your responsibility to make sure that nothing IDisposable is created, since it would be impossible to clean it up properly.

Here is an example of a custom factory import/export using Func<T>.

[Export(typeof(ISomeInterface))]
public class SomeClass
{
    private readonly Func<Foo> fooFactory;

    [ImportingConstructor]
    public SomeClass(Func<Foo> fooFactory)
    {
        this.fooFactory = fooFactory;
    }

    public void DoStuff()
    {
       Foo newFoo = fooFactory();
       ...
    }
}

public class FooFactory
{
    [Export(typeof(Func<Foo>))]
    public void CreateFoo()
    {
        ...
    }
}
Wim Coenen
Should I be using the "using" pattern now that I have to call Dispose or is this only necessary if any of it's dependencies actually implement dispose. How is this justified? It sounds like it's a one-way street to horrid memory leaks.
John Leidegren
@John MEF is designed for open ended systems, so the thinking behind this was that when writing a part you can't know whether any of the parts in the transitive closure of its imports need to be disposed or not. So when using ExportFactory in an open ended system you should always dispose the lifetime context. If you are composing only parts you created and you know none of them are IDisposable then it would be OK to not call Dispose.
Daniel Plaisted