In my last two articles on MEF, i.e., “First Guide to MEF & Silverlight (Part I)” and “First Guide to MEF & Silverlight (Part II)” I described the framework with the help of a Console application and a Silverlight application. Hope that gave you a basic idea on it and what this MEF does.
In this article, I will try to give you more knowledge on MEF & its benefits. This time, I will again use a Console Application which will give more visibility to the underlying scenarios. Hope, like the previous articles, this will also help you to understand the framework better to use it in your future need.
To begin with, first read my Part – I of the tutorial here: First Guide to MEF & Silverlight (Part I). This will give you the idea on the prerequisite for developing MEF application. We will use that chapter as the base to demonstrate this chapter.
The motive of this tutorial & demo is to create an application and add its features whenever we need without changing the actual application code. Hence, at the end, we need to just plugin our new DLL to the application and that will add the new feature without any modification to the original code. In future, if we want to add one more new feature, we will be able to do this by deploying that new DLL to the client PC and he will be able to access them without upgrading the application. If he wants to remove the feature, he can easily do that by just removing the respective DLL without degrading the original application.
Sounds interesting? Then let us discuss about each point with a sample application which will give you more understanding of the power of MEF. Read the complete article and enjoy it.
Setting up the Project
Let’s start with a Console application targeting the Framework Version 4.0 using Visual Studio 2010. I am using Visual C# code format to demonstrate the sample application. If you are not familiar with that, I will suggest you read the complete article to understand the basics and later once you understand the need & use of MEF properly, you will be able to easily do it in another language.
Adding the Interface Library Project
Once your console application project has been created, add one Class Library project into the solution. To do this, right click on the solution and from the context menu, click Add –> New Project.
In the “Add New Project” dialog, select the “Class Library” template and set “
PersonLibrary” as the name of the project. Click “Ok” to tell the Visual Studio IDE to create the respective project for you.
Once the project has been created by the IDE, create a blank Interface named “
IPerson” inside the library project. The interface will look as below:
The reason behind a blank interface is just for the sake of this demo sample and to distinguish between the types to satisfy by the MEF Framework.
Setting up the Main Project
First of all, you need to add two assembly references in your Console application project. One is the
PersonLibrary project reference and the other is the System.ComponentModel.Composition.dll reference. To do this, right click on the main application project and click on “Add Reference”. Find those assemblies and include them in the project.
PersonLibrary DLL reference is required to use the
IPerson interface and the
System.ComponentModel.Composition DLL reference is required to use the MEF composition and importing parts.
Composing Parts in the Main Program
Now open your Program.cs from the Console Application project and create a
IPerson type property, so that we can import many
IPerson type contracts. Set the
ImportMany attribute to the property. Check the below code for details:
Program class, add a method called
CreateCompositionContainerFromDirectory() and create the
DirectoryCatalog pointing the root of the application first. Then, create the
CompositionContainer from the catalog and give a call to
ComposeParts() to satisfy the container. Now go inside your
Main() method and call the
CreateCompositionContainerFromDirectory() after creating an instance of the program class. The code for this is mentioned above.
Exporting Employee Class
That’s all about importing the parts. Now we have to create our contracts and export them to MEF. To do this, let us create another class library project inside the same solution. Name the new library project as
EmployeeLibrary. Once you added the new project, add the assembly reference of the
PersonLibrary project to use the
IPerson interface inside the new library. Also add the
System.ComponentModel.Composition assembly reference for MEF to export the contract.
Once you set up the project, it’s time for us to create the class. Let’s create a class named “
Employee” and inherit from the
IPerson interface. Inside the constructor, print a message so that when the instance of the class generates, it will print the message in the console. Set the attribute “
Export” to the class of type
Have a look into the code here:
Exporting Customer Class
Similar to the above library project, create another library project inside the same solution and name it as
CustomerLibrary. Add the assembly reference of the project “
PersonLibrary” and the
System.ComponentModel.Composition like you did for the
Once you setup the project, just create a similar class like “
Employee” inside this project but here we will name the class as “
Customer”. Put a different message inside the constructor, so that, once the instance of this class has been created, it will print it in the console.
Here is the code:
Woo!!! Our coding part is over. Now it is time to do a demo to showcase what we wanted to do and what we achieved. Build the whole solution and be sure that there are no errors in it and the solution builds successfully.
Go to the “bin” directory placed inside the debug folder of the main application, i.e., “
HelloMEFDemo3”. There you will find the “HelloMEFDemo3.exe” has been generated by the compiler. This is your console application that we developed just now. Double click on it to run the application. Voila!!! It’s a blank application!!! What happened to it?
Don’t worry. We run the application but the application doesn’t know about the
Employee or the
Customer class. We didn’t add the reference to import by the MEF automatically. So what do we do? We need to add the reference to the main console application, right? No, wait before doing this. We are not supposed to add the reference there. The reason behind this is: “Our application should not know about the contract parts. Later when we want, we will be able to include them automatically without writing a single piece of code to the application.”
So, what to do? Close your console application first. Now go to the bin directory of the project named “
EmployeeLibrary” and copy the library DLL to the console application output directory. See the below screenshot. I just placed the EmployeeLibrary.dll in the root folder of the executable application.
Now, run your application once again and you will see the message printed in the screen.
Close the console window and copy the CustomerLibrary.dll from the project output directory to the application root. Run the console application once more. You will see the message from the customer too printed on the screen. Have a look at the output here:
So what we learned from this small demo is: “Without changing the actual application, we can very easily plug-in our new code”. It is very easy to include additional features in our application without changing the main application. You just need to put the DLL library to the appropriate location to pick up by your app. If you need to remove some feature in the future, you just have to remove the reference DLL from the user directory where they were deployed. No need to revert your application back to a previous version.
It’s very useful for additional feature implementation and plugging them to your client application. Also, it’s very easy to maintain the versioning. Hence, it gives you more flexibility & extensibility over your code.
Hope this helped you a lot to understand the MEF in more detail. Now you can imagine the power of it for your application development. Go ahead and start exploring it for the future.
Don’t forget to vote fot it. Suggestions and feedback are always appreciated.
If you have any queries/doubts, don’t hesitate to let me know. I will always be eager to help you as early as possible.
- 28th August, 2010: Initial post