Search code examples
prism

Prism 8 support for Autofac


We have a big project that uses Autofac. We also use Prism EventAggregator and DelegateCommand within this project. We need to use regions and modules in this project. I read, unleass I miss understood, that prism no longer supports Autofac. Would anyone be kind enough, and inform me if Prism 8 supports a way to use Autofac or it will stuck with Unity and DryIOC?

Thanks in advance? Saifi


Solution

  • I had a similar problem in Prism 7, where I ended up to use Prism.Unit.PrismApplication for App.xaml.cs. But for the rest services I was able to use Autofac. Overrided ConfigureModuleCatalog

     protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
            {
                base.ConfigureModuleCatalog(moduleCatalog);
                moduleCatalog.AddModule<MyModule>();
    
                Bootstrapper.Run(this, new List<AutofacModuleBase>()
                                    {
                                        // autofac modules
                                    });
    
            }
    
    public class Bootstrapper
    {
        public static void Run(PrismApplicationBase prismApplicationBase,
            List<AutofacModuleBase> modules)
        {
           // other codes
            ContainerBuilder builder = new ContainerBuilder();            
            builder.RegisterModule(new ClientApplicationPrismModule(prismApplicationBase.Container));
            foreach (AutofacModuleBase autofacModuleBase in modules)
            {
                builder.RegisterModule(autofacModuleBase);
            }
    
            IContainer containerBuild = builder.Build();
            ServiceManager.Instance.SetContainerDirty(containerBuild);
           // other codes
        }
    
    }
    

    Resolve from Autofac

    public class ClientApplicationPrismModule : AutofacModuleBase
    {
        public ClientApplicationPrismModule(IContainerProvider container)
        {
            Container = container;
        }
    
        public IContainerProvider Container { get; set; }
    
        protected override void Load(ContainerBuilder builder)
        {
            RegisterTypeSpecial<IDialogService>(builder);
            RegisterTypeSpecial<IDialogWindow>(builder);
            RegisterTypeSpecial<IModuleInitializer>(builder);
            RegisterTypeSpecial<IModuleManager>(builder);
            RegisterTypeSpecial<RegionAdapterMappings>(builder);
            RegisterTypeSpecial<IRegionManager>(builder);
            RegisterTypeSpecial<IEventAggregator>(builder);
            RegisterTypeSpecial<IRegionViewRegistry>(builder);
            RegisterTypeSpecial<IRegionBehaviorFactory>(builder);
            RegisterTypeSpecial<IRegionNavigationJournalEntry>(builder);
            RegisterTypeSpecial<IRegionNavigationJournal>(builder);
            RegisterTypeSpecial<IRegionNavigationService>(builder);
            RegisterTypeSpecial<IRegionNavigationContentLoader>(builder);
            RegisterTypeSpecial<IContainerExtension>(builder, false);
        }
    
        protected void RegisterTypeSpecial<TFrom>(ContainerBuilder builder, bool isSingleInstace = true)
        {
            IRegistrationBuilder<TFrom, SimpleActivatorData, SingleRegistrationStyle> registration 
                = builder.Register((comp) =>
            {
                TFrom service = Container.Resolve<TFrom>();
                return service;
            }).As<TFrom>()
                .IfNotRegistered(typeof(TFrom));
    
            if (isSingleInstace)
            {
                registration.SingleInstance();
            }
        }
    }