Introduce to MEF (Study)- one

KEY  Concepts

  • Composition  is the practice of combining serveral objects with discrete  functionality into one or more complex objects.
  • ComposableParts are the key building blocks of MEF.  ComposableParts enable applications to expose and consume component extensions via Exports and Imports.
  • Contracts are the avenue for communication between export and import components. A Contract is normally implemented via an interface class. Contracts enable MEF ComposableParts to avoid dependencies or tight coupling with other components.
  • Conditonal Binding allows loading of components, which meet specific metadata criteria.

 

Assembly: System.ComponentModel.Composition

create host:

using System.ComponentModel.Composition.Hosting;
 
var Catalog = new AggreagteCatalog(new DirectCatalog("."),  new AssemblyCatalog(Assembly.GetExectuingAssembly());
 
var Container = new CompositonContainer(Catalog);
 
var window = Container.GetExportedValue<CashMaker>();

 

MEF’s container interacts with Catalogs to have access to Composable Parts. The container itself resolvers a part’s dependencies and exposes Exports to the outside world. you are free to add composable parts instances directly to the container if you wish.

 

There are different kinds of Catalogs, which are able to discover parts on different ways.  the full list of catalogs available on MEF is:

  • Assembly Catalog: Discovers the different parts on a specific assembly.
  • Directory Catalog:  Discovers parts inside the assembies on a specific directory.
  • Aggregate Catalog: Allows use more than one catalog combing them/
  • Type Catalog: Looks inside the assemblies for specific types.

 

you can import collections with the ImportMany attribute . This means that all instances of the specific contract will be imported from the Container. MEF parts can also support recomposition. This means that as new exports become avaiable in the container, collecitons are automatically updated with the new Set.

 

once MEF completes the CarQueries colleciton import proces, you want your application to be notified and so it can take particular action. To do this, make the CashMaker class to implement the IPartImportsSatisfiedNotification interface as shown in the following code.

 

1. ContosoAutomotive.Woodgrove project and select Properties. In the Properties page, select the Build Events tab (for C# projects) or select the Compile tab and press the Build Events… button (for Visual Basic projects) and enter the following value in the Post-build event command line:

Post-Build Command

copy "$(TargetPath)" "$(SolutionDir)ContosoAutomotive\bin\$(ConfigurationName)"

你可能感兴趣的:(int)