I am a backend developer doing server development using nestJs. I have a simple but perhaps difficult question.
This question arose from the following stackoverflow answer. stackOverflow
If the reason for not merging DTO is the following reasons
- Separated DTOs can require more code, but are easier to test.
- Interfaces can change and evolve independently.
- From a data access point of view, merging DTOs does not expose undesirable DTOs or provide restricted access.
Is it okay to create one unused response DTO that contains most of the entity's properties and create each actual response DTO using netsjs/Swagger's Pick Type? nestJS_pickType
EX)
- DTO that contains most of the entity's properties
export class BookResponseDto {
@ApiProperty({
description: 'book title',
})
title: string;
@ApiProperty({
description: 'author',
})
author: string;
@ApiProperty({
description: 'create date',
})
createDate: Date;
@ApiProperty({
description: 'sale rate',
})
sellRate: number;
@ApiProperty({
description: 'author country code',
})
authorCountry: string;
}
Since this is not actually used, there is no effect other than entity change, and if the entity is changed (when only Pick type is written in each response DTO), each individual response DTO corresponding to the change should be modified.
- each actual response DTO using netsjs/Swagger's Pick Type
export class GetBookAuthorResponseDto extends PickType(BookResponseDto, [
'author',
'authorCountry',
] as const) {
// can add aditional property, also can fix exist property
@ApiProperty({
description: 'author age',
})
authorAge: number;
}
if you want to authorCountry property type string to CountryCode enum that saves all countryCode, you can erase 'authorCountry' in PickType's array parameter and then plus property like authorAge
This seems to make it possible to reuse long and infrequently changing code, such as ApiProperty. I'm curious about your opinion. thank you