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.
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:
Are there any more valid reasons not to move logic into my entities? Or any other concerns to take into account?
Entity objects encapsulate business logic The best place to write your business logic is in entity objects, because they consistently enforce business logic for all views of data, accessed through any type of client interface.
Short answer: Entities may be part of a business domain. Thus, they can implement behavior and be applied to different use cases within the domain. DTOs are used only to transfer data from one process or context to another.
An entity is a lightweight persistence domain object. Typically, an entity represents a table in a relational database, and each entity instance corresponds to a row in that table. The primary programming artifact of an entity is the entity class, although entities can use helper classes.
Some reasons for using entities are: When the key is a property of an entity object representing the record as a whole, the object's identity and concept are often clearer than with key and value objects that are disjoint. A single entity object per record is often more convenient to use than two objects.
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.
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?
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.
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.
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.
EDIT
This question has been discussed many time and will probably continue to be discussed as there is no definitive answer. A few interesting links:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With