Prism: Implement Mef2Bootstrapper?

Created on 29 Dec 2015  路  7Comments  路  Source: PrismLibrary/Prism

MEF 2 is a much faster than MEF. Is it hard to write a bootstrapper that uses that version instead of the one available in .NET? Should you inherit the Bootstrapper or the MefBootstrapper class?

Maybe such a bootstrapper should be included in the library by default?

wontfix

Most helpful comment

Ok, thanks. I thought MEF 2 could replace MEF. I'm involved in a huge project that is heavily dependent on MEF, and that has proved to be a performance bottleneck. You don't happen to know how to optimize the MEF bootstrapping?

All 7 comments

MEF 2 is a light weight version of MEF specifically meant for platforms such as web. WPF will not benefit from this. It is easy to create your own base bootstrapper. Just look at the examples that are there already. WPF will not be using MEF 2. I am not sure if we will add support for MEF 2 for UWP or not.

Ok, thanks. I thought MEF 2 could replace MEF. I'm involved in a huge project that is heavily dependent on MEF, and that has proved to be a performance bottleneck. You don't happen to know how to optimize the MEF bootstrapping?

Sorry, I don't use MEF. MEF isn't a container, so I don't try to use it like one.

I have implementation of MEF2 bootstrapper, I use it in my current project it works really fast.

`using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Prism.Logging;
using Prism.Modularity;
using Prism.Regions;
using System.Composition.Convention;
using System.Composition.Hosting;
using System.Composition;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Prism;
using Prism.Events;
using Prism.Regions.Behaviors;
using Telerik.Windows.Controls;

namespace TelerikShell
{
public abstract class Mef2Bootstrapper : Bootstrapper
{
[Export]
public CompositionHost Container { get; set; }
protected ContainerConfiguration ContainerConfig { get; set; }

    protected ConventionBuilder Builder { set; get; }

    protected Mef2Bootstrapper()
    {
        ContainerConfig = new ContainerConfiguration();
        ConfigureConventions();
        ContainerConfig.WithAssemblies(AppDomain.CurrentDomain.GetAssemblies(), Builder);
    }

    protected override void InitializeModules()
    {
        this.Container.GetExport<IModuleManager>().Run();
    }

    protected virtual void ConfigureConventions()
    {
        Builder = new ConventionBuilder();

        Builder.ForType<EmptyLogger>().ExportInterfaces().Shared();
        Builder.ForType<ModuleCatalog>().ExportInterfaces().Shared();
        Builder.ForType<Mef2ServiceLocator>().ExportInterfaces().Shared();
        Builder.ForType<RegionAdapterMappings>().Export().Shared();

        Builder.ForType<SelectorRegionAdapter>().Export().ExportInterfaces().Shared();
        Builder.ForType<ItemsControlRegionAdapter>().Export().ExportInterfaces().Shared();
        Builder.ForType<ContentControlRegionAdapter>().Export().ExportInterfaces().Shared();

        Builder.ForType<RegionManager>().Export().ExportInterfaces().Shared();
        Builder.ForType<EventAggregator>().ExportInterfaces().Shared();
        Builder.ForType<RegionNavigationJournalEntry>().ExportInterfaces().Shared();
        Builder.ForType<RegionViewRegistry>().ExportInterfaces().Shared();
        Builder.ForType<RegionNavigationJournal>().ExportInterfaces().Shared();
        Builder.ForType<RegionNavigationContentLoader>().ExportInterfaces().Shared();
        Builder.ForType<RegionBehaviorFactory>().ExportInterfaces().Shared();
        Builder.ForType<ModuleManager>().ExportInterfaces().Shared();
        Builder.ForType<ModuleInitializer>().ExportInterfaces().Shared();


        Builder.ForType<AutoPopulateRegionBehavior>().Export().ExportInterfaces();
        Builder.ForType<BindRegionContextToDependencyObjectBehavior>().Export().ExportInterfaces();
        Builder.ForType<ClearChildViewsRegionBehavior>().Export().ExportInterfaces();
        Builder.ForType<DelayedRegionCreationBehavior>().Export().ExportInterfaces();
        Builder.ForType<RegionActiveAwareBehavior>().Export().ExportInterfaces();
        Builder.ForType<RegionManagerRegistrationBehavior>().Export().ExportInterfaces();
        Builder.ForType<RegionMemberLifetimeBehavior>().Export().ExportInterfaces();
        Builder.ForType<SelectorItemsSourceSyncBehavior>().Export().ExportInterfaces();
        Builder.ForType<SyncRegionContextWithHostBehavior>().Export().ExportInterfaces();
    }

    protected override void ConfigureServiceLocator()
    {
        var serviceLocator = Container.GetExport<IServiceLocator>();
        ServiceLocator.SetLocatorProvider((() => serviceLocator));
    }

    protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
    {
        var instance = Container.GetExport<RegionAdapterMappings>();
        instance.RegisterMapping(typeof(Selector), Container.GetExport<SelectorRegionAdapter>());
        instance.RegisterMapping(typeof(ItemsControl), Container.GetExport<ItemsControlRegionAdapter>());
        instance.RegisterMapping(typeof(ContentControl), Container.GetExport<ContentControlRegionAdapter>());
        instance.RegisterMapping(typeof(RadPaneGroup), Container.GetExport<RadPaneGroupRegionAdapter>());
        return instance;
    }

    public override void Run(bool runWithDefaultConfiguration)
    {
        this.ModuleCatalog = this.CreateModuleCatalog();
        this.ConfigureModuleCatalog();
        this.Container = ContainerConfig.CreateContainer();
        Mef2ServiceLocator.Host = Container;
        this.ConfigureServiceLocator();
        this.ConfigureViewModelLocator();
        this.ConfigureRegionAdapterMappings();
        this.ConfigureDefaultRegionBehaviors();
        this.RegisterFrameworkExceptionTypes();

        this.Shell = this.CreateShell();

        if (this.Shell != null)
        {
            RegionManager.SetRegionManager(this.Shell, this.Container.GetExport<IRegionManager>());      
            RegionManager.UpdateRegions();
            this.InitializeShell();
        }

        var exports = Container.GetExports<IModuleManager>();
        if (exports != null && exports.Any())
        {
            this.InitializeModules();
        }

        var modules = Container.GetExports<IModule>();
        foreach (var module in modules)
        {
            module.Initialize();
        }
    }
}

}
`

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings