Application Modernization: What Is It and How to Get Started
Introduction to MEF
In this article I will be writing about Managed Extensibility Framework (MEF) along with a demo application using Windows Presentation Foundation (WPF).
Have you ever thought of building an applications by considering the extensibility of the application? I suppose most answers would be 'no'. Is this because it was not possible? No, it was so difficult. The way to load the modules dynamically (without adding a strong assembly reference to project) and plug it onto your application was through Reflection, else the other normal way is to perform code changes in order to accommodate the new modules. This is what led to the introduction of the MEF.
Component and classes for MEF in the .NET framework 4.0
The good news for .NET developers is that MEF is integrated and comes out of the box with .NET framework 4.0. The library for MEF is System.ComponentModel.Composition. All the MEF members are bundled up into the System.ComponentModel.Composition library which has to be referred explicitly to the project in order to use MEF. Below are the members of the MEF
Attribute to decorate the element to which the modules object to be injected. It is also called the Composable Part.
Attribute to decorate the member of the module (called as a part) which has to be exported to the main application.
Catalog provides the capability for MEF to locate the exported assemblies or modules.
A few of the available catalogs in MEF are
It picks the provided assembly to pick the exported parts.
It picks the exported modules from the assemblies in the specified directory.
AggregateCatalog can be used when multiple catalogs have to be considered for picking up the exported parts.
It represents the set of Composable Parts to add and remove from the composition container.
This is the actual match making class which composes and couples the export part and composes the import part.
Fig 1.0 shows the block diagram for MEF