What is Compose in MEF?

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

MEF is a framework for creating extensible applications that allows developers to discover and use extensions with no configuration required.Part is an object (e.g. a class, a method or a property) that can be imported or exported to the application.The parts are discovered dynamically via catalogs.Import attribute defines the need that a part has.The Export attribute fulfills that. It exposes those parts that will participate in the composition.In MEF jargon, Compose is that area where the Exported parts will be assembled with the imported ones.

e.g. say we have an interface as under

public interface ICalculator

int GetNumber(int num1, int num2);

any object that wants to be a part (Export Part) of MEF composition has to satisfy this Interface



public class Add:ICalculator
#region Interface members
public int GetNumber(int num1, int num2)
return num1 + num2;

The import part will be as under


public ICalculator CalciPlugin { get; set; }

and the composition part will be as under

public void AssembleCalculatorComponents()

//Step 1: Initializes a new instance of the
// System.ComponentModel.Composition.Hosting.AssemblyCatalog
// class with the current executing assembly.
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

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

//Step 3: Composable parts are created here i.e.
//the Import and Export components
// assembles here
catch (Exception ex)
throw ex;

In the AssembleCalculatorComponent function we are first looking for the catalogs (here Assembly Catalogs) from where the parts are coming. Once identified, the catalogs are held in a container and lastly the parts are composed.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response