C# MVVM 服务层在哪里? [英] C# MVVM Where Does the Service Layer Sit?

查看:34
本文介绍了C# MVVM 服务层在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个小程序,该程序将与串行端口上的设备进行通信.该程序将负责格式化用户输入的数据以及读取和呈现设备接收到的值.我对 WPF 和 MVVM 非常陌生,并且对整个数据绑定/XAML 混乱(我认为)有了基本的了解.

I am attempting to develop a little program which will communicate with a device on a serial port. The program will be responsible for formatting user entered data and reading and presenting values received by the device. I am pretty new to WPF and MVVM and have achieved a basic understanding of the whole databinding / XAML mess (I think).

目前我的理解是这样的:

Currently my understanding goes something like:

  1. 视图:只有用户界面的东西.绑定到 ViewModel.
  2. ViewModel:获取模型或模型的各种属性,并以视图可以理解的方式呈现它们.还为视图提供了一种修改模型的方法.
  3. 模型:用户界面呈现和修改的数据.

现在我不知道是什么为 ViewModel 提供了模型,以便整个应用程序都知道模型的变化.

Now I am at a loss as to what provides Models to the ViewModel such that the application as a whole is aware of changes to Models.

该模型目前类似于以下内容.我的设备获取校准记录,可以回读所有校准记录.

The Model currently looks something like the following. My device takes calibration records and can read back all the calibration records.

public class Device : ObservableObject
{
    public ObservableCollection<CalibRecord> CalibRecords { get; set; }

    private SerialPort sp;

    public Device(SerialPort port)
    {
        this.sp = port;
        this.CalibRecords = new ObservableCollection<CalibRecord>();
    }

    public void WriteCalibration(CalibRecord record)
    {
        /* Write a calibration record to the device */
    }

    public void ReadCalibration()
    {
        /* Read all calibration records from the device and update CalibRecords */
    }
}

我正在努力寻找一个地方来放置这个人,以便整个应用程序都可以访问它.目前我在主窗口的 ViewModel 中实例化了它,但是除非我将它注入到构造函数中,否则其他 ViewModel 无法访问它.这对于几个类来说很好,但随着 ViewModel 需要的类越多,它很快就会变得笨拙.

I am struggling for a place to put this guy so that it can be accessed by the entire application. Currently I instantiated it in the main window's ViewModel but then it can't be accessed by other ViewModels unless I inject it into the constructor. This is fine for a couple classes but gets unwieldy quickly the more classes a ViewModel needs.

也许这就是所谓的业务逻辑"或服务层".你能帮我理解把业务逻辑放在 MVVM 应用程序的什么地方吗?或者,你们有没有我应该查看的一些示例,这些示例侧重于整个应用程序(尤其是业务逻辑),而不仅仅是 MVVM 的内容?

Perhaps this is what the so-called "business logic" or "service layer" is. Can you help me understand where to put the business logic in an MVVM app? Or, do you guys have some examples I should look at that focuses on the whole application (particularly the business logic) and not just the MVVM stuff?

推荐答案

你对 MVVM 的理解是正确的,但是教科书式的描述"并没有说明服务.通常这是通过依赖注入 (DI) 完成的.定义一个接口 IMyDevice 并在 MyDevice 类中实现它.然后将它注册到您的 DI 容器 IMyDevice -> MyDevice.通过使用 DI 容器(正确地),您还可以将自己从 VM 构造图中解脱出来.您将拥有一个类似于以下内容的 VM:

Your understanding of MVVM is correct, but the "textbook description" doesn't account for services. Typically this is done with dependency injection (DI). Define an interface, IMyDevice and implement it in a MyDevice class. Then register it with your DI container IMyDevice -> MyDevice. By using a DI container (properly) you'll also take yourself out of the VM construction picture. You would have a VM something like:

public class MyViewModel : ViewModelBase
{
  public MyViewModel(IMyDevice myDevice)
  {
  }
}

要获取 VM 的实例,您可以:

to get an instance of the VM, you would do:

theDIContainer.Resolve<MyViewModel>();

它会更新 MyViewModel 类并自动为您解析和传递 IMyDevice 实例.

and it would new up the MyViewModel class and automatically resolve and pass in the IMyDevice instance for you.

DI 的内容还有很多,然后我在这里介绍了……只是对您的问题的基本 10,000 英里高答案.阅读 DI 并了解它如何与 MVVM 配合使用.

There is a lot more to DI then I covered here... just a basic 10,000 mile high answer to your question. Read up on DI and see how it comes into play with MVVM.

这篇关于C# MVVM 服务层在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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