First layer - translations layer

What for?

DTO (Data Transfer Object) layer is used for e.g.:

  • currently logged in user

FetchUserResponse -> new UserDto(response) -> UserFactory.fromUserDto(dto)

* factory from domain layer
  • list of users

FetchUserListResponse -> new UserListDto(response) -> dto.users.map(user => new UserListItem(user))

* UserListItem from domain layer
👾

REMBER don't use same model for 2 things. User isn't UserListItem.

DTO

DTO - Data Transfer Object - it is a class that is resposible for tranlsating incoming responses to our models.

Example DTOs: find-users.dto.ts, user-profile.dto.ts, user.dto.ts.

ℹ️

DTO/Translations layer is responsible for managing incoming data shape and encapsulating changes.

class UserDto {
    id: UserID
    username: string
    address: AddressDto
 
    constructor(response: FetchUserResponse) {
      this.id = response.id
      this.username = response.user_name
      this.address = AddressDto.fromFetchUserResponse(response)
    }
 
    static fromResponse(res: FetchUserResponse): UserDto {
      return new UserDto(res)
    }
}

Types

Types in translation layer are:

  • responses
  • inner types, like UserID
type UserID = string
 
type FetchUserResponse = {
  id: UserID
  username: string
  address: AddressResponseObject
}
 
type AddressResponseObject = {
  street: string;
  city: string;
}

Example

Example common feature: users

      • user-id.ts
      • fetch-user-response.ts
    • user.dto.ts
    • address.dto.ts
  • class UserDto {
      id: UserID;
      username: string;
      address: AddressDto;
     
      constructor(response: FetchUserResponse) {
        this.id = response.id;
        this.username = response.user_name;
        this.address = AddressDto.fromFetchUserResponse(response);
      }
     
      static fromResponse(res: FetchUserResponse): UserDto {
        return new UserDto(res);
      }
    }