Friday 27 July 2012

Microsoft Extensibility Framework(MEF)-Introduction

What is MEF?

Managed Extensibility Framework (MEF) is a component of .NET Framework 4.0 for creating lightweight, extensible applications. It allows application developers to discover and use extensions with no configuration required. It also lets extension developers easily encapsulate code and avoid fragile hard dependencies. MEF not only allows extensions to be reused within applications, but across applications as well. MEF was introduced as a part of .NET 4.0 and Silverlight 4.


Why was MEF Introduced?

How does a dotnet application work?
If there are many libraries that are referenced in a dot net project,with CLR and JIT in the application loader tries to load only those libraries that are needed for execution at the Form_Load.Whenever a method is called ,the IL for those methods are compiled JIT on demand and loaded into memory.

This is the concept of dynamic loading of libraries.

As the Microsoft says,"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. If you are building extensible applications, extensible frameworks and application extensions, then MEF is for you."

Now, although .NET uses the concept of loading the dlls on demand but still if you run a program , remove the referenced dll from bin and as soon as the execution reaches the point where the dll has to be loaded ,you add the dll the application fails to run and exit with an error. This is the problem that MEF tries to resolve.

Without MEF also, this problem of dynamically loading the dll during execution can be solved using the concept of reflection. But the amount of time and LOC used to solve this problem is complex and high.
So, the concept of MEF entered.

  • MEF provides a standard way for the host application to expose itself and consume external extensions. Extensions, by their nature, can be reused amongst different applications. However, an extension could still be implemented in a way that is application-specific. Extensions themselves can depend on one another and MEF will make sure they are wired together in the correct order (another thing you won't have to worry about).
  • MEF offers a set of discovery approaches for your application to locate and load available extensions.
  • MEF allows tagging extensions with additional metadata which facilitates rich querying and filtering.

Looking inside MEF

MEF's core consists of a catalog and a CompositionContainer. A catalog is responsible for discovering extensions and the container coordinates creation and satisfies dependencies.

The essence of MEF paradigm is built upon the idea of needs and part that can be discovered (in order of satisfying the needs).  MEF assume that applications are build from parts, each part may have needs to   consume other parts, or may be discover and consumed by other.

 In general parts consumers care about the contract and doesn't care about the parts implementation. 

MEF using Import solves the needs  term of Contract (the contract present the capabilities that is needed). Uses Export,to Expose discoverable parts (that latter can be instantiate and consumed by the Import), the Export is also Contract based. Compose: is handle by the MEF engine, it responsible of putting the pieces together (discover and instantiate the matching Exports, and hand it to the Imports).



Basic Terms Used in 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.
  • Catalog: An object that helps in discovering the available composable parts from an assembly or a directory.
  • Contract: The import and exported parts need to talk between themselves via some contract (e.g. an Interface or predefined data type like string)
  • Import: It defines the need that a part has. It is applicable only for single Export Attribute.
  • ImportMany: It is similar to Import Attribute but supports for multiple Export Attributes.
  • Export: The import attribute creates the needs. The Export attribute fulfills that. It exposes those parts that will participate in the composition.
  • Compose: In MEF jargon, Compose is that area where the Exported parts will be assembled with the imported ones.

Advantages

  • MEF breaks the tightly coupled dependencies across the application but respects the type checking of the loosely coupled parts.
  • Applications can be extended.
  • Components can be added at runtime.
  • Dynamic discovery of the components.
  • Great piece of reusability.

Conclusion



MEF works upon the principle of demand and supply ;needs of parts. 
Step 1:Parts and needs should be decorated with Import and Export attributes.
Step 2:The needs and parts must agree to the contract
Step 3:Using Catalog MEF discovers the parts to be plugged in
Step 4: Composition engine makes sure that parts satisfy the contract.



No comments:

Post a Comment