First Guide to MEF & Silverlight (Part–III)



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 about the framework with the help of a Console Application and a Silverlight Application. Hope that gave you basic idea on it and what this MEF does.

 

In this article I will try to give you more knowledge on MEF & it’s benefit. 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 a application and add it’s 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 want 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 points with a sample application which will give you more understanding of the power of MEF. Read the complete article & 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 to read the complete article to understand the basic and later once you understand the need & use of MEF properly, you will be able to easily do it in other 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:

 

Reason behind a blank interface is just for 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 reference 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 to the project. PersonLibrary dll reference is require to use the IPerson interface and the System.ComponentModel.Composition dll reference is require to use the MEF composition and importing parts.

 

image

 

 

Composing Parts in 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 detail:

 

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 Systam.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 you to showcase what we wanted to do and what we achieved. Build the whole solution & be sure that there are no errors in it and the solution build 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 with 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 to 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. Reason behind this is: “Our application should not know about the contract parts. Later when we want, 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 into 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 to our application without changing the main application. Just you need to put the dll library to the appropriate location to pick up by your app. If you need to remove some feature in future, you have to just remove the reference dll from the user directory where they were deployed. No need to revert back your application 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 it here in CodeProject: First Guide to MEF and Silverlight (Part–III)

Suggestions, Feedbacks are always appreciated. If you have any queries/doubts don’t hesitate to let me know. I will be always eager to help you as early as possible.

1 comments

  1. Just want to say thanks, this article is really helpful.

    ReplyDelete


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top