使用MVVM时应在哪个类中加载数据 [英] In which class should I load my data when using MVVM

查看:171
本文介绍了使用MVVM时应在哪个类中加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习C#,最近我了解了WPF的MVVM设计模式.我正在编写一个简单的程序来实现此目的,但是我不确定应该在哪里编写加载数据的方法.

I'm currently learning C#, and recently I've learned about the MVVM design pattern for WPF. I am writing a simple program as a way to practice this, but I'm not sure where I should write the method that loads the data.

我有一个SalesSheet类,如下所示.这保存了我从.xls文件加载的数据.

I have a SalesSheet class, as shown below. This holds the data that I load from a .xls file.

class SalesSheet
{
    public List<Row> Rows { get; set; }

    public SalesSheet()
    {
        Rows = new List<Row>();
    }

    public class Row
    {
        public string CallType { get; set; }
        public string HistoryText { get; set; }
    }

}

我的问题是,我应该在哪里编写加载数据的方法?编写这样的方法是否不好?

My question is, where should I write the method that loads the data? Is it bad practice to write a method like:

private void LoadData(string filePath)

在模型中,并将其称为构造函数?

in the model, and call it the constructor?

我应该从ViewModel加载它吗?

Should I load it from the ViewModel?

推荐答案

通常,一个小的WPF项目应具有以下近似的文件夹结构:

In general, a small WPF project should have the following approximate folder structure:

  • 项目名称
    • 转换器
    • DataAccess
    • 数据类型
    • 图片
    • ViewModels
    • 观看次数
    • ProjectName
      • Converters
      • DataAccess
      • DataTypes
      • Images
      • ViewModels
      • Views

      DataAccess是您应该在其中存储数据访问类的文件夹.区分应用程序的各个方面是一个很好的做法.视图,视图模型和数据访问类.这被称为关注点分离,是一种很好的做法,因为(除其他事项外)它使您可以切换图层...这意味着您可以稍后添加一个网站界面(或更改数据库),同时仍保持大多数代码不变,这也使测试代码更加容易.

      DataAccess is the folder where you should store your data access classes. It is good practice to separate the various aspects of the application; the views, the view models and the data access classes. This is known as Separation of Concerns and is good practice because (among other things) it enables you to switch out layers... this means that you could later add a web interface (or change your database) while still keeping the majority of your code the same, and it also makes testing your code easier.

      您可能在此文件夹中只有一个班级,我们称它为DataProvider.在此DataProvider类中,您放置了 all 所有数据访问方法.现在,您具有数据访问的一个入口点,并且可以在基本视图模型中添加对它的引用:

      You might only have one class in this folder, let's call it DataProvider. In this DataProvider class, you put all of your data access methods. You now have one point of entry to your data access and you can add a reference to it in a base view model:

      protected DataProvider DataProvider
      {
          get { return new DataProvider(); }
      }
      

      现在您的视图模型都可以访问项目数据源,然后您可以执行以下操作:

      Now your view models all have access to the project data source and then you can do something like this:

      SomeObject someObject = DataProvider.LoadData(filePath);
      

      当然,有很多不同的方式来实现这种模式,但是希望现在您能明白这一点.

      Of course, there are many different ways of implementing this pattern, but hopefully now, you get the idea.

      这篇关于使用MVVM时应在哪个类中加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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