In this post, I show you how to create a decoupled application using Managed Extensibility Framework (MEF)
Managed Extensibility Framework (MEF) is the next BIG thing happening in .Net Framework 4. This framework allows the developers to extend their apps in completely decoupled fashion.So what does that means well you can simply plug in (let us say new module) without recompiling the your main application. All that is required here that all the modules of the application follows the same contract.In this post , which is part one of two series. I will do a walkthrough of some real time scenario where you can apply MEF practically. Before you continue to read further , I strongly recommend that you get sound understanding of MEF. This post only talks about the MEF stuff that will be used in this project. You can look below in the reference section below to learn MEF.
Imagine you are working as a Solution Architect for some product based company , who has a product of HRMS. Now in this product there can be multiple modules like for instance
Your boss wants you to write this software in a such a manner that he can sell each license individually e.g. He can sell only Personal Module to one client to another client he may sell just two modules or to the another client he may sell the whole HR suite. The integration of other modules should be seamless that it does not disturbs the existing modules. I hope you have got the understanding now. Sounds Tuff , well not exactly if you use MEF in your development.
MEF is based on attributes style of programming.There are many attributes that are available for you to use. For this project we may not be using all of them but let me give you brief understanding of the attributes that we will be using for this project.
- Export Attribute: If you want to expose any of the component to others so that they can consume it. You must decorate that component or type with Export attribute. One of the constructor of this attribute is of Type object, which your interface class. This will be the key thing over here all our modules will be decorated with the Export attribute.
- Import Attribute : The import attribute is opposite of the export attribute.The import attribute will do the discovery of the parts or modules which are decorated with export attributes.The import attribute will only discover a single part whereas as ImportMany attribute will discover multiple parts.
Tip:It is always advisable to use ImportMany attribute rather than Import attribute so that it can discover more than one parts.
- ExportMetaData Attribute:This attribute allows the developers to export certain metadata that can be attached to any component.It has two parameters in the constructor the metadata name of string type and the value of that metadata of object type.
So you have seen the MEF attributes that will be used for this project now let us look at other object types which will be used.
- CompositionContainer: This class acts like a container for all the composable parts(The parts which are decorated with Export attribute).In our project this will holds all the modules along with their metadata in the form of AggregateCatalog described below.
- AggregateCatalog: This is the placeholder of the all the other catalogs like AssemblyCatalog and DictionaryCatalog
- AssemblyCatalog:It holds the catalog information of the underlying assembly.In our project we shall be utilizing it for getting the info about the ApplicationShell.
- DirectoryCatalog:This catalog consists of the objects in the designated directory.In our project , this will be the directory where all the assemblies of the modules will be placed.
All these objects are available in System.ComponentModel.Composition
MEF Reference Links
I hope you must have got some understanding of MEF now. Watch out this space for Part II, in which I shall be giving the complete understanding of the application along with the source code of the app.