|
9 | 9 | --- |
10 | 10 |
|
11 | 11 | ## Intent |
12 | | -Pass data with multiple attributes in one shot from client to server, to avoid multiple calls to remote server. |
| 12 | + |
| 13 | +Pass data with multiple attributes in one shot from client to server, to avoid multiple calls to |
| 14 | +remote server. |
13 | 15 |
|
14 | 16 | ## Explanation |
15 | 17 |
|
16 | 18 | Real world example |
17 | 19 |
|
18 | | -> We need to fetch information about customers from remote database. Instead of querying the attributes one at a time, we use DTOs to transfer all the relevant attributes in a single shot. |
| 20 | +> We need to fetch information about customers from remote database. Instead of querying the |
| 21 | +> attributes one at a time, we use DTOs to transfer all the relevant attributes in a single shot. |
19 | 22 |
|
20 | 23 | In plain words |
21 | 24 |
|
22 | 25 | > Using DTO relevant information can be fetched with a single backend query. |
23 | 26 |
|
24 | 27 | Wikipedia says |
25 | 28 |
|
26 | | -> In the field of programming a data transfer object (DTO) is an object that carries data between processes. The |
27 | | -motivation for its use is that communication between processes is usually done resorting to remote interfaces |
28 | | -(e.g., web services), where each call is an expensive operation. Because the majority of the cost of each call is |
29 | | -related to the round-trip time between the client and the server, one way of reducing the number of calls is to use an |
30 | | -object (the DTO) that aggregates the data that would have been transferred by the several calls, but that is served by |
31 | | -one call only. |
| 29 | +> In the field of programming a data transfer object (DTO) is an object that carries data between |
| 30 | +> processes. The motivation for its use is that communication between processes is usually done |
| 31 | +> resorting to remote interfaces (e.g. web services), where each call is an expensive operation. |
| 32 | +> Because the majority of the cost of each call is related to the round-trip time between the client |
| 33 | +> and the server, one way of reducing the number of calls is to use an object (the DTO) that |
| 34 | +> aggregates the data that would have been transferred by the several calls, but that is served by |
| 35 | +> one call only. |
32 | 36 |
|
33 | 37 | **Programmatic Example** |
34 | 38 |
|
35 | | -Let's first introduce our simple customer DTO class. |
| 39 | +Let's first introduce our simple `CustomerDTO` class. |
36 | 40 |
|
37 | 41 | ```java |
38 | 42 | public class CustomerDto { |
@@ -60,7 +64,7 @@ public class CustomerDto { |
60 | 64 | } |
61 | 65 | ``` |
62 | 66 |
|
63 | | -Customer resource class acts as the server for customer information. |
| 67 | +`CustomerResource` class acts as the server for customer information. |
64 | 68 |
|
65 | 69 | ```java |
66 | 70 | public class CustomerResource { |
@@ -94,10 +98,12 @@ Now fetching customer information is easy since we have the DTOs. |
94 | 98 | ``` |
95 | 99 |
|
96 | 100 | ## Class diagram |
| 101 | + |
97 | 102 |  |
98 | 103 |
|
99 | 104 | ## Applicability |
100 | | -Use the Data Transfer Object pattern when |
| 105 | + |
| 106 | +Use the Data Transfer Object pattern when: |
101 | 107 |
|
102 | 108 | * The client is asking for multiple information. And the information is related. |
103 | 109 | * When you want to boost the performance to get resources. |
|
0 commit comments