我应该在哪里创建实体对象?

发布于 2024-08-08 02:41:12 字数 124 浏览 4 评论 0原文

我有一个实体类和一个实体 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌︼了一个春 2024-08-15 02:41:12

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.

顾冷 2024-08-15 02:41:12

我通常让 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.

绅刃 2024-08-15 02:41:12

我假设您正在谈论一个持久性实体以及管理该持久性的东西。在我看来,使用工厂来简单地创建 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, an EntityManager, whatever, to deal with the persistence. I think the key point is not to let the persistence strategy/implementation bleed past your business API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文