views:

227

answers:

2
+1  Q: 

When to use MEF

I tried Unity and MEF and plain old new. And I am a bit confused as to when exactly you need to use any of these "patterns". Why would I choose to use MEF or Unity if using new is simpler and satisfies my needs.

In other words, what needs justify the use of MEF or Unity?

+3  A: 

MEF allows for isolation of subsystems within an application. This isolation is useful if you need to change subsystem implementations often, or even at runtime. MEF takes care of the details of loading the subsystems and wiring up all the providers to the appropriate consumers. It's a giant step forward toward the ideal of building an application by concatenating a set of self-contained modules that know nothing about each other.

A significant portion of the code in a traditional application is dedicated to simply gluing things together - glue code. MEF drastically reduces the need for glue code.

If you're just constructing an object so you can open a file or show a dialog, that's not what MEF is for. If you're wanting to write code that uses a generic xyz service but don't want to hard-code a dependency on vendor a or vendor b implementations, that might be a MEF candidate.

dthorpe
A: 

You can look at this very active discussion about Dependency Injection containers.

http://stackoverflow.com/questions/871405/why-do-i-need-an-ioc-container-as-opposed-to-straightforward-di-code

Vadim