TestName
TestName

Reputation: 388

The layer of DTO conversions

Controller should work with DTO, service with domain models. For communications between controller and service what is best place for conversion from DTO to models, in controller or service? And also, what is the best place for building DTO after executing service logic?

Upvotes: 0

Views: 137

Answers (1)

Pavel Smirnov
Pavel Smirnov

Reputation: 4799

The best place for conversion from DTO to Models and vice-versa is a Mapper.

You should not place the conversion logic in Service code, because a DTO can be used by multiple services. Placing it into Service code makes it tied to one specific service. Not a solution.

You should not place the conversion logic into a controller, because services, in order to convert something, become dependent on the controller. Not a solution.

Create a Mapper interface. Provide implementation for it (you can even use a library for mapping, like ModelMapper). And use this mapper for conversion whenever you need.

Upvotes: 1

Related Questions