MVVMCross中特定于平台的IoC [英] Platform-specific IoC in MVVMCross

查看:100
本文介绍了MVVMCross中特定于平台的IoC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在给MVVMCross打个比方,看看它是否会在即将到来的一些更大的项目中使用,这很棒.我喜欢导航,viewModel位置和通用的跨平台方法,这正是我所需要的.但是,根据平台的不同,我有点想分离一些依赖项注入.

I'm giving a MVVMCross a spin, to see if it will be of use in some bigger projects coming up, and it's great. I like the navigation, viewModel location and general cross-platform approach, which is just what I need. However, I'm a bit stuck on splitting out some of the dependency injection depending on the platform.

因此,我们有一个基本应用程序,带有一个共享的可移植库,用于在启动时初始化服务引用:

So, we have the basic application, with a shared portable library, that initialises the service references when starting up:

    public TwitterSearchApp()
    {
        InitaliseServices();
    }

    private void InitaliseServices()
    {
        this.RegisterServiceInstance<ITwitterSearchProvider>(new TwitterSearchProvider());
    }

好.定义了将在所有平台上使用的服务实现.但是,在那种情况下,我需要在不同平台上实现不同的实现呢?例如,存储/缓存,其核心要求是相同的,但在手机和平​​板电脑上需要以不同的方式处理.

Fine. That defines the service implementations that will be used across all the platforms. But what about the situation where I will need different implementations on different platforms - for instance perhaps storage/caching, where the core requirement is the same, but needs to be handled differently on a phone than on a tablet.

我认为它可能会出现在安装程序的某个地方:

I thought it might go in Setup somewhere:

public class Setup : MvxBaseWinRTSetup
    {
        public Setup(Frame rootFrame): base(rootFrame)
        {
        }

        protected override MvxApplication CreateApp()
        {
            var app = new TwitterSearchApp();//set platorm specific IoC here maybe?
            return app;
        }

        protected override void AddPluginsLoaders(Cirrious.MvvmCross.Platform.MvxLoaderPluginRegistry loaders)
        { // or perhaps here?
            loaders.AddConventionalPlugin<Cirrious.MvvmCross.Plugins.Visibility.WinRT.Plugin>();
            base.AddPluginsLoaders(loaders);
        }
    }

但我不确定.我已经看到了替换ViewModel定位器的参考,但是是否有替换其他IoC服务的类似方法?

but I'm not sure. I've seen the references to replacing the ViewModel locator, but is there are similar way of replacing the other IoC services?

感谢,总体而言,在框架上做得很好,我真的很喜欢它的工作原理(除了这一点,我还不太了解)

thanks, great job on the framework in general, I really like how it works (apart from this bit, which I don't understand properly yet)

托比

推荐答案

共有三个基本选项:

1 .在您的UI项目中添加特定于平台的服务,然后在安装过程中在替代项中注册它们-您使用的替代项取决于需要何时使用服务,但在大多数情况下,您可以只使用InitializeLastChance替代项,该替代项最后会被调用初始化:

1. Add the platform specific services in your UI project and then register them in an override during setup - which override you use depends on when your services are needed, but for most cases you can just use the InitializeLastChance override which gets called at the end of initialization:

protected override void InitializeLastChance()
{
    this.RegisterServiceInstance<IMyService>(new SingletonMyService());
    this.RegisterServiceType<IMyService2, PerCallService2>();
    base.InitialiseLastChance();
}

如果最后机会"对您的服务来说太晚了-如果您在核心应用启动过程中需要该服务-那么您可以覆盖InitializeIoC之后的任何初始化步骤-例如InitializeFirstChance.有关初始化步骤的列表和顺序,请参见

If 'last chance' is too late for your service - if you need the service during the core app startup - then you can override any initialisation step after InitializeIoC - e.g. InitializeFirstChance. For the list and order of initialisation steps, see InitializePrimary and InitializeSecondary in MvxBaseSetup.cs

2 .在用户界面代码的其他位置添加特定于平台的注册-例如在特定View的构造函数中使用(此选项用处不多...但是如果您想在某些特殊情况下可以使用它...)

2. Add the platform specific registration in some other bit of the UI code - e.g. in the constructor for a specific View (this option isn't used much... but you could use it in some odd cases, if you wanted to...)

3 .使用插件-所有插件都是IoC的包装.插件的缺点是它们增加了一些开发开销(您必须添加单独的项目和插件样板文件),但是它们的优点是可以在各个应用程序之间重用,并且更容易为它们编写测试应用程序和测试工具.有关插件的更多信息,请参见为Task/Intent提供单跨平台支持,请参见 http://slodge.blogspot.co.uk/2012/10/build-new-plugin-for-mvvmcrosss.html

3. Use a plugin - all plugins are is a wrapper around IoC. Plugins have the disadvantage that they add some development overhead (you have to add the separate projects and the plugin boilerplate files), but they have the advantages that they can be reused across apps and it's easier to write test apps and test harnesses for them. For more info on plugins, see Making mono cross platform support for Task/Intent and see http://slodge.blogspot.co.uk/2012/10/build-new-plugin-for-mvvmcrosss.html

我的一般建议-如果要在以后的项目中重用代码,请从第一个选项开始,然后迁移到插件...

My general advice - start with the first option and migrate out to a plugin if you want to reuse the code in later projects...

这篇关于MVVMCross中特定于平台的IoC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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