guide transferobject - rfreier/oasp4j GitHub Wiki
The technical data model is defined in form of persistent entities. However, passing persistent entities via call-by-reference across the entire application will soon cause problems:
-
Changes to a persistent entity are directly written back to the persistent store when the transaction is committed. When the entity is send across the application also changes tend to take place in multiple places endangering data sovereignty and leading to inconsistency.
-
You want to send and receive data via services across the network and have to define what section of your data is actually transferred. If you have relations in your technical model you quickly end up loading and transferring way too much data.
-
Modifications to your technical data model shall not automatically have impact on your external services causing incompatibilities.
To prevent such problems transfer-objects are used leading to a call-by-value model and decoupling changes to persistent entities.
For each persistent entity we create or generate a corresponding entity transfer object (ETO) that has the same properties except for relations. In order to centralize the properties (getters and setters with their javadoc) we use a common interface for the entity and its ETO.
If we need to pass an entity with its relation(s) we create a corresponding composite transfer object (CTO) that only contains other transfer-objects or collections of them. This pattern is illustrated by the following UML diagram from our sample application.
Finally, there are typically transfer-objects for data that is never persistent. A common example are search criteria objects (derived from SearchCriteriaTo in our sample application).
The logic layer defines these transfer-objects (ETOs, CTOs, etc.) and will only pass such objects instead of persistent entities.
If we need to do service versioning and support previous APIs or for external services with a different view on the data, we create separate transfer-objects to keep the service API stable (see service layer).