2009-05-16

MEF introduction

The MEF team declare the MEF as

“The Managed Extensibility Framework (MEF) is a new library in .NET that enables greater reuse of applications and components. Using MEF, .NET applications can make the shift from being statically compiled to dynamically composed.”

you can download the MEF bits in here

SO what is the MEF and why do you want it?

Lets start with what !(MAF)
MEF is not another DI (dependency injection), you should think of MEF as plug-ins infrastructure rather then being DI.

Actually you can consider MEF as very flexible plug-ins discovery mechanism.

How does it work?

The essence of MEF paradigm is built upon the idea of needs and part that can be founds (in order of satisfying the needs).

You can think of it as having Guilds that publish its policy (shoemaker, tailor, ext…), and Peoples that declaring their professions, the MEF infrastructure apply the role of matching the Peoples to their suitable Guilds.

image

The above diagram present institutions (University and software company) that has specific need (in terms of human segments),
and peoples that apply to one or more of the segments.
the MEF framework role is to satisfy the institution needs by discovering the people which apply the right segments.

The MEF terminology: institutions will be declare as Import and the peoples will be declare as Export Parts

Where does MEF looking for the for its Export Parts?

MEF is looking for export parts in Catalogs there is built-in catalogs (like Directory, Assembly, Types) and you definitely can build your own.

Code samples:

You can download the sample here (VS 2008 SP1)

the following are very light snippets of how it will look

// property that declare its needs for university related peoples
[Import (typeof (IUniversityRelated))]
public IEnumerable<PeopleBase> Peoples { get; private set; }
// class that decorate export part for university
[Export (typeof (IUniversityRelated))]
public class Student: PeopleBase, IUniversityRelated
// asking the MEF to compose the right export parts
// in order of satisfying the imports

private static CompositionContainer s_container =
new CompositionContainer (new AggregateCatalog ());
static void Main ( string[] args ) {

// here the MEF magic matching game occur
s_container.Compose (new CompositionBatch ());

Summary

MEF is very friendly plug-ins framework which help you to get greater extensibility with less efforts and more consistency.
It is very simple decoration module which having supper light dependencies.I hope you find this post useful, I'm having intensions to write future post which will cover the MEF in more details including some advance technique.

About

Bnaya Eshet C.T.O and co-founder of Wise Mobility

No comments: