我应该在哪里创建实体对象?
我有一个实体类和一个实体 DAO 类。
是否应该由 DAO 类负责创建实体类的实例,或者是否应该有一个实体创建者/管理器类仅使用 DAO 类从数据库获取数据来创建实体类。
谢谢,
克里斯
I have an entity class and an entity DAO class.
Should it be the responsibility of the DAO class to create instances of the entity class, or should there be an entity creator/manager class that uses the DAO class only to get the data from the database to create the entity class.
Thanks,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DAO 应该负责从数据存储中加载持久对象并返回瞬态实例。为什么要在这里添加另一层抽象?
为了创建新的实体,可能会涉及工厂(或汇编器)。然而,通常只有当实体创建足够复杂时,这才是合理的。在大多数情况下,一个简单的构造函数就可以满足要求。
It should be the responsibility of the DAO to load a persistent object from the datastore and returning a transient instance. Why add another layer of abstraction here?
For creating new Entities, a Factory (or Assembler) might be involved. However, usually this is only justified when entity creation is complex enough. A simple constructor fits the bill just fine in most cases.
我通常让 DAO 了解实体组装并返回一个完全水合的实体。为什么?因为,通常 DAO 的存在只是为了支持该实体。如果其角色并不局限于支持该实体或相关实体,那么您可能需要查看中间层。
I usually let the DAO know about the entity assembly and return a fully hydrated entity. Why? Because, usually the DAO only exists to support that entity. If its role is isn't bound to supporting that entity or related entities, then you may want to look at an intermediate layer.
我假设您正在谈论一个持久性实体以及管理该持久性的东西。在我看来,使用工厂来简单地创建 POJO 没有任何价值。使用传统方法,然后使用 DAO、EntityManager 等来处理持久性。我认为关键点是不要让持久性策略/实现渗透到您的业务 API 中。
I'm assuming you're talking about a persistent entity and something that manages that persistence. In my opinion, there is no value in using a factory to simply create the POJO. Use conventional means and then use a
DAO
, anEntityManager
, whatever, to deal with the persistence. I think the key point is not to let the persistence strategy/implementation bleed past your business API.