你如何真正使用MVVM中的数据库? [英] How do you really work with a database in MVVM?

查看:158
本文介绍了你如何真正使用MVVM中的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过的绝大多数指南只是使用模型来代替真正的数据库(据说是为了简单起见),然后不解释如何实际使用真正的数据库。



剩下的就是...... 复杂



最多我见过的指南使用真正的数据库是为了设计企业应用程序,并倾向于涉及持久性无知,依赖注入,业务线,服务层,WCF,实体框架,POCO和控制反转等术语(不是一个几个星期前我听说过的。



最后,我所要做的就是学会为自己制作个人DVD追踪程序。它应该是一个独立的桌面应用程序。不是一些巨大的企业事情。



我一直依赖的书(主要是因为它以一种我可以有点的方式打破了局面了解), Silverlight和WPF中的MVVM企业架构生存指南具体说:

The vast majority of the guides I've read just use mock-ups in place of a real database (supposedly for simplicity) and then don't explain how to actually use a real database.

The rest of them are...complicated.

Most of the guides I've seen that use real databases are geared toward designing Enterprise applications and tend to involve terms like persistence ignorance, dependency injection, line of business, service layer, WCF, Entity Framework, POCOs, and inversion of control (not one of which I'd heard of a few weeks ago).

Ultimately all I'm trying to do is learn enough to make a personal DVD tracking program for myself. It should be a standalone desktop application. Not some huge enterprise thing.

The book I've been relying on (mainly because it breaks things down in a way I can kinda understand), MVVM Survival Guide for Enterprise Architectures in Silverlight and WPF specifically says:

You should only introduce a service layer in your project if it's
needed. Each new architectural feature we will cover in this text
will provide benefits for a cost. It's extremely important that you are
familiar with the trade-offs and only add what you need. If you are
writing an internal tool, adding a Service Layer might be overkill and
provide no ROI to your project.





不幸的是,这本书的结构方式,它介绍了服务层(以及持久性无知和WCF)很久才到达代码背后没有代码部分,我不知道如何分离两个。



所以这里有一个问题:对于个人使用程序:我应该如何处理数据库? WCF是否必要?如果是这样,您如何设置它来创建一个独立的应用程序?如果没有,我该怎么做?



(如果你需要我正在尝试创建的程序的更多细节,请说出来,我会把它们包括在内。)



Unfortunately, the way the book's structured, it introduces the service layer (and persistence ignorance and WCF) long before it gets around to the "no code in the code behind" part and I can't tell how to separate the two.

So here's the question: for a personal use program: how should I go about working with a database? Is WCF necessary? If so, how do you set it up to create a standalone application? If not, what should I do instead?

(If you need more details of the program I'm trying to create, please say so and I'll include them.)

推荐答案

我假设您没有为执行这个项目而选择编程,并且打算在完成后立即删除它,对吧? ;)我的意思是:不要回避至少试图以一种严肃的方式设计和构建它,因为你将通过这样做来学习。这并不是说你应该像企业应用程序那样设计它。



WCF是一个传输/远程处理层。正确实现并且不被滥用时,WCF不会向应用程序添加任何有价值的东西,除了允许跨层分割它。如果您的应用程序应该在一台PC上运行,那么您将不需要它。



我的建议:为您的数据库访问构建一个存储库层并使用您的存储库的ViewModels。存储库公开了查询和修改数据的功能,并隐藏了特定于数据库的内容。如果您想要更改数据库或稍后使应用程序可以远程访问,那么您将需要一个中心点来进行更改,并且根本不需要触摸ViewModel(理想情况下)。



虽然Kenneth建议的文章非常出色,但我认为这将是一个过于先进的过程,并且与您不熟悉的技术混合在一起。我建议在Web上搜索存储库模式的一般解释,以及使用ADO.NET进行数据库访问来演示它的文章(与实体框架相反)。
I assume you didn't pick up programming for doing this one project and intend to drop it as soon as you're done with it, right? ;) What I mean: Don't shy away from at least trying to design and build it in a no-nonsense way because you'll learn by doing so. That's not to say you should design it like an enterprise application.

WCF is a transport/remoting layer. When correctly implemented and not abused, WCF doesn't add anything of value to an application beyond enabling to split it across tiers. If your application should run on a single PC, you won't need it.

My suggestion: Build a repository layer for your database access and use that repository from your ViewModels. The repository exposes the functionality to query and modify your data and hides the database-specific stuff. If you would want to change either your database or make your application remote-able later, you would have one central point where you would have to make changes and your ViewModels wouldn't have to be touched at all (ideally).

While the article suggested by Kenneth is really excellent, I assume it will be one step too advanced and too intermingled with technology you're not familiar with yet. I suggest search for general explanations of the repository pattern on the webs and for articles that demonstrate it using ADO.NET for database access (in contrast to Entity Framework).


这篇关于你如何真正使用MVVM中的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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