Greg Gum
Greg Gum

Reputation: 37909

Using MEF inside a class library

OK, so inside a class library, is it a good idea NOT to use MEF?

Here is an example:

ISomeInterface

5 Implementations of ISomeInterface

Once class that imports all ISomeInterface and uses them.

Again, this is all inside a single dll. Since it is in a DLL, there is no bootstrapping of MEF to create the catalog, and it seems a bit much to build a catalog just to use it once.

Am just learning MEF and how to use it.

Greg

Upvotes: 0

Views: 109

Answers (1)

Greg Gum
Greg Gum

Reputation: 37909

After reading up a bit more on this, it looks like there is no reason that MEF can't be used inside a DLL for it's own parts creation. When I asked this question, I was thinking that the Importing would be mainly inside a Main() or App() type of function to compose the entire app. But if composing needs to be done on a major part that gets exported to the app, it can still use MEF to compose itself in the constructor, like this:

//An aggregate catalog that combines multiple catalogs
        var catalog = new AggregateCatalog();
        //Adds all the parts found in the same assembly as the Program class
        catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));

        //Create the CompositionContainer with the parts in the catalog
        _container = new CompositionContainer(catalog);

        //Fill the imports of this object
        try
        {
            this._container.ComposeParts(this);
        }
        catch (CompositionException compositionException)
        {

        }

Upvotes: 1

Related Questions