MEF with WinForms

Jul 12, 2012 at 9:12 PM

I am trying to use MEF in my WinForms application.  I only wanted to inject it in a control that gets inserted into another control which is inserted in a TabControl on my mainForm.  The Property Components (shown below) in my UserControl is never intialized.  When I copy the property into my mainForm it is initialized.  The control that uses this is initialized using a new constructor (i.e. MyControl control = new MyControl()).  I am assuming it did not get initialized becuase the MyControl object was created with the new command.  Is this true?  If so, how do I add MEF in parts of my existing application without replacing all object creation that uses new to do imports?

    public partial class MyControl : UserControl, IPartImportsSatisfiedNotification
        public IEnumerable<IFoo> Components { get; set; }

        public MyControl()

        public void OnImportsSatisfied()
            int i = 10;

Jul 13, 2012 at 2:31 AM

I created two links on skydrive with examples

"MEF WinForms Example 1" creates MyControl using new and hence the Foos collection is not created in MyControl

"MEF WinForms Example 2" creates MyControl using an Import and in this case the Foos collection is created in MyControl!136&authkey=!AM0AQ8DE2XmejmU!137&authkey=!AL4COGljjBVSI50

Why does the first case not work?  How can I get this to work without using the Import.  The reason I do not want to use the Import for MyControl is becuase I am working with a large WinForm project (pre MEF).  I only want to make use of MEF for a new feature with little change to the rest of the project.  Can I do this?


Jul 13, 2012 at 8:36 AM

Any type of Import and Export works just resolve type from CompositionContainer. It should be done by calling any variants of GetExportedValue from compositionContainer. see the following links:

Jul 13, 2012 at 2:39 PM
Edited Jul 13, 2012 at 2:40 PM

Does this mean that I need to create multiple CompositionContainer's?  In the sample code I created a CompositionContainer in my MainForm.  Do I now need to also create a CompositionContainer in MyControl or do I have to pass the container from MainForm into MyControl?

Jul 13, 2012 at 4:21 PM

No, My mean is when you create any object using new keyword instead of using container, nothing should be happen in container side ( that means no initialization ). To be honest the container is a central factory for creating objects and OnImportsSatisfied means notification event when all imports satisfied. In this case you must create your object through the factory ( you already have an instance of CompositionContainer ).

you can use your instance directly or using that through a central locator ( a static class as wrapper over your instance like CommonServiceLocator project in codeplex ) for creating your class instance instead of using new for getting your proper behaviour.

Jul 13, 2012 at 5:12 PM

If you are using some type of static to store your container so that the form has access to it during the constructor, you can call Container.SatisfyImportsOnce(this) from your control constructor and still use 'new' to create the form.  It's not ideal and basically represents technical debt, but since you aren't reworking the application it will work. 

Jul 14, 2012 at 4:38 AM

Thank you