MVVM where to put Data Access Layer?

后端 未结 6 1401
自闭症患者
自闭症患者 2020-12-07 16:13

I am investigating WPF\'s MVVM design pattern. But am unsure where to put the Data Acess code?

In some examples I have looked at, data access is performed directly i

6条回答
  •  离开以前
    2020-12-07 16:37

    MVVM stands for Model, View, and ViewModel. The piece you are missing is the Model, which is where your data access code lives.

    The ViewModel takes the Model and presents it to the View for display, so typically you would have something like this:

    class PersonModel : IPerson
    {
        // data access stuff goes in here
        public string Name { get; set; }
    }
    
    class PersonViewModel
    {
        IPerson _person;
    
        public PersonViewModel(IPerson person)
        {
            _person = person;
        }
    
        public Name
        {
            get { return _person.Name; }
            set { _person.Name = value; }
        }
     }
    

    The PersonView would then bind to the properties of the PersonViewModel rather than directly to the model itself. In many cases you might already have a data access layer that knows nothing about MVVM (and nor should it) but you can still build ViewModels to present it to the view.

提交回复
热议问题