New Posts New Posts RSS Feed: Get access to the MEF catalog
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Get access to the MEF catalog

 Post Reply Post Reply
Author
kdev View Drop Down
Groupie
Groupie
Avatar

Joined: 03-Jan-2013
Posts: 83
Post Options Post Options   Quote kdev Quote  Post ReplyReply Direct Link To This Post Topic: Get access to the MEF catalog
    Posted: 07-May-2014 at 7:52am
Hi,

I have a scenario with cocktail where I need to get access to the Mef Catalog to query the Part property (http://stackoverflow.com/questions/23497474/how-to-know-the-original-type-of-a-class-exported-as-an-interface-in-mef).

I need this information to implement the TargetGuard method of the INavigator. I check if an attribute decorate (or not) the class to allow (or refuse) the navigation.

I understand why it's not possible to publish this property as it's MEF dependant and the ICompositionProvider is "universal" for all type of IoC. But it's in some case really annoying to not have access to the catalog.
I also know I could write my own implementation of MEF and give it to cocktail but it looks it's a lot of work (need to redo the bootstrapper too) for just a small thing (in my point of view).

Could it be possible to add a class (dedicated to MEF) on Cocktail which, like EntityAspect.Wrap(), could wrap the cocktail MEF implementation and publish the Catalog ?

Right now I have a workaround but it's far to be clean. in the TargetGuard method, I create a new AggregateCatalog and fill it with the loaded assemblies. It would be nicer to use the catalog present in Cocktail



Edited by kdev - 07-May-2014 at 7:55am
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 20-May-2014 at 12:04pm
As with every dependency, you just need to make the catalog available through MEF so you can inject it where you need it. Here's how you can accomplish this.

    public class AppBootstrapper : CocktailMefBootstrapper<MainViewModel>
    {
        private ComposablePartCatalog _catalog;

        protected override ComposablePartCatalog PrepareCompositionCatalog()
        {
            // Hang on to the catalog
            return _catalog = base.PrepareCompositionCatalog();
        }

        protected override void PrepareCompositionContainer(CompositionBatch batch)
        {
            base.PrepareCompositionContainer(batch);

            // Add catalog to the MEF container
            batch.AddExportedValue(_catalog);
        }
    }

    [Export]
    public class MainViewModel : Screen
    {
        [ImportingConstructor]
        public MainViewModel(ComposablePartCatalog catalog)
        {
            
        }
    }
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down