Print Page | Close Window

Get access to the MEF catalog

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4699
Printed Date: 24-Sep-2019 at 1:13am


Topic: Get access to the MEF catalog
Posted By: kdev
Subject: Get access to the MEF catalog
Date 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




Replies:
Posted By: mgood
Date 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)
        {
            
        }
    }



Print Page | Close Window