Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general <strong>Value Objects</strong> should be Immutable. Like <em>Integer</em> or <em>String</em> objects in Java. We can use them for transferring data between software layers. If the software layers or services running in different remote nodes like in a microservices environment or in a legacy Java Enterprise App. We must make almost exact copies of two classes. This is the where we met DTOs.</p> <pre><code>|-----------| |--------------| | SERVICE 1 |--&gt; Credentials DTO &gt;--------&gt; Credentials DTO &gt;-- | AUTH SERVICE | |-----------| |--------------| </code></pre> <p>In legacy Java Enterprise Systems DTOs can have various EJB stuff in it.</p> <p>I do not know this is a best practice or not but I personally use <strong>Value Objects</strong> in my Spring MVC/Boot Projects like this:</p> <pre><code> |------------| |------------------| |------------| -&gt; Form | | -&gt; Form | | -&gt; Entity | | | Controller | | Service / Facade | | Repository | &lt;- View | | &lt;- View | | &lt;- Entity / Projection View | | |------------| |------------------| |------------| </code></pre> <p><strong>Controller</strong> layer doesn't know what are the entities are. It communicates with <strong>Form</strong> and <strong>View Value Objects</strong>. Form Objects has JSR 303 Validation annotations (for instance @NotNull) and <strong>View Value Objects</strong> have Jackson Annotations for custom serialization. (for instance @JsonIgnore)</p> <p>Service layer communicates with repository layer via using Entity Objects. Entity objects have JPA/Hibernate/Spring Data annotations on it. Every layer communicates with only the lower layer. The inter-layer communication is prohibited because of circular/cyclic dependency.</p> <pre><code>User Service ----&gt; XX CANNOT CALL XX ----&gt; Order Service </code></pre> <p>Some <strong>ORM</strong> Frameworks have the ability of projection via using additional interfaces or classes. So repositories can return View objects directly. There for you do not need an additional transformation.</p> <p>For instance this is our User entity:</p> <pre class="lang-java prettyprint-override"><code>@Entity public final class User { private String id; private String firstname; private String lastname; private String phone; private String fax; private String address; // Accessors ... } </code></pre> <p>But you should return a Paginated list of users that just include id, firstname, lastname. Then you can create a View Value Object for ORM projection.</p> <pre class="lang-java prettyprint-override"><code>public final class UserListItemView { private String id; private String firstname; private String lastname; // Accessors ... } </code></pre> <p>You can easily get the paginated result from repository layer. Thanks to spring you can also use just interfaces for projections.</p> <pre class="lang-java prettyprint-override"><code>List&lt;UserListItemView&gt; find(Pageable pageable); </code></pre> <p>Don't worry for other conversion operations <code>BeanUtils.copy</code> method works just fine.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload