Prism 8 支持 Autofac
Prism 8 support for Autofac
我们有一个使用 Autofac 的大项目。我们还在该项目中使用了 Prism EventAggregator 和 DelegateCommand。我们需要在这个项目中使用区域和模块。我读到,除非我没有理解,棱镜不再支持 Autofac。
如果 Prism 8 支持使用 Autofac 的方式,或者它会卡在 Unity 和 DryIOC 中,有人会友善地告诉我吗?
提前致谢?
赛菲
我在 Prism 7 中遇到了类似的问题,我最终将 Prism.Unit.PrismApplication
用于 App.xaml.cs。但对于其余服务,我能够使用 Autofac。
覆盖了 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
}
}
从 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();
}
}
}
prism 8官方不支持autofac,你只能使用autofac直到prism 7。但是prism提供了关于如何添加自定义容器的说明:
https://prismlibrary.com/docs/dependency-injection/add-custom-container.html
我们有一个使用 Autofac 的大项目。我们还在该项目中使用了 Prism EventAggregator 和 DelegateCommand。我们需要在这个项目中使用区域和模块。我读到,除非我没有理解,棱镜不再支持 Autofac。 如果 Prism 8 支持使用 Autofac 的方式,或者它会卡在 Unity 和 DryIOC 中,有人会友善地告诉我吗?
提前致谢? 赛菲
我在 Prism 7 中遇到了类似的问题,我最终将 Prism.Unit.PrismApplication
用于 App.xaml.cs。但对于其余服务,我能够使用 Autofac。
覆盖了 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
}
}
从 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();
}
}
}
prism 8官方不支持autofac,你只能使用autofac直到prism 7。但是prism提供了关于如何添加自定义容器的说明:
https://prismlibrary.com/docs/dependency-injection/add-custom-container.html