vendredi 7 décembre 2018

Data transfer objects (DTO) pattern in Go

Note: refers mostly to the development of HTTP APIs and web development in general.

In C# and Java, we have DTO pattern, in layman terms, it's just a class that's "boundary specific" sometimes also called "view model". 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, and a separate DTO for a response (CreateUserResponse).

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.

If that is true how do you separate your data models and/or domain models from your API models?

Aucun commentaire:

Enregistrer un commentaire