Thursday, January 16, 2014

Unity VS. MEF , Choosing the right DI container for your client application

When we need to choose for a DI framework for our client system,
We will probably have to choose between the two main frameworks available,
Unity and MEF.
For start I have to say that both of them are good frameworks, and every one of them has its advantages.
First, let's make it clear, both of them allow you to define containers that you can register with classes, and later on inject them. Both of them will take care of initializes the classes before you will use them.
Let's talk about the special features every one of them has:
MEF:
·         If your system is going to have a lot of plugins/extensions in its lifetime, MEF provides a great platform to work with. The mechanism of MEF will search for all the extensions that expose the predefined interface and will import them to your desired place. It is very convenient way to work with.
The downside for this feature is that you can't work with POCO objects,why is that?
Because you have to put attributes on the classes that you want to export and import. So if you are dealing with closed assembly that is not predefined as MEF expected , you have a problem.
·         The Attributes mechanism of MEF let you query  your desired classes with a variety of combination as you need, just declare your attributes (many as you wish) and import them with your desired filtering.

UNITY:

·         Unity works in a more obvious way, you call the container and ask the desired class. If you registered this class to the container , you will get it by your demand. MEF mechanism works with attributes so it is less intuitive way, and again need a control on every class you need to use.
·         Unity support interception, that mean that you can define a method that will be called before or after the injection of the class (for logging etc.). MEF don’t have this ability.

Like I mentioned above , these are a great frameworks to use for IoC and DI.
Just pay attention to the advantages of each of them so you could choose the framework that is right for you.










No comments:

Post a Comment