我的棱镜服务应该什么时候注册? [英] When should my prism services be registered?

查看:74
本文介绍了我的棱镜服务应该什么时候注册?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力寻找解决此问题的理想方法.现在,在创建应用程序外壳之前,我在 Boostrapper 中创建了我的服务,方法是:

I've been struggling with the ideal approach to this. Right now, I have my services created in Boostrapper right before I create my application shell, in the method:

protected override DependencyObject CreateShell() 

创建 shell 后,我创建所有视图模型,传递它们需要的服务.

After my shell gets created, I then create all my view models, passing the services they need.

首先,我想知道这是否是一个好的做法.另外,我试图在 .config 文件中找到声明服务的示例,但我真的没有看到.这也不是一个好习惯吗?

So firstly, I want to know if that's a good practice. Also, I've tried to find examples of declaring services inside a .config file, but I really didn't see any. Is this not a good practice either?

示例:

    protected override DependencyObject CreateShell()
    {
        appWnd = ServiceLocator.Current.GetInstance<ApplicationWindow>();
        Container.RegisterInstance<ILicensing>(new LicensingService());
        Container.RegisterInstance<IAnotherService>(new AnotherService());

        return appWnd;
    }

推荐答案

UnityBootstrapper 的 ConfigureContainer() 方法应该被覆盖以执行您的要求:

The method ConfigureContainer() of the UnityBootstrapper is supposed to be overridden to do what you are asking for:

MSDN - 配置容器:

复合应用程序库和构建在其上的应用程序它的顶部依赖于用于注入所需依赖项的容器.在容器配置阶段,几个核心服务是已注册,如以下来自 UnityBootstrapper 的代码所示.

Both the Composite Application Library and the applications built on top of it depend on a container for injecting required dependencies. During the container configuration phase, several core services are registered, as shown in the following code from the UnityBootstrapper.

UnityBootstrapper 上的 MSDN

MSDN 示例:

protected virtual void ConfigureContainer()
{
    …
    if (useDefaultConfiguration)
    {
        RegisterTypeIfMissing(typeof(IServiceLocator), typeof(UnityServiceLocatorAdapter), true);
        RegisterTypeIfMissing(typeof(IModuleInitializer), typeof(ModuleInitializer), true);
        RegisterTypeIfMissing(typeof(IModuleManager), typeof(ModuleManager), true);
        RegisterTypeIfMissing(typeof(RegionAdapterMappings), typeof(RegionAdapterMappings), true);
        RegisterTypeIfMissing(typeof(IRegionManager), typeof(RegionManager), true);
        RegisterTypeIfMissing(typeof(IEventAggregator), typeof(EventAggregator), true);
        RegisterTypeIfMissing(typeof(IRegionViewRegistry), typeof(RegionViewRegistry), true);
        RegisterTypeIfMissing(typeof(IRegionBehaviorFactory), typeof(RegionBehaviorFactory), true);
    }
}

您也可以在此处注册实例等,直接使用 Container,就像您已经这样做的那样.

You can also register instances here, etc., using Container directly, as you already do.

CreateShell() 方法不是执行此操作的地方,因为您只能在此处创建外壳程序.

The CreateShell() method is not the place to do this, as you should do nothing more than creating the shell here.

所以,简而言之,只需覆盖 ConfigureCatalog() 并将您的代码粘贴到那里.

So, in short, just override ConfigureCatalog() and paste your code there.

这篇关于我的棱镜服务应该什么时候注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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