每个“容器”类一个DAO或每个表一个DAO? [英] One DAO per 'container' class or one DAO per table?

查看:222
本文介绍了每个“容器”类一个DAO或每个表一个DAO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个'容器'类,其中包含多个数据库表中的字段,我使用DAO模式访问数据。

I have a 'container' class with fields that are contained in several database tables, and I use the DAO pattern for accessing the data.

问题是,我应该为这个'容器'类创建一个DAO,还是最好每个表有一个DAO并合并他们的数据?

The question is, should I create a single DAO for this 'container' class, or is it better to have one DAO per table and combine their data?

推荐答案

您应该根据应用程序需求设计DAO,而不是数据库的布局。开始使用一个DAO,如果它变得太大,那么以一种对你的代码有意义的方式将它重构为多个DAO。

You should design your DAO according to your application needs, not the layout of your database. Start of with one DAO, and if it becomes too large, then refactor it into multiple DAOs in a way that makes sense to your code.

DAO的重点是从应用程序隐藏任何数据库概念(如表)。您的应用程序应该只使用一些有用的方法将其视为服务。

The whole point of a DAO is to hide any database concepts (like tables) from your application. Your application should just view it as a service with some useful methods.

例如,如果您的应用程序需要一些来自Users表和EmailAddresses表的用户数据,你的应用程序代码不应该协调两个DAO - 它应该调用一个DAO方法getUserDetails(),DAO将隐藏需要调用多个表的事实。

For example, if your application needs some user data that comes from both the Users table and the EmailAddresses table, your application code should not have to coordinate two DAOs - it should call one DAO method getUserDetails() and the DAO will hide the fact that multiple tables need to be called.

我建议您提出问题中的第一个选项,但我不会将自己局限于每个容器类一个DAO这一规则。

I am recommending the first of the options in your question, but I wouldn't restrict yourself to the rule "one DAO per container class".

这篇关于每个“容器”类一个DAO或每个表一个DAO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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