Friday, October 23, 2009

MEF Framework for Dependency Injection

Recently I have time to read MEF (Managed Extensibility Framework) on Microsoft's Open Souce web site CodePlex. I knew this one for quite a while. I decided to spend time to read the whole document about the framework because this one appears many times when I search for new stuff and IOC/DI. Many places also mention that MEF will be part of .Net Framework 4.0 and Visual Studio 2010.

The framework seems very simple. Basically it uses attribute classes Import and Export to define dependency relationships. Export is used to export a class and in most cases the class should implement an interface. Then Import is used by MEF to find out matched class to get an instance. MEF also supports a collection of same Imports with same types. In this way, Parts are separated by application and libraries which are linked by MEF only by types specified in Import and Export. One advantage of MEF is that it is based on reflection to get DI relationship by Catalog. As a result, there is no need to define DIs by xml or config files.

I have used StructureMap and I really like its strength in IOC/DI, as in my previous blogs. SM also separates dependent parts between clients and users very well. I think I'll try MEF out in my projects to find out really how MEF works.

0 comments: