Data Transfer Objects (DTOs)

Data Transfer Object (DTO) is a simple design pattern for transporting necessary data between different parts of an application. This could be between the client and server.

The primary role of a DTO is to act as a data container. It contains a set of attributes (fields) but no business logic.

Why We Use DTOs

  • Encapsulation of Data: DTOs allow exposing only the necessary data to clients or consumers, hiding internal domain logic or sensitive fields. This protects the core models from unintended exposure.

  • Decoupling: DTOs Creates a seamless relationship between your domain models and external systems (e.g., APIs, UI). This decoupling makes it easier to evolve your domain without breaking external contracts. Your domain model can change, but as long as the DTO stays consistent, your API remains stable.
  • Reduces Overhead: DTOs allow bundling multiple pieces of data into a single object, which significantly reduces the number of calls required over a network or between application layers.
  • Data Masking/Aggregation: DTOs let you filter or aggregate data from one or more sources, ensuring that a layer or process only receives the exact data it needs—no more, no less.

In summary, DTOs are lightweight data bags that simplify communication, improve performance, and maintain a cleaner separation between your application's layers.

 

 CodeStencil provides an intelligent approach to the implementation of DTOs that eliminates manual implementation of DTOs. You can access the DTO user interface from: SCHEMA | Data Transfer Objects (DTOs) or with the hotkey CTRL+SHIFT+D.

 

  1. Tables - This is a list from your Schema Dictionary containing the Tables and Views in your project.
  2. DTOs - These are the specific classes you have created to be used in this project. Note: During Schema Import, these are automatically generated and named in the format: TABLE NAME+"Dto".
  3. Table Columns - These are all the columns pulled from the Schema Dictionary based on the selected table in (1) above. You can select and add these columns to DTO Columns ( See 4). You will notice the column - "ArtistName" displayed in a different color, see DTO Lookup Columns
  4. DTO Columns - These are the columns that will be displayed on your grid/page. Note: Most stencils are designed to exclude columns that are unchecked from being displayed if the pattern you are using is based on DTOs. For less complicated patterns, the Schema Dictionary will control the columns that are displayed on grids/pages.
  5. Column Sequence - Just like (4) above, you can order the sequence in which the columns are displayed using these arrows to move the columns around.

DTO Lookup Columns

When you have a foreign key in a table;

 

You typically want to display a column from the related table instead of the foreign key, which is the ArtistId in the image above. CodeStencil allows you to pre-select the column you want to use, called a Table Label in CodeStencil terminology. If you want your lookup column to produce a result like this:

Here are the steps to take:

  1. Table - Select the Foreign Key Table. In this case, the table Artist has a Foreign Key column in the Album table.
  2. Table Label - We have selected the "Name" column to use as the Table Label. Check the "Set As Table Label" checkbox to specify this column as the table label.
  3. Mapped Name - Create a mapped name. Usually in the format - TABLE NAME + COLUMN NAME ( No Spaces).

With all these conditions met, you will automatically see the lookup column appear under the list of columns that can be added.

  1. Related Table - This is the Foreign Key Table
  2. Name - This is the Mapped Name specified in the Table Label column.

Note: As part of the Schema Import process, the Table Label, Mapped Name, and DTO structure will be automatically generated for all tables as they are imported, so you do not need to do steps 1-3.

 

 

 

 

 


Was this article helpful?