MEF vs UnityContainer, Which is better?

Topics: Prism v4 - WPF 4
May 6, 2011 at 9:46 AM


I ve an application done in Prism 2.0, now i m working on updating the same to Prism 4.0...

Currrently UnityContainer is use. Would converting the implementations to MEF increase the preformance of the project?


May 6, 2011 at 2:42 PM


Both Unity and MEF have the same basic functionality for dependency injection, even they work differently.

As Prism documentation shows in Chapter 3: Managing Dependencies Between Components

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.

As you can see, each container has specific capabilities. Therefore, if you are consider migrating from Unity to MEF, keep in mind the preceding capabilities and determine which of them fits your scenario better, along with the additional overhead of switching the DI mechanism you’re using.

Take into account that in Prism v4, the version of unity that is used is 2.0.414.0. 

Additionally, remember that Prism is extensible, allowing other containers to be used instead Unity or MEF.

I hope you find this information useful.


Miguel Bronzovic