开发者

Working with child container in prism with unity extensions

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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜