MVVM 把数据访问层放在哪里? [英] MVVM where to put Data Access Layer?

查看:25
本文介绍了MVVM 把数据访问层放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 WPF 的 MVVM 设计模式.但不确定将数据访问代码放在哪里?

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

在我看过的一些示例中,数据访问是直接在 ViewModel 中执行的.在 ViewModel 中放入 linq to sql 之类的东西似乎很奇怪?其他例子有一个单独的数据访问项目,这看起来更像吗?

In some examples I have looked at, data access is performed directly in the ViewModel. It seems odd to put something like linq to sql in the ViewModel? Other examples have a seperate project for Data Access, this seems more like it?

这是通用的方法吗?我觉得我在这里错过了什么!

Is this there a general approach? I feel like I am missing something here!

谢谢

推荐答案

我会再添加一层,本质上你想要的是一个数据工厂.您想创建一组类,这些类将为您 CRUD 到数据库并将干净的 POCO 对象返回给 ViewModel.

I would add another layer, essentially what you want is a data factory. You want to create a set of classes that will CRUD to the database for you and return clean POCO objects to the ViewModel.

一个很好的例子是 书呆子晚餐书.它涵盖了 MVC 而不是 MVVM,但模式非常相似,它们在该解决方案中访问数据的方式将是一个很好的起点.

A good example to look at would the Nerd Dinner book. It covers MVC not MVVM but the patterns are very similar and the way they access data in that solution would be good starting point.

希望这会有所帮助.

这篇关于MVVM 把数据访问层放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆