#Dto for data between services?

6 messages · Page 1 of 1 (latest)

wide rover
#

I have a couple services for building an invoice that are only used by other internal services. I was wondering if i should use Dto for those service methods or just typescript types?

rocky imp
#

Sounds like they are filling the role of a domain service so they generally return domain entities. What is the difference between a dto and ts types for you?

wide rover
# rocky imp Sounds like they are filling the role of a domain service so they generally retu...

So say for example I have a controller/service with path /invoices. That service to create a invoice will ofcourse take in a dto. But now that invoice service needs to call a method like 'generate line item' in another line item service. So that generate line item needs to know like the item id for example. Now my confusion is should I use a dto for that data being passed in the line item service method or use a dto?

#

For me the difference between dto and ts types i guess would be dto can be validated with class-validator which is more capable then ts types because it can check things like making sure a string is a date. Not sure what other difference it would have. I am asking because a dto is def a heavier overhead and going to slow me down if I am writing dto for every service method.

Just wanted to ask you guys on what was standard for most of you. Not trying to shoot myself in the foot by choosing one or the other without understanding all possible impacts from both approaches

rocky imp
#

Whether you pass it as a plain object or a class instance doesn't matter therefore a type and or a class can be both considered a DTO. (Data Transfer Object). As mentioned earlier, your application should translate the DTO into a Domain(or database) entity and work with that instead. The use of DTOs is to create a layer of indirection by protecting against changes, and there is not enough reason to protect against changes between application services to a given extent. I attached a diagram that shows 1 way how to do it

#

It really depends on the amount of "protection" you want against changes bubbling up and causing issues for the consumer