Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4181 |  Welcome, Guest!   Register  Login
Home > Articles > Others > Plug-ins / Modules Development with MEF -Part I

Plug-ins / Modules Development with MEF -Part I

1 vote(s)
Rating: 5 out of 5
Article posted by Goldytech on 2/17/2010 | Views: 4468 | Category: Others | Level: Beginner red flag


In this post, I show you how to create a decoupled application using Managed Extensibility Framework (MEF)

INTRODUCTION
Managed Extensibility Framework (MEF) is the next BIG thing happening in .Net Framework 4. This framework allows the developers to extend their apps in completely decoupled fashion.So what does that means well you can simply plug in (let us say new module) without recompiling the your main application. All that is required here that all the modules of the application follows the same contract.In this post , which is part one of two series. I will do a walkthrough of some real time scenario where you can apply MEF practically. Before you continue to read further , I strongly recommend that you get sound understanding of MEF. This post only talks about the MEF stuff that will be used in this project. You can look below in the reference section below to learn MEF.
MEF Scenario
Imagine you are working as a Solution Architect for some product based company , who has a product of HRMS. Now in this product there can be multiple modules like for instance
  • Personal
  • Payroll
  • Recruitment
Your boss wants you to write this software in a such a manner that he can sell each license individually e.g. He can sell only Personal Module to one client to another client he may sell just two modules or to the another client he may sell the whole HR suite. The integration of other modules should be seamless that it does not disturbs the existing modules. I hope you have got the understanding now. Sounds Tuff , well not exactly if you use MEF in your development.
Getting Started
MEF is based on attributes style of programming.There are many attributes that are available for you to use. For this project we may not be using all of them but let me give you brief understanding of the attributes that we will be using for this project.
  1. Export Attribute: If you want to expose any of the component to others so that they can consume it. You must decorate that component or type with Export attribute. One of the constructor of this attribute is of Type object, which your interface class. This will be the key thing over here all our modules will be decorated with the Export attribute.
  2. Import Attribute : The import attribute is opposite of the export attribute.The import attribute will do the discovery of the parts or modules which are decorated with export attributes.The import attribute will only discover a single part whereas as ImportMany attribute will discover multiple parts.
    Tip:It is always advisable to use ImportMany attribute rather than Import attribute so that it can discover more than one parts.
  3. ExportMetaData Attribute:This attribute allows the developers to export certain metadata that can be attached to any component.It has two parameters in the constructor the metadata name of string type and the value of that metadata of object type.

So you have seen the MEF attributes that will be used for this project now let us look at other object types which will be used.

  1. CompositionContainer: This class acts like a container for all the composable parts(The parts which are decorated with Export attribute).In our project this will holds all the modules along with their metadata in the form of AggregateCatalog described below.
  2. AggregateCatalog: This is the placeholder of the all the other catalogs like AssemblyCatalog and DictionaryCatalog
  3. AssemblyCatalog:It holds the catalog information of the underlying assembly.In our project we shall be utilizing it for getting the info about the ApplicationShell.
  4. DirectoryCatalog:This catalog consists of the objects in the designated directory.In our project , this will be the directory where all the assemblies of the modules will be placed.
All these objects are available in System.ComponentModel.Composition assembly.
MEF Reference Links
CLOSURE
I hope you must have got some understanding of MEF now. Watch out this space for Part II, in which I shall be giving the complete understanding of the application along with the source code of the app.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:11 year(s)
Home page:http://goldytech.wordpress.com
Member since:Tuesday, August 04, 2009
Level:Bronze
Status: [Member]
Biography:Hello Everyone
Myself Muhammad Afzal , aka GoldyTech. Thats my pen name.
I reside in India and work as a solution Architect on .NET platform.
I hope you must have enjoyed reading my blog. Please leave your comments or suggestions good or bad and help me to improve
>> Write Response - Respond to this post and get points
Related Posts

We are happy to inform that DotNetFunda.Com and Questpond.Com have brought online training sessions on OOPS, ASP.NET, ADO.NET and Sql Server exclusively for DotNetFunda.Com members in the month of February and March 2010. Below are some faqs about these sessions.

Team Foundation Server has very rich features and this article discusses few of the features and templates in view of CMMI implementation.

MS project Add-in allows us to create ribbon through XML. XML provides many options to customize ribbon controls like place our custom images to controls, screen tip, super tip, enable/disable etc.

As part of the Microsoft .NET Community, we are the official partner of the TechCon 2011 being organized by Delhi Technology Forum and have an exciting offer for the members of DotNetFunda.com. Read more for complete details.

The main function of windows service is to run the application in the background. Mainly creating service in long running executable applications, in earlier times we called as NT services.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:47:33 AM