MEF for Beginner (Deployment Catalog) – part 12

2010/04/01

MEF for Beginner (Deployment Catalog) – part 12

deployment catalog,MEF,Import,Importmany,export,compose,composition

 

this is the 12th post of the MEF for Beginner series, the series TOC is available here.

this post will focus on Deployment Catalog.

 

the code sample for this post can be found here.

 

 
What is MEF Deployment Catalogs?

the deployment catalog is actually a redesign of the older package catalog.

it enable to load parts from xap packages a-synchronically.

 

Code sample

the following code sample depend on 2 assembly that should be added to the project:

  • System.ComponentModel.Composition
  • System.ComponentModel.Composition.Initialization

 

at the startup (app.xaml)

Code Snippet
  1. private void Application_Startup(object sender,StartupEventArgs e)
  2. {
  3.     InitializeContainer();
  4.     this.RootVisual = new MainPage();
  5. }
  6.  
  7. private void InitializeContainer()
  8. {
  9.     var catalogs = new AggregateCatalog();
  10.  
  11.     var catalog = new TypeCatalog(typeof(PlugIns));
  12.     catalogs.Catalogs.Add(catalog);
  13.  
  14.     // Create deployment catalog
  15.     var deployCatalog = new DeploymentCatalog("PlugIns.xap");
  16.     deployCatalog.DownloadCompleted += OnDownloadCompletedHandler;
  17.     deployCatalog.DownloadProgressChanged += OnDownloadProgressChangedHandler;
  18.     deployCatalog.DownloadAsync();
  19.  
  20.     catalogs.Catalogs.Add(deployCatalog);
  21.  
  22.     // initialize the main application composition host (container)
  23.     CompositionHost.Initialize(catalogs);
  24. }

line 3, initialize MEF before the creation of any windows that depend on MEF.

line 9, define aggregate catalog (this is basically a catalog collection).

lines 11-12, create type catalog for plug-ins within the main xap (plug-in within the main xap

can be assess directly) and add the catalog to the aggregate catalog.

line 15, create deployment catalog (it get the xap relative path as parameter).

lines 16-17, register for xap download process events (optional).

line 18, start downloading the xap.

line 20, add the deployment catalog to the aggregate catalog.

line 23, initialize the composition host with the aggregate catalog (so

latter using CompositionInitializer.SatisfyImports will operate against the aggregate catalog).

 

at the MainPage.xaml the code will look as follow:

Code Snippet
  1. public partial class MainPage:UserControl,INotifyPropertyChanged
  2. {
  3.     public MainPage()
  4.     {
  5.         InitializeComponent();
  6.         this.DataContext = this;
  7.         CompositionInitializer.SatisfyImports(this);
  8.     }
  9.  
  10.     private IEnumerable<string> _plugIns;
  11.     [ImportMany(AllowRecomposition = true)]
  12.     public IEnumerable<string> PlugIns
  13.     {
  14.         get
  15.         {
  16.             return _plugIns;
  17.         }
  18.         set
  19.         {
  20.             _plugIns = value;
  21.             if(PropertyChanged != null)
  22.                 PropertyChanged(this,new PropertyChangedEventArgs("PlugIns"));
  23.         }
  24.     }
  25.  
  26.     public event PropertyChangedEventHandler PropertyChanged;
  27. }

line 7, asking MEF to satisfy any import of the current instance (in our case the PlugIns property).

lines 11, decorate the PlugIns property for being assigned by the MEF composition.

notice that the decoration define AllowRecomposition = true, this is very important

because the deployment catalog is a-synchronic, therefore we do not control the exact time of the assignment.

 

Summary

deployment catalog is the current way for delay composition from xap packages.

it is a-synchronic and it will notify the catalog upon the download completion.

 

the code sample for this post can be found here.

 

Learn more

 

kick it on DotNetKicks.com Shout it


Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>