MEF for Beginner – Part 2 (VS 2008)
How To build your first MEF application
in this post we will cover the basic steps that is needed for building your first MEF application.
you can find more about the MEF concept on part 1 of this series.
the next following instructions will lead you through very simple steps of building simple Logger application.
this post is written on Visual Studio 2008, use this link for the Visual studio 2010 version.
the Silverlight version will be discuss on the part 3.
Step 1: create new console application
Step 2: add reference
previous to the .NET 4.0 MEF does not include in the CLR, therefore adding reference to MEF involve
one extra step of downloading the MEF source code from http://mef.codeplex.com/
open the MEF.sln and compile the and build the ComponentModel project
add reference to System.ComponentModel.Composition.dll
Step 3: Create the ILogger interface
the ILogger interface will latter use as a contract.
Step 4: Write the logger implementation
the following code show simple logger implementation for logging to the EventLog, Console and the Debug.
Step 5: Create proxy to the logger service
Let’s see what do we have so far
so far we having all the parts needed for our simple logging solution.
but we lack the glue to bind those parts (how do the logger proxy will discover and initialize its loggers?).
MEF as a glue
In the next steps we will see how MEF can be used
to glue the parts together (without coupling the
Step 6: Decorating the parts
out of the box MEF having very simple and friendly attribute model,
which can be used to decorate our Exported parts (the part that we want to be discoverable),
and our Imports (the units (properties for example) that needed those parts).
Note: MEF does not dictate the decoration (attribute) model.
Step 6.1: Decorating the Exported parts
the following code is using the Export attribute for decorating the part
that should be exposed while asking for the ILogger (contract).
Step 6.2: Decorating the needs
the following code is using the ImportMany attribute decoration,
to inform the MEF infrastructure that the Loggers property should assign with the parts that match
the ILogger contract.
Step 7: Composition
Note: decoration by itself does nothing but attaching declaration to code units.
the following code illustrate how to start the MEF composition process.
the following steps occurs at the static constructor:
Create instance of the _logger (singleton pattern)
Create catalog (that telling MEF where to look for parts discovery)
Create container (host), and handing the catalog.
Call the ComposeParts method of the container (this is where the Logger property get assigned with the discovered loggers).
the ComposeParts gets the _logger (that way the composition will act upon the _logger instance instead of creating another.
the code for this post available here.
further on this series i will discuss the Silverlight version for MEF and some we will continue to explore many
other aspect of the MEF framework like Metadata, Lifetime management, Catalogs, Initialization techniques, Pocket MEF and match more.
Point of interest
if you living in Israel you can hear me speaking on MEF at the SDP
(actually this lecture isn’t yet updated in the syllabus but i believe that it will in a few days)