Click here to Skip to main content
15,867,834 members
Articles / Programming Languages / C# 4.0

First Guide to MEF and Silverlight (Part III)

Rate me:
Please Sign up or sign in to vote.
4.99/5 (28 votes)
27 Aug 2010CPOL7 min read 67.7K   298   32   27
In this article, I described the need of MEF using a Console Application. Read it and don't forget to vote for it and share your feedback.

Introduction

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.

Image [http://www.kunal-chowdhury.com]

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.

image

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.

image

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.

image

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:

Inside the 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 IPerson.

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 EmployeeLibrary.

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:

Demo

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. Surprised smile Voila!!! It’s a blank application!!! What happened to it?

image

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.

image

Now, run your application once again and you will see the message printed in the screen.

image

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:

image

Summary

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.

History

  • 28th August, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 454560014-Jan-11 22:34
Member 454560014-Jan-11 22:34 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»15-Jan-11 6:08
professionalKunal Chowdhury «IN»15-Jan-11 6:08 
GeneralMy vote of 5 Pin
Member 347494817-Nov-10 14:59
Member 347494817-Nov-10 14:59 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»15-Jan-11 6:07
professionalKunal Chowdhury «IN»15-Jan-11 6:07 
GeneralMy vote of 5 Pin
Oscar N.28-Sep-10 15:08
Oscar N.28-Sep-10 15:08 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»29-Oct-10 20:36
professionalKunal Chowdhury «IN»29-Oct-10 20:36 
GeneralMy vote of 5 Pin
mdhussain62-Sep-10 1:27
mdhussain62-Sep-10 1:27 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»4-Sep-10 1:03
professionalKunal Chowdhury «IN»4-Sep-10 1:03 
GeneralSuperb - 5! Pin
Anantharaman_N1-Sep-10 1:23
Anantharaman_N1-Sep-10 1:23 
GeneralRe: Superb - 5! Pin
Kunal Chowdhury «IN»1-Sep-10 1:34
professionalKunal Chowdhury «IN»1-Sep-10 1:34 
GeneralMy vote of 5 Pin
Marcelo Ricardo de Oliveira30-Aug-10 8:22
mvaMarcelo Ricardo de Oliveira30-Aug-10 8:22 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»30-Aug-10 8:28
professionalKunal Chowdhury «IN»30-Aug-10 8:28 
GeneralExcellent Pin
rahim md30-Aug-10 6:17
rahim md30-Aug-10 6:17 
GeneralRe: Excellent Pin
Kunal Chowdhury «IN»30-Aug-10 6:20
professionalKunal Chowdhury «IN»30-Aug-10 6:20 
GeneralMy vote of 5 Pin
rahim md30-Aug-10 6:14
rahim md30-Aug-10 6:14 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»30-Aug-10 6:19
professionalKunal Chowdhury «IN»30-Aug-10 6:19 
GeneralThis is something... Pin
Sung M Kim29-Aug-10 7:45
professionalSung M Kim29-Aug-10 7:45 
AnswerRe: This is something... Pin
Kunal Chowdhury «IN»29-Aug-10 8:42
professionalKunal Chowdhury «IN»29-Aug-10 8:42 
GeneralMy vote of 5 Pin
Abhishek Sur29-Aug-10 1:41
professionalAbhishek Sur29-Aug-10 1:41 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»29-Aug-10 3:35
professionalKunal Chowdhury «IN»29-Aug-10 3:35 
Generalnice Pin
Sacha Barber27-Aug-10 21:48
Sacha Barber27-Aug-10 21:48 
GeneralRe: nice Pin
Kunal Chowdhury «IN»27-Aug-10 22:03
professionalKunal Chowdhury «IN»27-Aug-10 22:03 
GeneralMy vote of 5 Pin
Abhijit Jana27-Aug-10 17:02
professionalAbhijit Jana27-Aug-10 17:02 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»27-Aug-10 17:35
professionalKunal Chowdhury «IN»27-Aug-10 17:35 
GeneralExcellent Pin
Pete O'Hanlon27-Aug-10 10:33
subeditorPete O'Hanlon27-Aug-10 10:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.