Is MEF right for me?

Apr 13, 2012 at 12:31 PM

I'm writing a wpf mvvm application with that will use a plugin architecture.

I envision my application inspecting an "Extensions" directory for assemblies and having a container from which to instantiate desired objects from.  I figure any code that needs a specific object to satisfy an interface will get it from the single global container.

Looking at MEF, I think I get the importance of the export attribute.  I can use that to define which classes in my "Extensions" assemblies will be discovered and be available for instantiation and maybe take advantage of the export metadata.  For the import attribute, I'm not seeing as much value.  Wherever and whenever my code needs to instantiate a specific object that comes from the container, why not just ask for it?  If could have no import attributes on any of my properties and instead instantiate my properties in a constructor or as needed.

Would this be defeating the purpose of MEF?  Am I just thinking too simply?  Is MEF a good choice for me?

Thanks
Buzz 

Apr 13, 2012 at 10:54 PM
Edited Apr 13, 2012 at 10:56 PM

What you describe is using MEF as a Service Locator. That is probably a common scenario and should work fine.

Doing dependency injection with imports instead (preferably via an [ImportingConstructor]) has some advantages though. This way your component code doesn't have to depend on the MEF container and the global where it is available. That makes it easier to switch to another container, and to reuse your components in other applications without having to modify them. After all, other applications may not have the Service Locator global in the same place. They may use a different IoC container than MEF. Or they may not use a container at all!

Dependency injection also makes it easier to unit test your code. In the test, you can simply pass test substitutes for the dependencies to the constructor of the component under test. Doing this with a service locator is still possible I guess, by preparing the global container for each test, but it is harder to maintain the test code because the compiler can't give you errors if you fail to provide a dependency.

To summarize: imports allow you to minimize your assumptions about the environment. The less assumptions you make, the more reusable your components are.

Apr 14, 2012 at 5:55 PM
The short answer is Yes, MEF is a good choice.
You are describing your design in low details in the context of MEF because you did not describe which parts of your design will be extensible.
The very simple design of plugins that get discovered when the application start is not where MEF shines.
Take VS 2010 for example. Microsoft use MEF to load parts (plugins) which extend the editor. You can either extend the content of the editor or the chrome or something else but it is specific parts. http://msdn.microsoft.com/en-us/library/dd885013.aspx

When you will go into the details of your app you should identify the extensibility points and that's where you should use MEF.

Ido



ב-13 באפר 2012, בשעה 15:32, "buzzweetman" <notifications@codeplex.com> כתב/ה:

From: buzzweetman

I'm writing a wpf mvvm application with that will use a plugin architecture.

I envision my application inspecting an "Extensions" directory for assemblies and having a container from which to instantiate desired objects from. I figure any code that needs a specific object to satisfy an interface will get it from the single global container.

Looking at MEF, I think I get the importance of the export attribute. I can use that to define which classes in my "Extensions" assemblies will be discovered and be available for instantiation and maybe take advantage of the export metadata. For the import attribute, I'm not seeing as much value. Wherever and whenever my code needs to instantiate a specific object that comes from the container, why not just ask for it? If could have no import attributes on any of my properties and instead instantiate my properties in a constructor or as needed.

Would this be defeating the purpose of MEF? Am I just thinking too simply? Is MEF a good choice for me?

Thanks
Buzz