MvvmCross IoC中的子容器 [英] Child containers in MvvmCross IoC

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

问题描述

我有一个WPF MVVM应用程序,我希望将其重构为使用MvvmCross来支持Android实现的WPF和Mono.

I have a WPF MVVM application that I'd like to refactor to use MvvmCross to support a WPF and Mono for Android implementations.

当前,我们正在使用Unity 3.0进行依赖项注入,并依靠其对容器层次结构的支持(一个主容器带有主视图和视图模型及服务;对于与服务器的每次会话,一个带有视图的子容器,视图模型以及使用寿命有限的服务). MvvmCross中的IoC可以支持子容器吗?如果没有,您将如何建议以与MvvmCross兼容的方式实施外部IoC?

Currently we are using Unity 3.0 for dependency injection and rely on its support for container hierarchies (one main container w/ the main view and view model and services; and for each session with a server a child container with views, view models and services that have a limited lifetime). Can the IoC in MvvmCross support child containers? If not, how would you recommend implementing external IoC that does in a manner that is compatible with MvvmCross?

更新: 我们不需要支持多个子容器-随时可以激活一个子容器.

Update: We don't need to support multiple child containers-- just a single child container is active at any one time.

谢谢!

推荐答案

当今的MvvmCross框架主要针对移动"和平板"开发.在这一方面,到目前为止,我并没有遇到需要多个容器的情况-因此该框架采用了一种简单的单一容器方法.

The MvvmCross framework as it stands today is mainly targeted at 'mobile' and 'tablet' development. In this area there aren't many situations I've encountered so far where you need multiple containers - so the framework has adopted a simple single container approach.

如果确实要/需要多个容器,则Mvv​​mCross被设计为可以根据需要覆盖所有内容.因此,如果您想覆盖使用的IoC库,那么您应该能够使用任何PCL IoC容器(或注入其他本机容器).对于每一个,您都需要为我们的Io​​C界面提供一个适配器-请参见 IMvxIoCProvider.cs

If you do want/need multiple containers, then MvvmCross is designed so that everything can be overridden if you want to. So if you want to override the IoC library used, then you should be able to use any PCL IoC container (or inject different native ones). For each of these you will need to provide an Adapter for our IoC interface - see IMvxIoCProvider.cs

除此之外,尽管我不能完全理解您当前的需求(我理解代码胜于文本!),但是其他可能适合您特定问题领域的可能性包括:

In addition to this, whilst I don't fully understand your current needs (I understand code better than text!), then other possibilities which might suit your particular problem area include:

  • you could always leave the current MvvmCross container in place for MvvmCross objects, but use a separate IoC system for your classes. While slightly out of date, this blogpost about creating a custom Custom ViewModelLocator helped @gshackles along the way to using TinyIoC and constructor injection - see what he built in https://github.com/gshackles/CrossBar

MvvmCross容器允许您在需要时替换解析器-即,如果您为接口IMyService注册了一种类型,则注册第二种类型将替换第一种.

the MvvmCross container allows you to replace resolvers if you need to - i.e. if you have one type registered for the interface IMyService, then registering a second type will replace the first.

您可以在主MvvmCross容器中注册第二个IoCContainer-为此,您可以通过第二个容器提供子对象.这可能不是完美的构造函数注入,但是应该可以让您紧密控制子容器的生命周期

you could register a second IoCContainer with the main MvvmCross container - doing this you could then provide your child objects via that second container. This might not be perfect constructor injection, but should allow you to closely control child container lifecycle

进行一些小的更改(继承),您可以轻松地创建-当前唯一使该对象成为单例的是其继承.

with some small changes (inheritance), you could easily create multiple copies of the current 'simple' MvvmCross container - the only thing that currently forces this object to be a singleton is its inheritance.

进行了一些小的更改(在接口方法中添加了virtual),您还可以考虑从SimpleContainer继承以覆盖功能并提供多个解析程序字典.

with some small changes (virtual added to the interface methods), you could also consider inheriting from the SimpleContainer to override functionality and to provide multiple resolver dictionaries.

总的来说,我认为您应该为此提供很多选择.但是,需要提防的一件事是确保您了解每个应用程序平台上容器的生命周期. WPF生命周期很容易-应用程序启动,应用程序退出-但是WindowsPhone,WindowsStore和Android应用程序可以以不同的方式(快捷方式,逻辑删除,搜索等)启动

Overall, I think you should have plenty of options for this... One thing to watch out for, though, is to make sure you understand the lifecycle of your containers on each application platform. WPF lifecycles are easy - apps start, apps exit - but WindowsPhone, WindowsStore and Android apps can be launched in different ways (shortcuts, tombstoning, search, etc)

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

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