14

I'm starting a new personal Prism 4 project. The Reference Implementation currently uses Unity.

I'd like to know if I should use MEF instead, or just keep to Unity.

I know a few discussions have mentioned that these two are different, and they do overlap, but will I be missing out if I simply choose Unity all the way?

Jonas Arcangel
  • 2,085
  • 11
  • 55
  • 85
  • [This thread](http://stackoverflow.com/questions/3735546/what-should-i-use-in-prism-mef-or-unity/3855389#3855389) provides some useful insight on this subject. – Damian Schenkelman Oct 26 '10 at 12:49

3 Answers3

23

Also check out the documentation:

Key Decision: Choosing a Dependency Injection Container

The Prism Library provides two options for dependency injection containers: Unity or MEF. Prism is extensible, thereby allowing other containers to be used instead with a little bit of work. Both Unity and MEF provide the same basic functionality for dependency injection, even though they work very differently.

Some of the capabilities provided by both containers include the following:

  • They both register types with the container.
  • They both register instances with the container.
  • They both imperatively create instances of registered types.
  • They both inject instances of registered types into constructors.
  • They both inject instances of registered types into properties.
  • They both have declarative attributes for marking types and dependencies that need to be managed.
  • They both resolve dependencies in an object graph.

Unity provides several capabilities that MEF does not:

  • It resolves concrete types without registration.
  • It resolves open generics.
  • It uses interception to capture calls to objects and add additional functionality to the target object.

MEF provides several capabilities that Unity does not:

  • It discovers assemblies in a directory.
  • It uses XAP file download and assembly discovery.
  • It recomposes properties and collections as new types are discovered.
  • It automatically exports derived types.
  • It is deployed with the .NET Framework.
Pat
  • 16,515
  • 15
  • 95
  • 114
7

I am currently doing the same investigation. I was last week attending the p&p symposium at Redmond. I had the chance to chat with some of the p&p people on that.

MEF

+Part of .net, no need for extra libraries

+Very powerful in extensibility, modularity scenarios

-More generic approach, less flexible for DI scenarios

-You need to decorate with attributes, your code is glued to MEF

Unity

+Very flexible for DI scenarios

+If you stick with ctor injection and avoid using named instances then you don't need to use any attributes. Most of your system doesn't rely on Unity

-No out of the box support for extensibility, modularity scenarios

-Need to deploy the 3rdparty libraries

What I think is a good idea is to use MEF for extensibility (manage the modules of your app, localize registrations) and use Unity for DI.

  • That's what I figured. For Prism, I'll use MEF for modularity, and Unity for code plumbing (dependencies, AOP, etc) – Jonas Arcangel Oct 29 '10 at 04:53
  • 1
    You may want to take a look at http://github.com/mefcontrib. It has a module that allows you to use mef and unity together. There is a writeup on how to use it at http://mefcontrib.codeplex.com/wikipage?title=Unity%20Integration&referringTitle=Documentation%20%26%20Features. We are using this in our next project. – photo_tom Nov 01 '10 at 23:41
0

Well this has to be clear that MEF implements Inversion of control but it is not a part of it, so this means that they are not same, there is a difference, that we use unity when we have static dependency and MEF provides us with dynamic dependency.

MEF also provides us with extensibility, by which we can induce a port type mechanism and can also specigy the type of component which can interact via that port. more can be understood from: MSDN Document

j0k
  • 22,600
  • 28
  • 79
  • 90
Iti Tyagi
  • 269
  • 1
  • 2
  • 6