Note: refers mostly to the development of HTTP APIs and web development in general.
In C# and Java, we have DTO pattern, in layman's terms, it's just a class that's "domain-specific" sometimes also called "view model" (sent over a network). So for example, our DTO for login would be something like LoginParams
with username and password (only).
Or let's say CreateUserRequest
that contains properties needed only for that specific operation(to create a user we don't need id for example), and a separate DTO for a response (CreateUserResponse
or just User
).
It's also used to hide certain properties from being serialized or because API model differs too much from our domain or data model.
So consider a model User
with property like hashed_password
, obviously, we don't want to return hashed_password
to the client of our HTTP API, because it's something that only server should know about. So for that purpose, we have a separate class UserDto
or UserResponseModel
with properties we want to send to the API user.
Automapper and similar libraries are commonly used to simplify this process by mapping one object to another for us.
I've read somewhere that this pattern in Golang isn't present and it's generally wrong to apply C# and Java patterns to language like Go (??).
If that is true how do you separate your data models and/or domain models from your API models?