C#MVVM服务层在哪里?

时间:2021-07-17 07:47:18

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).

我正在尝试开发一个与串口上的设备通信的小程序。该程序将负责格式化用户输入的数据以及读取和显示设备接收的值。我是WPF和MVVM的新手,已经对整个数据绑定/ XAML混乱有了基本的了解(我认为)。

Currently my understanding goes something like:

目前我的理解如下:

  1. View: UI only stuff. Binds to ViewModel.
  2. 查看:UI唯一的东西。绑定到ViewModel。

  3. ViewModel: Takes a model or various properties of a model and presents them in a way that the View can understand. Also provides a way for the view to modify the model.
  4. ViewModel:获取模型的模型或各种属性,并以View可以理解的方式呈现它们。还为视图提供了修改模型的方法。

  5. Model: The data the UI presents and modifies.
  6. 模型:UI呈现和修改的数据。

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.

现在我不知道为ViewModel提供了什么模型,以便整个应用程序能够了解模型的变化。

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 */
    }
}

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.

我正在努力争取一个地方放这个人,以便整个应用程序可以访问它。目前我在主窗口的ViewModel中实例化了它,但是除非我将它注入到构造函数中,否则它不能被其他ViewModel访问。对于几个类来说这很好,但是ViewModel需要的类越多越快。

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应用程序的哪个位置吗?或者,你们有一些我应该关注的例子,它们集中在整个应用程序(特别是业务逻辑)而不仅仅是MVVM的东西上吗?

1 个解决方案

#1


4  

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:

您对MVVM的理解是正确的,但“教科书描述”并不考虑服务。通常,这是通过依赖注入(DI)完成的。定义一个接口,IMyDevice并在MyDevice类中实现它。然后将其注册到您的DI容器IMyDevice - > MyDevice。通过使用DI容器(正确),您还可以将自己从VM构建图片中取出。你会有一个像这样的VM:

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

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

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

theDIContainer.Resolve<MyViewModel>();

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

它将新建MyViewModel类并自动解析并传入IMyDevice实例。

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.

DI还有更多内容,我在这里介绍......只需要一个基本的10,000英里高的答案来回答你的问题。阅读DI,了解它如何与MVVM一起发挥作用。

#1


4  

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:

您对MVVM的理解是正确的,但“教科书描述”并不考虑服务。通常,这是通过依赖注入(DI)完成的。定义一个接口,IMyDevice并在MyDevice类中实现它。然后将其注册到您的DI容器IMyDevice - > MyDevice。通过使用DI容器(正确),您还可以将自己从VM构建图片中取出。你会有一个像这样的VM:

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

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

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

theDIContainer.Resolve<MyViewModel>();

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

它将新建MyViewModel类并自动解析并传入IMyDevice实例。

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.

DI还有更多内容,我在这里介绍......只需要一个基本的10,000英里高的答案来回答你的问题。阅读DI,了解它如何与MVVM一起发挥作用。