Saturday, January 1, 2011

PRISM4 : With UNITY or MEF ? –Dependency Injection (DI) & Inversion of Control (IOC)

Managing Dependencies Between Components, we outline some of the differences between the containers and leave the choice to the developer based on the needs of the project.  With Prism being container agnostic, developers can write a few simple classes for any container and plug it into Prism.  In the box, we have support of Unity and MEF, but the extension point is there and documented.

Here are some of the big differences between MEF and Unity:

  • MEF is not a DI container (it’s a dynamic add-in framework that uses DI techniques), but can be made to act like a DI container for simple scenarios. It has the advantage of being in the .NET 4.0 framework.
  • Unity is a full DI container. It supports .NET 3.5 and 4.0. It also supports more complex DI things like open generics, policy injection (AOP), etc.

The development styles differ quite a bit due to the different approaches in design an scenarios for usage.  With MEF, it is recommended to do declarative programming (attributes for registration/exporting and importing).  With Unity, the more common approach is a lot more imperative (registration via code rather than attributes). Constructor injection is preferred in Unity (even though it can do property injection) vs property injection is preferred in MEF (even though it can do constructor injection).

 

- Microsoft Patterns & Practice.

1 comment: