满足子视图模型依赖关系
我正在构建一个主从表单。主视图模型构造细节视图模型的实例。这些细节视图模型有几个依赖项,需要用新类实例来满足。 (这是因为它们需要在与主虚拟机不同的数据上下文中运行的服务层。)
满足这些依赖关系的最佳方法是什么?
谢谢你,
本
I'm building a master-detail form. The master view model constructs instances of the details view model. These details view models have several dependencies which need to be satisfied with new class instances. (This is because they need service layers that operate in a separate data context from the master vm.)
What would be the best way to fulfill these dependencies?
Thank you,
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WPF 应用程序框架 (WAF)BookLibrary 示例应用程序strong> 展示了如何使用 MV-VM 实现主/细节场景。它使用 MEF 作为 IoC 容器来满足 ViewModel 依赖关系。
The BookLibrary sample application of the WPF Application Framework (WAF) shows how to implement a Master/Detail scenario with M-V-VM. It uses MEF as IoC Container to satisfy the ViewModel dependencies.
您还可以使用容器构建详细视图:
容器将解析 IAccountService 和 ITransactionService 的依赖关系。但您仍然依赖 IOC 框架(除非您使用 CommonServiceLocator)。
以下是我使用 CommonServiceLocator 执行此操作的方法:
You can also use the container to construct the detail view:
The container will resolve the dependencies for IAccountService and ITransactionService. But you will still have a dependency on the IOC framework (unless you use the CommonServiceLocator).
Here is how I do it using the CommonServiceLocator:
一些可能性:
硬编码引用
以下方法可以解决该问题。然而,由于它引入了硬编码的依赖关系,因此使用它是不可能的。
通过 IoC 框架解决
另一个选择是让父视图模型保存对 IoC 框架的引用。这种方法引入了对 IoC 框架的主视图模型依赖。
工厂函数
Some Possibilities:
Hard-Coded References
The following approach would solve the problem. However, as it introduces hard-coded dependencies, using it is out of the question.
Resolution via IoC Framework
Another option would be for the parent view model to hold a reference to an IoC framework. This approach introduces a master view model dependency on the IoC framewok.
Factory Func<>s