如何在视图模型中获取子容器引用
我正在尝试将包含在存储库中的数据服务(实体管理器)从模块 A 中的 ViewModel(称为“AVM”)共享到模块 B 中的 ViewModel(称为“BVM”),但我无法正常工作。我们使用 PRISM/Unity 2.0
这是我的场景:
用户可以打开多个客户屏幕(复合视图作为迷你外壳) 客户(工作单元)。我们使用子容器来实现这一点。每个子容器使用自己的实体管理器解析其自己的存储库(存储库是子容器中的单例)。这是在模块 A 中完成的。主 shell 有一个主区域管理器,每个客户屏幕及其子容器都会创建一个作用域区域。 在每个客户屏幕中,都有一个视图“AV”(连接到 ViewModel“AVM”),其中有一个注册为“SubRegion”的子区域(选项卡控件)。我们使用“屏幕工厂”创建它,
在模块 B 中,我们在视图“BV”和视图模型“BVM”中有一个客户订单。在模块B的构造函数中,我们通过注入获得主容器。在初始化方法中,我们解析(主)区域管理器并向其注册视图“BV”。在视图“BV”的构造函数中,注入/创建了 ViewModel“BVM”。
现在可以了,但是 ViewModel“BVM”无法获取子容器。它只获取主容器。 这是可行的,还是我必须以另一种方式做到这一点?
谢谢, 尼尔斯
I´m trying to share a Data Service (Entity Manager) wrapped in a Repository from a ViewModel (called 'AVM') in Module A to a ViewModel (called 'BVM') in Module B, and I can't get this working. We use PRISM/Unity 2.0
This is my scenario:
A user may open multiple Customer screens (composite view as mini shell) each with another
customer (unit of work). We realize this using child containers. Each child container resolves it's own repository with its own Entity manager (the repository is a singleton within the child container). This is done in module A. The main shell has a main region manager, and each Customer screen with its childcontainer creates a scoped region.
In each customer screen there is a View 'AV' (connected to ViewModel 'AVM') with a SubRegion (tab control) registered as 'SubRegion'. We create this with a 'Screen Factory'
In Module B we have a Customer Orders in View 'BV' and ViewModel 'BVM'. In the constructor of Module B we get the main container by injection. In the initialize method we resolve the (main) region manager and register View 'BV' with it. In the constructor of View 'BV' a ViewModel 'BVM' is injected/created.
Now this works, but the ViewModel 'BVM' cannot get the child container. It only get the main container.
Is this doable, or do I have to do this another way?
Thanks,
Niels
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种可能性:
我最喜欢的选择是完全在专门用于 Ioc 的模型绑定器中执行此操作 - 无论如何,这是您必须做的。我喜欢 Autofac 使用的模型和 代码非常简洁地说明了这一点。
我对 Unity 一无所知 - 自 v1.1 以来我就没有接触过它。如果您自己滚动,一种方法是获取创建的子容器并将其注册到 TLS。然后,您可以覆盖 Ioc 模型绑定器功能以跟踪 TLS 存储的子容器。相当黑客,但我不确定还有其他方法。我首先会选择上面的选项#1。将选项 #2 视为研究统一选项的唯一方法。
There are a couple of possibilities:
My favorite option is do this entirely inside of a model binder specialized for Ioc - which is what you'll have to do anyway. I like the model that Autofac uses and the code spells it out quite succinctly.
I have no idea around Unity - I haven't touched it since v1.1. If you are rolling your own, one method is to take the created child container and register it in TLS. You can then override the Ioc model binder functionality to go after the TLS stored child container. Pretty hackish, but I'm not sure of another way. I would go with option #1 above first and foremost. Look to option #2 as only a way to investigate options wrt to unity.