.NET 4.0 MEF FAQ (Socket, Plug, and extension)
- Introduction and Goal
- What is MEF (Manage extensibility framework)?
- How can we implement MEF in .NET 4.0?
- Can we see a simple example of MEF with the above 3 steps?
- Should data types of import and export match?
- Can we connect import and export via some defined contract rather than .NET data types?
- This looks very much similar to DI and IOC, why the re-invention?
- Can they work hand in hand?
- We would like to see a sample in Silverlight?
- Can we also see a simple Sample in WPF?
- How about some Samples in ASP.NET?
- Source code
- Reference
Introduction and Goal
What is MEF (Manage extensibility framework)?

How can we implement MEF in .NET 4.0?
- Import (Create the socket) - Use the "Import" attribute to define a hook or socket in your application.
- Export (Attach the Plug) - Use the "Export" attribute to create the plug which can attach to the plug or hook.
- Compose (Extend it) - Use the composition container and compose it.

Can we see a simple example of MEF with the above 3 steps?
- using System.ComponentModel.Composition;
- using System.ComponentModel.Composition.Hosting;
- using System.Reflection;
- class Program {
- [Import()]
- public string ImportMessage {
- set;
- get;
- }
- }
- class clsMySimpleMessage {
- [Export()]
- string ExportMessage {
- set {}
- get {
- return "Message inserted via MEF";
- }
- }
- }
- private void Compose() {
- // Get a reference to the current assemblies in the project.
- AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); // Once you have got the reference of catalog add it to the composition container.
- CompositionContainer compositionContainer = new CompositionContainer(catalog); // Finally call composeparts to connect the Import with Export.
- compositionContainer.ComposeParts(this); // use the property
- Console.WriteLine(ImportMessage);
- Console.ReadLine();
- }

Should data types of import and export match?


Can we connect import and export via some defined contract rather than .NET data types?
- interface ImyInterface {
- string getString();
- }
We need to ensure that the hooks defined in both import and export attributes are of the same interface type as shown below. The binding logic using the composition container does not change; it's the same as described in the previous section.
This looks very much similar to DI and IOC, why the re-invention?
- [Export(typeof(ImyInterface))]
- class clsMyImplementation: ImyInterface
- {
- public string getString()
- {
- return "This is my string";
- }
- }
- class Program
- {
- [Import()]
- public ImyInterface myInterface
- {
- set;
- get;
- }
- }
| Requirement | Solution |
| The application should be a tiered application with each tier decoupled from the other for better reusability and maintainability. | DI IOC |
| The application should provide a facility for exporting data into different formats. External vendors should be able to plug in their own export mechanism into the application. | MEF |
| DI / IOC | MEF | |
| Decoupling | Yes | |
| Extensibility | Yes | |
| Integrate Known things | Yes | |
| Integrate Unknown thing | Yes | |
| Automatic discovery | Yes | |
| Registration | Yes | |
| Discovery | Yes | |
| Compile-time | Yes | |
| Run time | Yes |
Can they work hand in hand?
| Requirement | Solution |
| The application should be 3 tier architecture with UI, BO, and DAL layers decoupled from each other. | DI IOC |
| The data access layer needs to be open and extensible. The application will provide the ability to vendors to plug their own data connection mechanism so that the application can connect to their proprietary databases. | MEF |

We would like to see a sample in Silverlight?
- public partial class MainPage: UserControl {
- [ImportMany]
- public ObservableCollection < UserControl > ImportedItems { get;
- set; }
- }
- [Export(typeof(UserControl))]
- public partial class SilverlightControl1: UserControl
- {
- public SilverlightControl1()
- {
- InitializeComponent();
- }
- }
- [Export(typeof(UserControl))]
- public partial class SilverlightControl2: UserControl
- {
- public SilverlightControl2()
- {
- InitializeComponent();
- }
- }
- CompositionInitializer.SatisfyImports(this);


Can we also see a simple Sample in WPF?
How about some Samples in ASP.NET?
Phrrr..phusss….tired will update soon.
Source code
You can get the source code attached with this article from the top of this article.
Reference
- Hansel and Glenn talk about the difference between MEF and IOC on http://www.hanselminutes.com/default.aspx?showID=166
- 10 reasons why to use MEF
- http://csharperimage.jeremylikness.com/2010/04/ten-reasons-to-use-managed.html
- MEF for beginners
- http://blogs.microsoft.co.il/blogs/bnaya/archive/2009/11/28/mef-for-beginner-concept-part-1.aspx
- As said by Sreeni Ramadurai its import, export and bind
- http://mstecharchitect.blogspot.com/2010/02/mef-managed-extensibility-framework.html
- Simple article on MEF
- http://www.sidarok.com/web/blog/content/2008/09/26/what-is-this-managed-extensibility-framework-thing-all-about.html

Mahesh ChandPosted Sep 3, 2010, 9:34 PM
tractor and trolly. Good one.