使用 Unity 扩展在 Prism 中使用子容器

发布于 2024-11-28 13:47:54 字数 1730 浏览 2 评论 0原文

上下文

我正在寻找一种在 Silverlight 应用程序中使用 PRISM 和 unity 构建系统的方法,以便我可以拥有一个用例控制器来管理导航和与特定用例相关的其他内容。该用例控制器应该有自己的统一容器,以便我可以隔离该用例所需的依赖项。

代码

以下代码片段用于初始化用例控制器并对其进行设置,以便它使用正确的子容器。

public class MyController
{
    private IUnityContainer _container;

    public MyController(IUnityContainer container)
    {
        _container = container;

        _container.RegisterType<Object,ChildView>(ModuleViews.MyChildView);
    }

    [Dependency]
    public IRegionManager RegionManager { get; set; }

    public StartUseCase()
    {
        // This results in a resolve action for System.Object instead of the view I registered
        // in the child container
        this.RegionManager.RequestNavigate(ModuleRegions.ChildRegion,ModuleViews.MyChildView);
    }

    public static MyController Create(IUnityContainer container)
    {
        var childContainer = container.CreateChildContainer();
        childContainer.RegisterInstance<IUnityContainer>(childContainer);

        // The container view is registered in the parent container
        var containerView = container.Resolve<Object>(GlobalViews.MyContainerView);
        var childRegionManager = parentRegionManager.Regions[GlobalRegions.MainRegion].Add(containerView,GlobalViews.MyContainerView,true);

        childContainer.RegisterInstance<IRegionManager>(childRegionManager);

        var controller = childContainer.Resolve<MyController>();

        return controller;
    }
}

问题

基础知识有效,但是当我尝试使用 RegionManager.TryNavigate(...) 时,它无法解析具有指定名称的视图。我已在子容器中使用正确的名称注册了视图,但显然我在用例控制器中获得的 RegionManager 只解析了我在根容器中注册的视图。

我的问题

我需要做什么才能使子视图正确解析,而不将其注册到父统一容器中。

Context

I'm looking for a way to build system using PRISM and unity in a Silverlight application so that I can have a use case controller that manages the navigation and other stuff related to a specific use case. This use case controller should have its own unity container so that I can isolate the dependencies needed for that use case.

The code

The following snippet is used to initialize the use case controller and set it up so it uses the correct child container.

public class MyController
{
    private IUnityContainer _container;

    public MyController(IUnityContainer container)
    {
        _container = container;

        _container.RegisterType<Object,ChildView>(ModuleViews.MyChildView);
    }

    [Dependency]
    public IRegionManager RegionManager { get; set; }

    public StartUseCase()
    {
        // This results in a resolve action for System.Object instead of the view I registered
        // in the child container
        this.RegionManager.RequestNavigate(ModuleRegions.ChildRegion,ModuleViews.MyChildView);
    }

    public static MyController Create(IUnityContainer container)
    {
        var childContainer = container.CreateChildContainer();
        childContainer.RegisterInstance<IUnityContainer>(childContainer);

        // The container view is registered in the parent container
        var containerView = container.Resolve<Object>(GlobalViews.MyContainerView);
        var childRegionManager = parentRegionManager.Regions[GlobalRegions.MainRegion].Add(containerView,GlobalViews.MyContainerView,true);

        childContainer.RegisterInstance<IRegionManager>(childRegionManager);

        var controller = childContainer.Resolve<MyController>();

        return controller;
    }
}

Problem

The basics work, but when I try to use RegionManager.TryNavigate(...) it doesn't resolve the view with the specified name. I have registered the view with the correct name in the child container, but apparantly the RegionManager I got in my use case controller somehow only resolves views that I have registered in the root container.

My question

What do I need to do to be able to make the child view resolve correctly, without registering it in the parent unity container.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫腻 2024-12-05 13:47:54

这是因为该区域正在尝试使用默认的 ServiceLocator 解析您的视图。当该区域尝试为您解析视图时,它会转到 RegionNaviationService 并使用它来尝试解析视图。默认的 RegionNavigationService 使用 ServiceController 来执行此操作。

如果你想解决这个问题,你可以通过将一个值传递给该区域的 NavigationService 属性来为你的区域提供一个新的 RegionNavigationService 。您可以使用它来确保您的区域从您希望其使用的 Unity 容器(而不是默认使用的默认 ServiceLocator)解析其视图。

希望这有帮助

This is because the Region is trying to resolve your views using the default ServiceLocator. When the region tries to resolve your views for you it goes to a RegionNaviationService and uses this to try to resolve the View. The default RegionNavigationService uses the ServiceController to do this.

If you want to get around this you can give your region a new RegionNavigationService by passing one to the NavigationService property of the region. You can use this to ensure that your region resolves it's views from the Unity container you want it to use rather than the default ServiceLocator that it uses by default.

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文