this post is the first in a series of posts of How To start using the new MEF (Manage Extensibility Framework).
the series will include simple steps for using MEF on the .NET, Silverlight and Compact Framework platform.
part 1 will focus on the MEF concept, while the following posts will focus on the implementation details.
What is MEF?
the MEF framework is the Extensibility (plug-ins) standard for the .NET
The MEF team declare 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.”
The core concept of MEF
that can be discovered (in order of satisfying the needs).
MEF assume that application is 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 is using the following terms to address the above paradigm:
Import: Define needs in term of Contract (the contract present the capabilities that is needed).
Export: Exposing 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).
as analogy 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.
that way you can architecture the your solution to consume unknown implementation of parts.
What does MEF bring to the table?
actually MEF is all about breaking the tightly coupled dependencies within your solution
without loosing the type check safeness.
Addressing the Unknown
MEF addressing potentially unknown parts using its Compose mechanism which will be responsible
to discover and instantiate the parts that satisfy the needs of the Imports.
Show me the Code
the next parts of this series will focus on how to use the MEF framework in more details
and give the practice needed for building your first MEF application.
but i wont live you without having the taste of how does it implement.
for the first taste i will borrow sample from Glenn Block presentation:
the scenario Glenn present is a Rule engine.
the first part is a single Rule that has need for logger.
the code snippet expose the Rule by decorating the class with the Export attribute.
the class declare its needs for logger by decorating the Logger property with the Import attribute.
Note: MEF does not dictate the use of attribute but this discussion is out of the scope of this post.
the Rule Engine class has needs to have the rules, so it decorate the Rules properties as follow:
the final step is to compose all parts together:
MEF is very flexible and there is many other ways of implementing that task.
in the following post we will learn how to use MEF in both VS 2008 and 2010, and on different platforms.
once again i want to thanks Glenn Block for giving me his MEF presentation which help me building this post.
more resources can be found at Glenn Block post