What is MEF? Why and where we should use MEF?
MEF is an acronym for Managed extensibility framework. This is a library from Microsoft to build extensible applications. It is attributed programming model as it works on attributes like import, importmany, export, importingconstructor, exportfactory, etc. MEF is very helpful for larger applications where injecting the dependencies in between the distant parts is usually quite complex. It only requires copying the dll in the bin folder of the existing application.
MEF is not an IoC (Inversion of control) container, even though it uses IoC concepts. MEF focuses on application extensibility with component discovery and composition. IoC containers are most useful with static dependencies whereas MEF can work with dynamic dependencies. MEF is great at the plug-in pattern for a whole assembly at ‘run-time’, while something like Unity make the code easier to change at design-time.
MEF Benefits:
- MEF is an integral part of .Net 4.0 framework that can be used with any existing .Net applications. It can be used as a third party plugin and it can bring the benefits of a loosely-coupled plugin-like architecture to regular applications.
- MEF helps modularity, so we can separate the functionalities in different modules which are very useful for Silverlight applications. It allows us to create multiple xap files and load it when needed. MEF used for components which are loaded dynamically by demand following the principle of “Load On Demandâ€. So, basically it does not load all the files at the same time. This makes the Silverlight applications run much faster.
- MEF helps to reuse the code. We can reuse the existing code using the attributes like import, importmany, export, importingconstructor, exportfactory, etc. Example: We can use the MEF with MVVM (Model View ViewModel) and dynamically bind one viewmodel with multiple views.
Case Study
Client Challenge
The client needed to expand the functionality and look & feel of their resource and project management application. The existing application could not deliver satisfying results and was not compliant with Service Legal Agreements (SLAs) as demanded by their enterprise customers. In addition, the existing architecture was not that much flexible to support additional functionality, data presentation or customization.
Extending the application via MEF:
Integrating the existing application with Microsoft MEF benefited with:
- Security: The applications separated across various modules, extended security by loading only the accessible components.
- Dashboard Customization: According to the user access level, different user-specific dashboards were customized.
- Faster Presentation: The Silverlight application was separated in various modules. This allowed the application to be loaded in parts for faster data representation, avoiding huge load time delays.
- Scalability: The use of MEF offers possibility to add-on functionalities using the various attributes, in a plug-n-play pattern.
Technology
- Microsoft .NET
- Microsoft Silverlight 4.0
- Managed Extensibility Framework
- Microsoft PRISM
Basic terminologies of MEF:
Part: A Part is an object (e.g. a class, a method or a property) that can be imported or exported to the application.
Import Attribute: It defines the need that a part has. It applies in a scenario when we require only a single attribute.
Export Attribute: Export attribute fulfills the needs which are created by import attributes. We can import only those parts which are decorated with export attribute.
ImportMany Attribute: This attributes allows us to consume multiple parts which are decorated with export attributes.
Contract: The import and exported parts need to talk between themselves via some contract. So they are the bridge between import and export. (e.g. an Interface or predefined data type like string)
Catalog: An object that helps in discovering the available compose-ready parts from an assembly or a directory.
Compose: In MEF jargon, Compose is that area where the Exported parts will be assembled with the imported ones.
Glossary:
[1] Ten Reasons to use the Managed Extensibility Framework by Jeremy Likeness – Article Source
[2] Migrate from Basic to MVVM and MEF Composable Patterns for a Silverlight Application – Part 1 by Shenwei Liu – Article Source