MVVM where to put Data Access Layer?

后端 未结 6 1410
自闭症患者
自闭症患者 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:34

    Data access should not be in the view model, as this is supposed to be a view specific (possibly simplified) representation of the domain model.

    Use a mapper of some sort to map your view model (the VM in MVVM) to your model (the first M). New objects in your model can be created using the factory pattern. Once created, you can store them in a database using the repository pattern. The repositories would then represent your data access layer. In your repository you could use an O/R mapper like NHibernate or Entity Framework.

    EDIT:
    I see that GraemeF suggests putting the data access code in the model. This is a NOT a good approach, as this would force you to update your domain model if you were to move from e.g. SQL Server to Oracle or XML files. The domain objects should not have to worry about how they are persisted. The repository pattern isolates the domain from its persistence.

提交回复
热议问题