满足子视图模型依赖关系

发布于 2024-09-11 20:21:29 字数 149 浏览 10 评论 0原文

我正在构建一个主从表单。主视图模型构造细节视图模型的实例。这些细节视图模型有几个依赖项,需要用类实例来满足。 (这是因为它们需要在与主虚拟机不同的数据上下文中运行的服务层。)

满足这些依赖关系的最佳方法是什么?

谢谢你,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟─花易冷 2024-09-18 20:21:29

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.

伴梦长久 2024-09-18 20:21:29

您还可以使用容器构建详细视图:

var detailViewModel = container.CreateInstance<DetailViewModel>();

容器将解析 IAccountService 和 ITransactionService 的依赖关系。但您仍然依赖 IOC 框架(除非您使用 CommonServiceLocator)。

以下是我使用 CommonServiceLocator 执行此操作的方法:

this.accountService = ServiceLocator.Current.GetInstance<IAccountService>();
this.transactionService = ServiceLocator.Current.GetInstancey<ITransactionService>();

You can also use the container to construct the detail view:

var detailViewModel = container.CreateInstance<DetailViewModel>();

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:

this.accountService = ServiceLocator.Current.GetInstance<IAccountService>();
this.transactionService = ServiceLocator.Current.GetInstancey<ITransactionService>();
我不在是我 2024-09-18 20:21:29

一些可能性:

硬编码引用

以下方法可以解决该问题。然而,由于它引入了硬编码的依赖关系,因此使用它是不可能的。

// in the master view model
var detailViewModel = new DetailViewModel(new AccountService(), new TransactionService());

通过 IoC 框架解决

另一个选择是让父视图模型保存对 IoC 框架的引用。这种方法引入了对 IoC 框架的主视图模型依赖。

// in the master view model
var detailViewModel = new DetailViewModel(resolver.GetNew<IAccountService>(), resolver.GetNew<IAccountService>());

工厂函数

class MasterViewModel {
  public MasterViewModel(Func<Service.IAccountService> accountServiceFactory, Func<Service.ITransactionService> transactionServiceFactory) {
    this.accountServiceFactory = accountServiceFactory;
    this.transactionServiceFactory = transactionServiceFactory;

    // instances for MasterViewModel's internal use
    this.accountService = this.accountServiceFactory();
    this.transactionService = this.transactionServiceFactory():
  }
  public SelectedItem { 
    set {
       selectedItem = value;
       DetailToEdit = new DetailViewModel(selectedItem.Id, accountServiceFactory(), transactionServiceFactory());
    }
    // ....

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.

// in the master view model
var detailViewModel = new DetailViewModel(new AccountService(), new TransactionService());

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.

// in the master view model
var detailViewModel = new DetailViewModel(resolver.GetNew<IAccountService>(), resolver.GetNew<IAccountService>());

Factory Func<>s

class MasterViewModel {
  public MasterViewModel(Func<Service.IAccountService> accountServiceFactory, Func<Service.ITransactionService> transactionServiceFactory) {
    this.accountServiceFactory = accountServiceFactory;
    this.transactionServiceFactory = transactionServiceFactory;

    // instances for MasterViewModel's internal use
    this.accountService = this.accountServiceFactory();
    this.transactionService = this.transactionServiceFactory():
  }
  public SelectedItem { 
    set {
       selectedItem = value;
       DetailToEdit = new DetailViewModel(selectedItem.Id, accountServiceFactory(), transactionServiceFactory());
    }
    // ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文