Best practices for using MEF's RegistrationBuilder with Prism

Nov 1, 2014 at 2:44 PM
Edited Nov 1, 2014 at 2:47 PM
I'm not finding any guidance on how to implement MEF's RegistrationBuilder (Convention model) in Prism - can you provide an example of a best pattern and practice for this?

Below, in my POC, I implement what little documentation I can find on the convention model topic to successfully execute imported commands but only if I manually satisfy imports.
        [ImportMany]
        public IEnumerable<ICommand> Commands { get; set; }

        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();

            // Add this class 
            AggregateCatalog
                   .Catalogs
                   .Add(new AssemblyCatalog(typeof(QuickStartBootstrapper).Assembly));

            // Add the AddIns folder 
            var catalog = new DirectoryCatalog("AddIns");
            AggregateCatalog.Catalogs.Add(catalog);

            // Export all ICommand values and import them into the Commands property
            var conventions = new RegistrationBuilder();
            conventions.ForTypesDerivedFrom<ICommand>().Export<ICommand>();
            conventions.ForType<QuickStartBootstrapper>()
                    .ImportProperty<ICommand>(p => p.Commands);

            var thisAssemblyCatalog = new 
                AssemblyCatalog(typeof (CallbackLogger).Assembly, conventions);

            this.AggregateCatalog.Catalogs.Add(thisAssemblyCatalog);

            using (var service = thisAssemblyCatalog.CreateCompositionService())
            {
                service.SatisfyImportsOnce(this, conventions);
                foreach (var command in Commands)
                {
                    if (command.CanExecute(moduleManager))
                        command.Execute(Logger);
                }
            }
        }

 
Nov 4, 2014 at 2:02 PM
My recent blog on "a way" for How to use MEF's convention model in PRISM
Marked as answer by BillKrat on 11/4/2014 at 7:02 AM