企业 Java 实体应该是愚蠢的吗? [英] Should Enterprise Java entities be dumb?

查看:23
本文介绍了企业 Java 实体应该是愚蠢的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们遗留的 Java EE 应用程序中,有很多值对象 (VO) 类,它们通常只包含 getter 和 setter,可能是 equals()hashCode().这些(通常)是要保存在持久性存储中的实体.(作为记录,我们的应用程序没有 EJB - 尽管可能在未来发生变化 - 我们使用 Hibernate 来持久化我们的实体.)在 VO 中操作数据的所有业务逻辑都是独立的类(不是 EJB,只是 POJO).我的 OO 心态讨厌这个,因为我相信给定类上的操作应该驻留在同一个类中.所以我有一种重构的冲动,将逻辑移到相关的 VO 中.

In our legacy Java EE application, there are loads of value object (VO) classes which typically contain only getters and setters, maybe equals() and hashCode(). These are (typically) the entities to be saved in persistence storage. (For the record, our app has no EJBs - although that might change in the future -, and we use Hibernate for persisting our entities.) All the business logic to manipulate the data in VOs is in separate classes (not EJBs, just POJOs). My OO mindset hates this, as I do believe that the operations on a given class should reside in that same class. So I have an urge to refactor to move logic into the related VOs.

我刚刚与一位在 Java EE 方面比我更有经验的同事进行了讨论,他确认哑实体至少曾经是推荐的方法.然而,他最近也读到了质疑这一立场有效性的意见.

I just had a discussion with a co-worker who is much more experienced in Java EE than me, and he confirmed that dumb entities at least used to be the recommended way to go. However, he has also read opinions recently which question the validity of this stance.

我知道有些问题至少限制了可以放入实体类的内容:

I understand that there are issues which at least limit what can be put inside an entity class:

  • 它不应该直接依赖于数据层(例如,查询代码应该进入单独的 DAO)
  • 如果它直接暴露给更高层或客户端(例如通过 SOAP),它的接口可能需要受到限制

是否有更有效的理由将逻辑移到我的实体中?或者还有其他需要考虑的问题?

Are there any more valid reasons not to move logic into my entities? Or any other concerns to take into account?

推荐答案

DTOVO 应该用于传输数据而不是嵌入逻辑.业务对象另一方面应该嵌入一些逻辑.我说一些,因为在您放入协调涉及多个业务对象的逻辑的服务中的内容与放入业务对象本身的内容之间总是要找到平衡.业务对象中的典型逻辑可以是验证、字段计算或其他一次仅影响一个业务对象的操作.

The DTO and VO are supposed to be used to transfer data and don't embed logic. The business objects on the other hand are supposed to embed some logic. I say some, because there is always a balance to find between what you put in services which coordinate logic involving several business objects and what you put in the business objects themselves. Typical logic in the business objects can be validation, field computation, or other operation that impact only one business object at a time.

请注意,到目前为止我还没有提到术语实体.持久性实体随着 ORM 流行起来,现在我们尝试同时使用持久性实体作为 DTO 业务对象.也就是说,实体本身在层与层之间流动,并包含一些逻辑.

Note that I haven't mentioned the term entity so far. Persistent entities were popularized with ORM and we nowadays try to use persistent entities both as DTO and business object at the same time. That is, the entity themselves flow between layers and tiers, and contain some logic.

还有什么更正当的理由不将逻辑移到我的实体中?或任何其他需要考虑的问题?

Are there any more valid reasons not to move logic into my entities? Or any other concerns to take into account?

正如您所指出的,这完全取决于依赖关系和您公开的内容.只要实体是愚蠢的(接近 DTO),它们就可以很容易地隔离在一个专用的 jar 中,用作层的 API.您在实体中放入的逻辑越多,就越难做到这一点.注意您公开的内容和依赖的内容(加载类,客户端也需要具有依赖类).这适用于异常、继承层次结构等.

As you pointed out, it's all a matter of dependencies and what you expose. As long as the entities are dumb (close to DTO) they can be isolated in a dedicated jar easily that serves as API of the layer. The more logic you put in the entities, the harder it becomes to do that. Pay attention to what you expose and what you depend on (the load the class, the client will need to have the depend class as well). This applies to exceptions, inheritance hierarchy, etc.

举个例子,我有一个项目,其中实体有一个在业务层使用的方法 toXml(...).因此,实体的客户端依赖于 XML.

Just to give an example, I had a project where the entities had a method toXml(...) used in the business layer. As a consequence, client of the entities depended on XML.

但是如果你不太在意层,以及API和实现之间的严格分离,我认为在实体中移动一些逻辑是好的.

But if you don't care too much about layers, and strict separation between API and implementation, I think it's good to move some logic in the entities.

编辑

这个问题已经讨论过很多次了,可能还会继续讨论,因为没有明确的答案.一些有趣的链接:

This question has been discussed many time and will probably continue to be discussed as there is no definitive answer. A few interesting links:

这篇关于企业 Java 实体应该是愚蠢的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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