0

I've used MEF before, but am having problems now with the newer implementation. I'm exporting objects inheriting from a base class, that has the [InheritedExport] attribute, and am trying to get them imported.

Before, I would just have an array property with [ImportMany(typeof(mytype))], and use CompositionInitializer.SatisfyImports(this) to get the imports to work, but now I have two problems:

1) The exports are in a different, referenced assembly; 2) CompositionInitializer doesn't seem to exist in the newer MEF in the 4.5 framework.

I'm trying to create an AggregateCatalog to solve problem #1, but have no idea where to go from here.

In the end, I'm trying to import a collection that can be used by the entire WPF app, if anyone can give me any assistance in suggesting an overall solution.

Contango
  • 76,540
  • 58
  • 260
  • 305
Random
  • 1,896
  • 3
  • 21
  • 33

1 Answers1

1

1) The exports are in a different, referenced assembly;

As you say, you will need the AggregateCatalog to aggregate a number of catalogs. Usually you will need one catalog for each assembly that contains a part (export/import). This means that you will have to use the AssemblyCatalog class for each one of theses loaded assemblies. You can access the loaded assemblies through one of the types that they contain. Here's a small example that adds a couple of AssemblyCatalogs to an AggregateCatalog.

AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(SomeClassInSomeAssembly).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(SomeClassInAnotherAssembly).Assembly));

For more information on catalogs you can read this small article.

2) CompositionInitializer doesn't seem to exist in the newer MEF in the 4.5 framework.

This is only available on Silverlight. On WPF you will have to create a CompositionContainer and use its SatisfyImports method.

CompositionContainer container = new CompositionContainer(catalog);
container.SatisfyImports(someObjectWithImports);
Panos Rontogiannis
  • 4,154
  • 1
  • 24
  • 29
  • What I'm struggling with, as I see my problem more clearly, is how to share the catalog among all the classes in my WPF application. I don't think a Singleton class is the way to go, but can't find any practical examples of how it's supposed to be done. I shouldn't have to write the code to create the catalog in every part that has has imports. – Random Feb 24 '13 at 05:00
  • @Random You shouldn't pay much attention to the catalog. The catalogs are created and used to create the composition container. The composition container is the important one. Usually you have one class that creates the container and manages it. This class is a kind of bootstrap, which you simply use at the initialization phase of your app. Most of the other classes you implement do not need to know anything about it. They simply have export and import declarations. If you need some other class to compose an object then you can pass it the container. – Panos Rontogiannis Feb 24 '13 at 18:40
  • Or create a static class that manages the container config and access. – Matt May 09 '13 at 15:56