对于可测试性来说,最好的数据库访问模式是什么?
我已经阅读并涉足过一些内容,包括活动记录、存储库、数据传输对象。 哪个最好?
I've read about and dabbled with some including active record, repository, data transfer objects. Which is best?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个很好的问题,应该引起一些思考。
数据库访问通常不需要经过严格的测试——尤其是自动化测试,我当然希望增加对数据库的测试量。
我正在使用从 Visual Studio 内部运行的 MbUnit 测试框架来进行一些测试。
我们的应用程序尽可能使用存储过程,我编写的测试设置数据库进行测试,调用存储过程并检查结果。
对于相关存储过程的集合,我们有一个 C# 文件,其中包含对这些存储过程的测试。 (但是,到目前为止,我们的覆盖率可能约为 1%!)。
活动记录是一个有吸引力的选择,因为 Ruby 本身就强调自动化测试。 如果我重新开始,这将是使用活动记录的一个点。
This is a good question which should provoke some thought.
Database access is often not subject to rigorous testing - particularly not automated testing, and I would certainly like to increase the amount of testing on my database.
I'm using the MbUnit test framework running from inside Visual Studio to do some testing.
Our application uses stored procedures wherever possible, and the tests I have written set up the database for testing, call a stored procedure, and check the results.
For a collection of related stored procedures, we have a C# file with tests for those stored procs. (However, our coverage is probably about 1% so far!).
Active record is an attractive option because of Ruby's built-in emphasis on automated testing. If I were starting over, that would be a point for using active record.
“最佳”问题并不真正有效。 世界充满了组合和变化。 您应该从必须回答的问题开始:您要解决什么问题。 回答后,您将查看最适合解决该问题的工具。
'Best' questions are not really valid. The world is filled with combination and variations. You should start with the question that you have to answer: What problem are you trying to solve. After you answer that you look at the tools that work best with the issue.
虽然我同意“最佳”问题并不是最好的形式(因为它们是如此任意),但它们也并非完全无关紧要。
在 SO 构建的世界中,开发人员投票选出“最佳”内容,为什么不提出最佳问题呢? “最佳问题”会引发讨论和不同意见。
最终,当有人“谷歌”“数据访问模式”时,他们应该会来到此页面并看到过多的内容那么答案,对吧?
While I agree "best" questions are not the greatest form (since they are so arbitrary), they're not totally irrelevant either.
In the world constructed here at S.O. where developers vote on what's "best", why not have best questions? "Best questions" prompt discussion and differing opinions.
Eventually, when someone "googles" 'data access pattern' they should come to this page and see a plethora of answers then, right?
存储库可能是可测试性的最佳模式,因为它允许您在需要测试时用模拟替换存储库。 ActiveRecord 将您的模型与数据库联系起来(有时很方便,但通常更难以测试)。
Repository is probably the best pattern for testability since it allows you to replace a repository with a mock when you need to test. ActiveRecord ties your models to the database (convenient sometimes, but generally more dificcult to test).
这实际上取决于您的任务。 至少您应该了解并理解所有数据库访问模式,以选择一种最适合当前问题的模式。
It really depends on your task. At least you should know and understand all database access patterns to choose one most suitable for current problem.