koderlabs Blogs

All You Need to Know About ‘Managed Extensible Framework’

Managed Extensible Framework (MEF) has been around for a long time now, I still believe that a beginner should begin with a straightforward article. It is now an essential component of the. NET Framework 4.0 and can be used in any application or implementation.

What the heck is MEF?

Managed Extensible Framework is a framework from Microsoft Corporation to build Extensible applications. Since MEF is pluggable in nature, its basic purpose is to plug-in components to an already running application. It is an integral part of .NET 4.0 Framework and it finds its applicability in any kind of .NET applications.

Life before MEF

There has been always a gruesome problem faced by our ancestors while thinking about the run-time extensibility. Any application, with complete modules, that wants to support a plugin model or to extend its module needs to create its own infrastructure from scratch which may even sound horrendous for the devs.


MEF seeks to fix the extensibility issue of run-time. These plugins are often application-specific and cannot be reused across various applications. It comes with:
– A source to provide a standard way for the host application to expose its modules and consume external modules.
– MEF provides a number of discovery methods to find and load accessible extensions.
– It enables tagging extensions with extra metadata to promote wealthy querying and filtering.

MEF Nomenclature:

Before kicking off, How Managed Extensible Framework works, let’s have a look at basic terminologies which surely will help us to understand it with much ease.

  1. Part: A Part is an object (e.g. a class, a method or a property) that can be exported by a plugin and imported by an application.
  2. Catalog: An object that helps in discovering the available composable parts from an assembly or a directory.
  3. Contract: The import and the exported parts need to talk between themselves via some contract (e.g. an Interface or predefined data type like string)
  4. Import Attribute: It acts as a Hook or Socket. Defines the need that a part has. It is applicable only for single Export Attribute.
  5. ImportMany Attribute: It is similar to Import Attribute but supports for multiple Export Attributes.
  6. Export Attribute: Acts as the plug which can be attached to the Socket(i.e: Import) in order to complete the flow. The import attribute creates the need. The Export attribute fulfills that. It shows that those parts will participate in the composition.
  7. Compose: In MEF language, Compose is that area where the Exported part and Import communicate together, the Exported parts will be assembled with the imported ones.

Up until this point, we are quite familiar to the terminologies on which the Managed Extensible Framework comprise of. Now let’s dive into how it actually works.

MEF at work:

If you’re familiar with the principle of Demand and Supply, You’re almost there!

In Managed Extensible Framework, The suppliable part must and should be discovered. Both the suppliable part and the needed part must agree to the contract(i.e Interface). Whenever there is a requirement for some parts to be plugged into the application, MEF performs a dynamic discovery of those parts into Directory or Assembly from some location(i.e: Catalog). The composition engine(i.e: Composition Container) will assemble those parts together as long as they satisfy the contract.

Let’s be practical …

We will create a simple class which will have a hook to import an addition extension method to experience the pluggable nature.

The kairotic assembly to achieve the extensibility within your application: ‘System.ComponentModel.Composition.dll’

In order to create a real word kind of hook in MEF, we need to first create a contract using interfaces as shown.

We need to ensure that both the import and export attributes are of the same interface type as shown IContract.
Exported part, Add’, consent the contract rules.

Composition Container:

After creating the Export method and Contract, We need to communicate the Import(Socket) and Export(Plug) to complete the cycle. 
CompononetAssembler is the core part; The discovery of dynamic and static references are then added into the Catalog to make them discover.

While ceasing it …

Many of the architects and developers, I know, been using it in enterprise applications and it has enabled them to build more composite, extendable, testable, and scalable applications faster and, in my opinion, better.

Please share your responses and feedback below. I’d especially love to hear if you are currently working on a MEF project and would like to share how it has helped (or perhaps hindered) you in the process. Cheers!

Author Bio:

Ahsan Raza – Master at Tsundoku & a nethead. Software Engineer @KoderLabs LLC. Crazy about tech, traveling & football.

Connect with me: Facebook | LinkedIn

Add comment