MVVM从ViewModel分离数据访问

时间:2011-02-01 12:43:43

标签: c# wpf mvvm db2

我是WPF和MVVM的新手,到目前为止,我有一个应用程序,它从DB2数据库中获取一些ContactList对象,并在UI中显示它们的信息。目前我有一个ContactListModel类和一个我绑定的InformationViewModel类。我的InformationViewModel类被设置为我的View的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();
}

2 个答案:

答案 0 :(得分:4)

您需要阅读有关存储库设计模式的信息:

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

存储库将提供对域对象的访问,这意味着您的经理,模型和其他人将理解对某个存储库的访问作为实际集合,这是一个完全抽象,允许您将数据访问逻辑与业务分开。 / p>

您的模型将具有填充,存储或查找DTO的方法,这些方法已转换为域对象,之后将使用您的存储库转换为数据。

答案 1 :(得分:3)

粗略的模拟,您可以将SaveContact和DeleteContact方法添加到DataLayer类。

    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();
        }
    }