Wednesday 18 May 2011

MEF basics

I’m digging into the Managed Extensibility Framework (MEF) and need to keep track of some simple terms, definitions and concepts. This post is just an aide-mémoire so don’t expect any detail. I’ve crushed the MEF documentation into some bullet points.

What is MEF?

“MEF offers discovery and composition capabilities that you can leverage to load application extensions.” *
The basic namespace for everything MEF is System.ComponentModel.Composition.

Basic terms and definitions

  • Catalog
    • Responsible for discovering extensions (ComposableParts)
    • Assembly Catalog
      • Discovers all the exports in a given assembly
    • Directory Catalog
      • Discover all the exports in all the assemblies in a directory
      • Does a one-time scan of the directory and will not automatically refresh when there are changes in the directory (can call Refresh() to rescan)
      • Not supported in Silverlight
    • Aggregate Catalog
      • Use when a combination of catalogs is needed
    • Type Catalog
      • Discovers all the exports in a specific set of types
    • Deployment Catalog
      • Silverlight only
      • For dynamically downloading remote XAPs
  • CompositionContainer
    • Interacts with Catalogs
    • Resolves a part's dependencies and exposes Exports to the outside world
  • ComposablePart
    • A composable unit within MEF
    • Offers up one or more Exports
    • May depend on one or more externally provided Imports
    • Attributed with the [System.ComponentModel.Composition.Export] and [System.ComponentModel.Composition.Import] attributes in order to declare their exports and imports.
    • Either added to the container explicitly or created through the use of catalogs
    • A common pattern is for a ComposablePart to export an interface or an abstract type contract rather than a concrete type
      • Allows the importer to be decoupled from the specific implementation of the export
  • Contract
    • The bridge between exports and imports
    • A contract is a string identifier
    • If no contract is specified, MEF will implicitly use the fully qualified name of the type as the contract
    • Every export has a contract, and every import declares the contract it needs
  • Contract Assembly
    • An assembly which contains contract types that extenders can use for extending your application
  • Exports
    • Composable Part export
      • Used when a Composable Part needs to export itself
      • Decorate the Composable Part class with the[Export]
    • Property export
      • Decorate a property with the [Export] attribute
      • Allows exporting sealed types such as the core CLR types, or other third party types
      • Allows decoupling the export from how the export is created
      • Allows having a family of related exports in the same Composable Part
    • Method export
      • Methods are exported as delegates which are specified in the Export contract
      • Allows finer grained control as to what is exported
      • Shields the caller from any knowledge of the type
      • Can be generated through light code gen
    • Inherited Exports
      • Base class / interface defines exports which are automatically inherited by implementers
      • Use [InheritedExport]
    • Lazy Exports
      • Can delay instantiation
      • Can prevent recursive composition down the graph
      • Import an [System.Lazy<T>] instead of [T] directly
  • Imports
    • Property import
      • Decorate the property with [Import]
    • Constructor parameters
      • Specify imports through constructor parameters
      • Add [ImportingConstructor] to the constructor
      • Add parameters to the constructor for each import
    • Field imports
      • Decorate the field with [Import]
    • Optional imports
      • MEF allows you to specify that an import is optional ([Import(AllowDefault=true)])
      • The container will provide an export if one is available otherwise it will set the import to Default(T)
    • Importing collections
      • Can import collections with the [ImportMany] attribute
      • All instances of the specific contract will be imported from the container and added to the collection
      • Recomposition
        • As new exports become available in the container, collections are automatically updated with the new set
        • [ImportMany(AllowRecomposition=true)]
    • IPartImportsSatisfiedNotification
      • Defines an OnImportsSatisfied method, which is called when all imports that could be satisfied have been satisfied


* MEF Overview

See also

Managed Extensibility Framework, on CodePlex