【问题标题】:MVVM separation of Data Access from ViewModel数据访问与 ViewModel 的 MVVM 分离
【发布时间】:2019-01-10 10:12:43
【问题描述】:

我是 WPF 和 MVVM 的新手,到目前为止,我有一个应用程序可以从 DB2 数据库中获取一些 ContactList 对象并在 UI 中显示它们的信息。目前我有一个 ContactListModel 类和我绑定到的 InformationViewModel 类。我的 InformationViewModel 类被设置为我的视图的 DataContext。问题是我的 InformationViewModel 类还包含我的数据库访问代码,即 db 连接和 SQL 命令,我想将它移动到我的 ContactListModel 类,以便我有一个单独的数据访问层。谁能帮我这个?谢谢!

ContactListModel.cs

public class ContactListModel//: INotifyPropertyChanged
{
    public int ContactListID { get; set; }
    public string ContactListName { get; set; }
    public ObservableCollection<AggregatedLabelModel> AggLabels { get; set; }
}

InformationViewModel.cs

public class InformationViewModel
{
    public InformationViewModel()
    {
        GetData();
    }

    private ObservableCollection<ContactListModel> myContactLists;

    public IEnumerable<ContactListModel> ContactLists
    {
        get { return myContactLists; }
    }


    public void GetData()
    {

        myContactLists = new ObservableCollection<ContactListModel>();

        DB2Connection conn = null;
        try
        {
            conn = new DB2Connection("SERVER CONNECTION;");
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message + " " + ex.InnerException);
        }

        //get all contactLists and their labels
        DB2Command command = new DB2Command("SELECT QUERY");
        command.Connection = conn;

        conn.Open();

        //Add unique contactLists to dictionary
        Dictionary<int, ContactListModel> myContactDictionary = new Dictionary<int, ContactListModel>();

        using (DB2DataReader dr = command.ExecuteReader())
        {
            while (dr.Read())
            {
                int id = Convert.ToInt32(dr["CONTACT_LIST_ID"]);

                if (!myContactDictionary.ContainsKey(id))
                {

                    ContactListModel contactList = new ContactListModel();

                    contactList.ContactListID = id;
                    contactList.ContactListName = dr["CONTACT_LIST_NAME"].ToString();
                    contactList.AggLabels = new ObservableCollection<AggregatedLabelModel>()
                {
                    new AggregatedLabelModel()
                    {
                        ID = Convert.ToInt32(dr["LABEL_ID"]),
                        Name = dr["LABEL_NAME"].ToString()
                    }

                };
                    myContactDictionary.Add(id, contactList);
                }
                else
                {
                    //populate existing contact lists with remaining labels
                    ContactListModel contactList = myContactDictionary[id];

                    contactList.AggLabels.Add
                    (
                        new AggregatedLabelModel()
                        {
                            ID = Convert.ToInt32(dr["LABEL_ID"]),
                            Name = dr["LABEL_NAME"].ToString()
                        }
                    );
                }
            }
        }
        conn.Close();
}

【问题讨论】:

    标签: c# wpf mvvm db2


    【解决方案1】:

    您需要了解存储库设计模式:

    这是在创建一种类似内存的对象集合,它将您的域对象(也称为“业务对象”、“业务实体”)转换为可以理解底层存储的某种格式。

    存储库将提供对域对象的访问,这意味着您的经理、模型和其他人将对某些存储库的访问理解为一个实际的集合,这是一个完全抽象,可以让您将数据访问逻辑与业务分离。

    您的模型将具有用于填充、存储或查找已转换为域对象的 DTO 以及稍后使用您的存储库转换为数据的方法。

    【讨论】:

      【解决方案2】:

      一个粗略的模型,您可以在 DataLayer 类中添加 SaveContact 和 DeleteContact 方法。

          public class DataLayer
          {
              public ObservableCollection<ContactModel> GetContacts()
              {
                  var tList = new ObservableCollection<ContactModel>();
      
                  //load from db into tList
      
                  return tList;
              }
          }
      
          public class ContactModel
          {
              public int ContactListID { get; set; }
              public string ContactListName { get; set; }
              public ObservableCollection<AggregatedLabelModel> AggLabels { get; set; }
          }
      
          public class ContactsViewModel
          {
              public ObservableCollection<ContactModel> ListOfContacts;
      
              public ContactsViewModel()
              {
                  var dl = new DataLayer();
                  ListOfContacts = dl.GetContacts();
              }
          }
      

      【讨论】:

      • 谢谢你 - 不过有一个问题,我正在使用名为 Get Data 的方法进行数据访问,该解决方案在哪里适合?
      猜你喜欢
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 2021-06-25
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      相关资源
      最近更新 更多