| name | enforcing-dto-record |
| description | Force all DTO (Data Transfer Object) types to be declared as record instead of class for immutability and value-based equality. |
DTO๋ record๋ก ๊ตฌํ ๊ฐ์
์ค๋ช
DTO(Data Transfer Object)๋ ์ฝ๊ธฐ ์ ์ฉ(ReadOnly) ๊ฐ์ฒด์
๋๋ค. C#์์ class๊ฐ ์๋ record๋ก ๊ตฌํํด์ผ ํฉ๋๋ค.
record๋ ๋ถ๋ณ์ฑ(immutability), ๊ฐ ๊ธฐ๋ฐ ๋๋ฑ์ฑ(value equality), with ์์ ๊ธฐ๋ณธ ์ ๊ณตํ๋ฏ๋ก DTO์ ์ต์ ์
๋๋ค.
๊ท์น
- ๋ชจ๋ DTO ํด๋์ค๋
class ๋์ record๋ก ์ ์ธํฉ๋๋ค
- ์๋ต/์์ฒญ ์ ์ฉ ๊ฐ์ฒด, ์ง๋ ฌํ/์ญ์ง๋ ฌํ ์ ์ฉ ๊ฐ์ฒด๊ฐ ๋์์
๋๋ค
- ์ํฐํฐ(Entity)๋ ์ํ ๋ณ๊ฒฝ์ด ํ์ํ ๋๋ฉ์ธ ๋ชจ๋ธ์ ์ ์ธํฉ๋๋ค
sealed class + { get; set; } ํจํด์ record + ์์น ๋งค๊ฐ๋ณ์(positional parameter) ๋๋ { get; init; } ํจํด์ผ๋ก ๋ณํํฉ๋๋ค
Worst Case (๋์ ์)
private sealed class PagedResponse
{
public int Page { get; set; }
public int PageSize { get; set; }
public int TotalCount { get; set; }
public int TotalPages { get; set; }
public EmployeeDto[] Data { get; set; } = [];
}
private sealed class CreatedResponse
{
public int Count { get; set; }
public EmployeeDto[] Data { get; set; } = [];
}
private sealed class EmployeeDto
{
public string Name { get; set; } = "";
public string Email { get; set; } = "";
public string Tel { get; set; } = "";
public DateTime Joined { get; set; }
}
private sealed class JsonEmployeeDto
{
public string? Name { get; set; }
public string? Email { get; set; }
public string? Tel { get; set; }
public string? Joined { get; set; }
}
Best Case (์ข์ ์)
private sealed record PagedResponse(
int Page,
int PageSize,
int TotalCount,
int TotalPages,
EmployeeDto[] Data);
private sealed record CreatedResponse(
int Count,
EmployeeDto[] Data);
private sealed record EmployeeDto(
string Name,
string Email,
string Tel,
DateTime Joined);
private sealed record JsonEmployeeDto(
string? Name,
string? Email,
string? Tel,
string? Joined);
์ ์ฉ ๋์
- API ์๋ต/์์ฒญ DTO
- JSON ์ง๋ ฌํ/์ญ์ง๋ ฌํ ๋งคํ ๊ฐ์ฒด
- ํ
์คํธ ์ฝ๋์ ์ญ์ง๋ ฌํ DTO
- ๋ด๋ถ ๋ฐ์ดํฐ ์ ๋ฌ์ฉ ๊ฐ์ฒด