Vyoms OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Bookmark and Share Rss Feeds

Managed Extensibility Framework (MEF) | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Sponsored Ads
Hot Jobs
Fresher Jobs
Experienced Jobs
Government Jobs
Walkin Jobs
Placement Section
Company Profiles
Interview Questions
Placement Papers
Resources @ VYOMS
Companies In India
Consultants In India
Colleges In India
Exams In India
Latest Results
Notifications In India
Call Centers In India
Training Institutes In India
Job Communities In India
Courses In India
Jobs by Keyskills
Jobs by Functional Areas
Learn @ VYOMS
GATE Preparation
GRE Preparation
GMAT Preparation
IAS Preparation
SAP Preparation
Testing Preparation
MBA Preparation
News @ VYOMS
Freshers News
Job Articles
Latest News
India News Network
Interview Ebook
Get 30,000+ Interview Questions & Answers in an eBook.
Interview Success Kit - Get Success in Job Interviews
  • 30,000+ Interview Questions
  • Most Questions Answered
  • 5 FREE Bonuses
  • Free Upgrades

VYOMS TOP EMPLOYERS

Wipro Technologies
Tata Consultancy Services
Accenture
IBM
Satyam
Genpact
Cognizant Technologies

Home » Articles » Managed Extensibility Framework (MEF)

Managed Extensibility Framework (MEF)








Article Posted On Date : Thursday, March 22, 2012


Managed Extensibility Framework (MEF)
Advertisements

MEF is a component of .NET framework 4.0, to create lightweight, extensible applications. It avoid hard dependencies and lets the application developer discover and use extensions without any configuration required.

Why MEF?

Imagine a situation where an application is using several smaller components. And the application is responsible for creating and running those components.

One possible solution could be including all the components as source code in the application. But you cannot add new components without modifying the source code.

Another solution could be to provide an interface for decoupling between application and components. So the component can implement the interface and interact with the application. But this approach too has a drawback. As the application cannot discover components by itself, it must be explicitly told which components are required and loaded.

Here MEF comes into the picture. MEF provides a way to discover components via composition. A MEF component specifies both its dependencies (known as imports) and what capabilities (known as exports) it makes available.

Let's understand it with the help of an example. Suppose you are making a simple calculator application which currently supports addition and subtraction.

Creating composition container and catalogue

Composition container keeps track of which components are available for composition and what are their dependencies. It provides a way by which application can get the instance of components to be composed. We need to include System.ComponentModel.Composition in reference.

    //An aggregate catalog that combines multiple catalogs

    var catalog = new AggregateCatalog();

    //Adds all the parts found in the same assembly as the current class

    catalog.Catalogs.Add(new AssemblyCatalog(typeof(this).Assembly));

 

    //If parts are placed at some other location then adds that directory path

    //catalog.Catalogs.Add(new DirectoryCatalog(componentsDirectoryPath));

 

 

    //Create the CompositionContainer with the parts in the catalog

    CompositionContainer _container = new CompositionContainer(catalog);

 

    //Fill the imports of this object

    try

    {

        this._container.ComposeParts(this);

    }

    catch (CompositionException compositionException)

    {
        Console.WriteLine(compositionException.ToString());
   }

catalog.Catalogs.Add(new AssemblyCatalog(typeof(this).Assembly));

This adds the components from the current assembly. To add the components from some specified folder location, add a directory catalogue like:

catalog.Catalogs.Add(new DirectoryCatalog(componentssDirectoryPath));

Where componentsDirectoryPath is the path of directory where components can be found.

Import and Exports

Define an interface like it.

[Import(typeof(IOperation))]
public interface IOperation
{
    string Operate(int leftOperand, int rightOperand);
}

This interface has an attribute Import. This ImportAttribute defines that the type IOperation needs to be imported. So we implement the IOperation class, and above the class we use ExportAttribute indicating that it has the capabilities of IOperation. Also it contains ExportMetadata attribute indicating that depending on the metadata Symbol the operation is performed.

[Export(typeof(IOperation))]

[ExportMetadata("Symbol", '+')]

class Add : IOperation

{

//Implementation of IOperation

}

Now we will do lazy initialization for getting objects like this:

[ImportMany]
IEnumerable<Lazy<IOperation, IMetadata>> operations;

Lazy initialization is used so that only operations that are needed are initialized. Based on the metadata it initializes what operation is to be performed. It contains ImportMany attribute
because IOperation can be filled by many exports like add, subtract etc.

Lazy Initialization

As we see we declare an IEnumerable for lazy initialization. Now how does it work? When we run application, it initializes the catalogue and creates a container. Based on the Import
attribute it finds the components which can be filled for it. Here we have only one class Add to fill it. So operations will contains only single Lazy<IOperation, IOperationData> object,
and that object will be initialized when it will be accessed first time. So based on the operation we will call Operate function.

foreach (Lazy<IOperation, IOperationData> i in operations)

    {

        if (i.Metadata.Symbol.Equals(operation))

                 result= i.Value.Operate(left, right).ToString();

    }

I have a sample project to illustrate it. It contains 2 operations Add and Subtract in the assembly and Multiplication in a separate dll, which can be found in "<current executing assembly
path>Extensions" folder.
 






Sponsored Ads



Interview Questions
HR Interview Questions
Testing Interview Questions
SAP Interview Questions
Business Intelligence Interview Questions
Call Center Interview Questions

Databases

Clipper Interview Questions
DBA Interview Questions
Firebird Interview Questions
Hierarchical Interview Questions
Informix Interview Questions
Microsoft Access Interview Questions
MS SqlServer Interview Questions
MYSQL Interview Questions
Network Interview Questions
Object Relational Interview Questions
PL/SQL Interview Questions
PostgreSQL Interview Questions
Progress Interview Questions
Relational Interview Questions
SQL Interview Questions
SQL Server Interview Questions
Stored Procedures Interview Questions
Sybase Interview Questions
Teradata Interview Questions

Microsof Technologies

.Net Database Interview Questions
.Net Deployement Interview Questions
ADO.NET Interview Questions
ADO.NET 2.0 Interview Questions
Architecture Interview Questions
ASP Interview Questions
ASP.NET Interview Questions
ASP.NET 2.0 Interview Questions
C# Interview Questions
Csharp Interview Questions
DataGrid Interview Questions
DotNet Interview Questions
Microsoft Basics Interview Questions
Microsoft.NET Interview Questions
Microsoft.NET 2.0 Interview Questions
Share Point Interview Questions
Silverlight Interview Questions
VB.NET Interview Questions
VC++ Interview Questions
Visual Basic Interview Questions

Java / J2EE

Applet Interview Questions
Core Java Interview Questions
Eclipse Interview Questions
EJB Interview Questions
Hibernate Interview Questions
J2ME Interview Questions
J2SE Interview Questions
Java Interview Questions
Java Beans Interview Questions
Java Patterns Interview Questions
Java Security Interview Questions
Java Swing Interview Questions
JBOSS Interview Questions
JDBC Interview Questions
JMS Interview Questions
JSF Interview Questions
JSP Interview Questions
RMI Interview Questions
Servlet Interview Questions
Socket Programming Interview Questions
Springs Interview Questions
Struts Interview Questions
Web Sphere Interview Questions

Programming Languages

C Interview Questions
C++ Interview Questions
CGI Interview Questions
Delphi Interview Questions
Fortran Interview Questions
ILU Interview Questions
LISP Interview Questions
Pascal Interview Questions
Perl Interview Questions
PHP Interview Questions
Ruby Interview Questions
Signature Interview Questions
UML Interview Questions
VBA Interview Questions
Windows Interview Questions
Mainframe Interview Questions


Copyright © 2001-2024 Vyoms.com. All Rights Reserved. Home | About Us | Advertise With Vyoms.com | Jobs | Contact Us | Feedback | Link to Us | Privacy Policy | Terms & Conditions
Placement Papers | Get Your Free Website | IAS Preparation | C++ Interview Questions | C Interview Questions | Report a Bug | Romantic Shayari | CAT 2024

Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |


Copyright ©2001-2024 Vyoms.com, All Rights Reserved.
Disclaimer: VYOMS.com has taken all reasonable steps to ensure that information on this site is authentic. Applicants are advised to research bonafides of advertisers independently. VYOMS.com shall not have any responsibility in this regard.