What is MEF?How MEF Works?

 Posted by Niladri.Biswas on 7/11/2012 | Category: .NET Framework Interview questions | Views: 3411 | Points: 40

MEF definition:
Managed Extensibility Framework is a new framework being incorporated inside the dotnet framework 4.0 whose purpose is to
plug-in components to an already running application. It comes under the System.ComponentModel.Composition.dll namespace.

Working of MEF:

MEF works upon the principle of demand/need and supply of parts.Initially we create a container and load all the possible sources of parts (export) into it and then Compose it. Whenever there is a requirement of some parts to be plugged into the application, MEF will perform a dynamic discovery of those parts from some location (which we call as Catalog). The composition engine resolves all the dependencies and throws an exception if it fails to do so. During composition phase,all the suppliable parts gets instantiated.The suppliable parts must be discovered and both the need and the suppliable parts must agree to the contract.

//Step 1: Initializes a new instance of the AssemblyCatalog  

var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

//Step 2: The assemblies obtained in step 1 are added to the CompositionContainer
var container = new CompositionContainer(assemblyCatalog);

//Step 3: Import and Export components parts are created here

Asked In: Some Interviews | Alert Moderator 

Comments or Responses

Login to post response