MEF for Beginner (Deployment Catalog) - part 12
MEF for Beginner (Deployment Catalog) - part 12

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
- private void Application_Startup(object sender,StartupEventArgs e)
- {
- InitializeContainer();
- this.RootVisual = new MainPage();
- }
-
- private void InitializeContainer()
- {
- var catalogs = new AggregateCatalog();
-
- var catalog = new TypeCatalog(typeof(PlugIns));
- catalogs.Catalogs.Add(catalog);
-
- // Create deployment catalog
- var deployCatalog = new DeploymentCatalog("PlugIns.xap");
- deployCatalog.DownloadCompleted += OnDownloadCompletedHandler;
- deployCatalog.DownloadProgressChanged += OnDownloadProgressChangedHandler;
- deployCatalog.DownloadAsync();
-
- catalogs.Catalogs.Add(deployCatalog);
-
- // initialize the main application composition host (container)
- CompositionHost.Initialize(catalogs);
- }
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
- public partial class MainPage:UserControl,INotifyPropertyChanged
- {
- public MainPage()
- {
- InitializeComponent();
- this.DataContext = this;
- CompositionInitializer.SatisfyImports(this);
- }
-
- private IEnumerable<string> _plugIns;
- [ImportMany(AllowRecomposition = true)]
- public IEnumerable<string> PlugIns
- {
- get
- {
- return _plugIns;
- }
- set
- {
- _plugIns = value;
- if(PropertyChanged != null)
- PropertyChanged(this,new PropertyChangedEventArgs("PlugIns"));
- }
- }
-
- public event PropertyChangedEventHandler PropertyChanged;
- }
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