更新多个实体的方法通常不是 DAO 的一部分吗?
我见过一百万个 DAO 示例,大多数情况下它们都实现了单个实体的基本 CRUD 操作,可能还有一些返回列表的方法(例如 List getCustomers())。
但是,我从未见过一个例子,其中有一个方法可以更新、删除或创建多个实体,例如:void update(List)。
更新多个实体的方法通常不是 DAO 的一部分,还是只是在示例中不经常使用? 我有一个要求,我必须进行一些批量插入,并且调用 myDAO.create() 一百次并不是非常有效。
我只是想在继续做看似显而易见的事情之前确保我没有遗漏任何东西。
I've seen a million examples of DAOs, and for the most part they all implement your basic CRUD operations for single entities, with maybe a few methods that return lists (e.g. List getCustomers()).
However, I've never seen an example in which there is a method that updates, deletes, or creates multiple entities, like: void update(List).
Are methods that update multiple entities not typically part of a DAO, or are they just not often used in examples? I've got a requirement in which I've got to do some batch inserting, and calling myDAO.create() a hundred times isn't terribly efficient.
I just want to make sure I'm not missing something before I go ahead and do what seems obvious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现批量更新通常是使用数据库供应商提供的工具来完成的。
我同意我见过的 DAO 通常没有重载的创建/更新/删除方法来获取 List,但没有理由他们不能。
让我感到困惑的一个想法是,当我编写交易时,DAO 并不拥有交易。 他们永远无法知道自己是更大工作单元的一部分。 这就是服务的作用。
我的建议是不要管 DAO,让单独的服务层负责批处理操作。 服务拥有事务逻辑。 这也是包含将大型更新“分块”的逻辑的好地方。 这使您可以检查批次并保持回滚日志的大小易于管理。
I find that batch updates are usually done with tools provided by the database vendor.
I agree that the DAOs that I've seen usually don't have methods for create/update/delete overloaded to take List, but there's no reason why they can't.
The one thought that brings me up short is that DAOs don't own transactions when I write them. They can never know where they're part of a larger unit of work. That's what services do.
My advice would be to leave the DAOs alone and let a separate service layer own the batch operations. Services own the transaction logic. It's also a good place to include logic for "chunking" a large update into pieces. That lets you checkpoint the batch and keep the size of your rollback logs manageable.
我认为这些例子并不经常使用它。 我的 DAO 具有访问、创建和更新单个实体和记录集合的方法。
I think the examples don´t use it often. My DAOs have methods to access, create and update both single entities and collections of records.
void update(List)
的行为应与void update(Item)
相同,只有List
会更新多个项目。 如果不是,那就应该是,除非有非常具体的理由不这样做。 如果它们做同样的事情,那么调用方法的开销就不是问题。例如,调用
update(Item)
1000 次与调用update(List)
1 次相比,它的作用与update(Item)
相同,仅调用一次1000 次意味着性能差异非常小。void update(List)
should act the same asvoid update(Item)
, only theList
one would update multiple items. If not, it should be unless there are very specific reasons why not to. The overhead of calling a method is not a concern if they do the same thing.For example, calling
update(Item)
1000 times vs callingupdate(List)
1 and it does the same asupdate(Item)
only a 1000 times means there will be very little performance difference.