简体   繁体   English

使用SQLite时实现MVVM设计模式的正确方法

[英]Correct way to implement MVVM Design pattern when using SQLite

So lets say I have a SQLite database of Person's with property Name 所以可以说我有一个属性为NamePerson's的SQLite数据库

Public Class Person
{

    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

And now I have a view with a ListBox Displaying those names 现在我有一个带有显示这些名称的列表框的视图

<ListBox ItemSource={Binding People}>
    <ListBox.ItemTemplate>
        <DataTemplate>
           <Label Text="{Binding Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

and my Views DataContext is PeopleViewVM 而我的Views DataContext是PeopleViewVM

Public Class PeopleViewVM
{
     Public PeopleViewVM
     {
       //Do my SQLite stuff,
       // Get IEnumerable Person's
       // Create Observable Collection of People
     }

        private ObservableCollection<Person> _people;
        public ObservableCollection<Person> People
        {
            get { return _people; }
            set 
            { 
                _people = value;
                RaisePropertyChanged();
            }
        }
}

Now I understand this is a simple example. 现在我知道这是一个简单的例子。 But I am unsure whether this is the correct implementation of the MVVM design pattern. 但是我不确定这是否是MVVM设计模式的正确实现。 If Person my model this means that the view is binding directly to the model when it is binding to the property name . 如果Person是我的模型,则意味着视图绑定到属性name时直接绑定到模型。 If I change the name of a person in code behind this won't be reflected in the view. 如果我在代码后面更改人员的姓名,则该名称不会反映在视图中。 What is the correct way to do this example using the MVVM design pattern? 使用MVVM设计模式执行此示例的正确方法是什么?

That can be the "correct" implementation, based on your requirements. 根据您的要求,这可能是“正确”的实现。 I wouldn't say that there's a "correct", and "incorrect" for this issue. 我不会说这个问题有一个“正确的”和“不正确的”。 More like: would it be better for my scenario, or not? 更像是:对我的情况会更好吗?

People choose to bind models against view directly based on their requirements, and how they feel. 人们选择直接根据他们的需求和感觉将模型与视图绑定。 Sometimes I like to simplify my models, and wrap them into "PersonViewModel", in order to expose more relevant properties, and not pollute the Model . 有时,我想简化模型,然后将它们包装到“ PersonViewModel”中,以便公开更多相关属性,而不污染Model

If that doesn't suit you, you can download ReSharper(takes care of "trying" to keep the View & viewmodel synchronized), or alternatively you can encapsulate your model further, and create a "proxy" object, as such: 如果不合适,则可以下载ReSharper(注意“尝试”以保持View&viewmodel同步),或者可以进一步封装模型并创建“ proxy”对象,例如:

Public Class PersonViewModel
{
     readonly Person _person;

     Public PersonViewModel(Person person)
     {
       _person = person;
     }

   public string Name
   {
       get { return _person.Name; }
       set 
       { 
           _person.Name = value;
           RaisePropertyChanged();
       }
   }

which seems to be pointless, but helps to keep the view and model even more separate, in case of model entities that change often. 这似乎毫无意义,但在模型实体经常更改的情况下,有助于使视图和模型更加分离。 ReSharper does take care of most cases, in my experience. 根据我的经验,ReSharper确实会处理大多数情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM