MEF is the .NET framework for building Extensible application. I was trying to find some simple Hello World application but the samples I got was not exactly what I was looking for.
I was still interested in Hello World app in a structure where at one place I will define that I need Hello World functionality which will take the name and return Hello #name. Now that contract information can be used by people to implement this functionality separately and then consumer of this functionality can use the contract and different implementations of it in an extensible manner so that the moment there are additional implementations of the same functionality it is directly available to the consumer application simply by copying the implementation dll to the Extension folder.
For this example I have downloaded the MEF Preview 8 from CodePlex.
We will start with creating the project which will hold the information about the functionalities (Contracts) that needs to be implemented. Please note that the sample code provided here is in C# (don’t worry, pretty straight forward code), however if you are looking for VB.NET version I would highly recommend checking out the code conversion tool available at:
http://www.developerfusion.com/tools/convert/csharp-to-vb/
We will start by creating a Windows Class Library project and add a new Interface file called ‘IHello.cs’.
Let us add two more Class Library Projects to the same solution namely ‘Impl1’ and ‘Impl2’. These two implementation projects will need to have reference to the ‘Contract’ dll in their Bin folder. In addition to that we will also need to add the reference of the ‘System.ComponentModel.Composition.dll’ available from the MEF download from CodePlex. Now we will add ‘Hello.cs’ class that will implement the IHello interface to both of these Projects with the implementation of SayHello method as mentioned below:
Impl1:
Impl2:
We are ready with the Contract information and there are two implementations of that Contract. Now let us create a project that will consume this information in an extensible manner. We will create a Consol App that will have a reference of the Contract dll along with ‘System.ComponentModel.Composition.dll’. We will also create a folder called ‘Extensions’ in which we will copy the dll’s from the implementation projects that will be dynamically picked by our Consol App.
Let us add a Consol Project named ‘ConsolApp’ to the same solution and add the required references as mentioned above. Add the below mentioned code to ‘Program.cs’:
We need to create a folder named ‘Extensions’ in ConsolApp bin in which we will be copying the implementation dll’s.
Copy the ‘Impl1.dll’ to the ‘Extensions’ folder. Run the ConsolApp application and enter your name. We will see the result from Impl1.
Exit the application and copy the Impl2.dll to the Extensions folder and run it again.
Future Enhancements:
#1: Currently we need to restart the ConsolApp in order to reflect the changes in the Extension directory. Code needs to be updated to either Dynamically identify and update or give an option to user something like (e to Exit and r to refresh).
#2: We need to present the user with the implementation options available in the extensions folder and user can choose the extension and only the corresponding implementation is used.
Any suggestion to implement above two things are welcome also the corresponding source code will be helpful.
Sample Code (C#) can be downloaded from:
