MEF for Beginner – Part 2 (VS 2010)
MEF for Beginner – Part 2 (VS 2010)
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 2010, use this link for the Visual studio 2008 version.
the Silverlight version will be discuss on the part 3.
Step 1: create new console application
Step 2: add reference
add reference to System.ComponentModel.Composition (MEF is part of the CLR starting from CLR 4.0)
Step 3: Create the ILogger interface
the ILogger interface will latter use as a contract.
Code Snippet
- public interface ILogger
- {
- void Write(string message);
- }
Step 4: Write the logger implementation
the following code show simple logger implementation for logging to the EventLog, Console and the Debug.
Code Snippet
- public class ConsoleLogger: ILogger
- {
- void ILogger.Write(string message)
- {
- Console.WriteLine(message);
- }
- }
- public class DebugLogger : ILogger
- {
- void ILogger.Write(string message)
- {
- Debug.WriteLine(message);
- }
- }
- public class EventLogLogger : ILogger
- {
- void ILogger.Write(string message)
- {
- EventLog.WriteEntry("MEFSample", message);
- }
- }
Step 5: Create proxy to the logger service
Code Snippet
- public class LoggerProxy
- {
- private static LoggerProxy _logger;
-
- static LoggerProxy()
- {
- _logger = new LoggerProxy();
- }
-
- private ILogger[] Loggers { get; set; }
-
- public static void Write(string message)
- {
- foreach (var logger in _logger.Loggers)
- {
- logger.Write(message);
- }
- }
- }
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
different parts).
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).
Code Snippet
- [Export(typeof(ILogger))]
- public class ConsoleLogger: ILogger
- {
- void ILogger.Write(string message)
- {
- Console.WriteLine(message);
- }
- }
- [Export(typeof(ILogger))]
- public class DebugLogger : ILogger
- {
- void ILogger.Write(string message)
- {
- Debug.WriteLine(message);
- }
- }
- [Export(typeof(ILogger))]
- public class EventLogLogger : ILogger
- {
- void ILogger.Write(string message)
- {
- EventLog.WriteEntry("MEFSample", message);
- }
- }
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.
Code Snippet
- [ImportMany]
- public ILogger[] Loggers { get; private set; }
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 catalog (that telling MEF where to look for parts discovery)
Create container (host), and handing the catalog.
Call the Compose method of the container (this is where the Logger property get assigned with the discovered loggers).
the final step is to gets the _logger from the container (notice that the LoggerProxy class is decorated with Export attribute)
Note: at this stage we will ignore the CompositionBatch because it will likely be removed from the release version.
Code Snippet
- [Export]
- public class LoggerProxy
- {
- private static LoggerProxy _logger;
-
- static LoggerProxy()
- {
- var batch = new CompositionBatch();
- var catalog = new AssemblyCatalog(typeof(LoggerProxy).Assembly);
- var container = new CompositionContainer(catalog);
- container.Compose(batch);
-
- _logger = container.GetExportedValue<LoggerProxy>();
- }
-
- [ImportMany]
- public ILogger[] Loggers { get; private set; }
-
- public static void Write(string message)
- {
- foreach (var logger in _logger.Loggers)
- {
- logger.Write(message);
- }
- }
- }
actually the VS 2010 MEF version is one step behind the MEF CTP 8,
so the composition stag will may handle slightly different in the release version
(see the MEF CTP 8 version of this post)
Download
the code for this post available here.
Summary
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)
