每个模块的 Prism 统一容器 [英] Prism unity container per module

查看:78
本文介绍了每个模块的 Prism 统一容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有两个模块,我想在单独的容器中为第二个模块注册类型.没有找到任何方法来做到这一点.

I have two modules in my app, and want to register types for a second module in a separate container. Didn't find any ways to do that.

我现在看到的唯一方法是为这样的可重用类型添加前缀:

Only way I see now is add prefixes to reusable types like that:

    var foo1 = new Foo("FOO 1");
    parentContainer.RegisterInstance<IFoo>("Foo1", foo1);

    var foo2 = new Foo("FOO 2");
    parentContainer.RegisterInstance<IFoo>("Foo2", foo2);

    parentContainer.RegisterType<IService1, Service1>(new ContainerControlledLifetimeManager(), new InjectionConstructor(new ResolvedParameter<IFoo>("Foo1")));
    parentContainer.RegisterType<IService2, Service2>(new ContainerControlledLifetimeManager(), new InjectionConstructor(new ResolvedParameter<IFoo>("Foo2")));

有没有什么办法可以配置prism来使用另一个容器作为模块?

Is there any way to configure prism to use another container for module?

推荐答案

在每个模块初始化时没有直接传递新容器(子/无子)的方法.我遇到了类似的情况,我需要模块在特定的统一容器(子)中注册它们的类型.我就是这样做的.
首先,我创建了一个继承自 UnityContainer 的新 Unity 容器.根据目录中的模块名称创建子容器字典.

There is no direct way of passing a new container(child/no child) while every module initialization. I had a similar situation, where I needed modules to register their types in a specific unity container (child). This is how I did it.
First of all, I created a new Unity container inherited from UnityContainer. Created a dictionary of child containers as per the module names in the catalogue.

public class NewContainer : UnityContainer
{
    private readonly IDictionary<string, IUnityContainer> _moduleContainers;

    public NewContainer(ModuleCatalog moduleCatalog)
    {
        _moduleContainers = new Dictionary<string, IUnityContainer>();
        moduleCatalog.Modules.ForEach(info => _moduleContainers.Add(info.ModuleName, CreateChildContainer()));
    }

    public IUnityContainer GetModuleContainer(string moduleName)
    {
        return _moduleContainers.ContainsKey(moduleName) ? _moduleContainers[moduleName] : null;
    }

}

现在我实现的每个模块都必须从 ModuleBase 实现,它使用父 UnityContainer 中为该模块提供的子容器.现在在子容器中注册您的类型.

Now every module which I implement must be implemented from ModuleBase which uses child containers provided for that module in the parent UnityContainer. Register your types in the child container now.

public abstract class ModuleBase : IModule
{
    protected IUnityContainer Container;

    protected ModuleBase(IUnityContainer moduleContainer)
    {
        var container = moduleContainer as NewContainer;
        if (container != null)
            Container = container.GetModuleContainer(GetType().Name);
    }

    public abstract void Initialize();
}

这就是我在引导程序中使用容器的方式 -

This is how I used the container in my bootstrapper -

public class NewBootStrapper : UnityBootstrapper
{
    private readonly ModuleCatalog _moduleCatalog;
    private DependencyObject _uiShell;

    public NewBootStrapper()
    {
        _moduleCatalog = Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("/projectName;component/ModulesCatalog.xaml",
                UriKind.Relative));         
    }

    protected override IUnityContainer CreateContainer()
    {
        return new DocumentBootStrapperContainer(_moduleCatalog);
    }
    protected override IModuleCatalog CreateModuleCatalog()
    {
        return new AggregateModuleCatalog();
    }
    protected override void ConfigureModuleCatalog()
    {
        ((AggregateModuleCatalog)ModuleCatalog).AddCatalog(_moduleCatalog);
    }
}

这篇关于每个模块的 Prism 统一容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆