Converting from DTO to DomainModel
Hello, I'm developing the CRM for repairing the office equipment
There are 7 stages of each ticket, each stage has its own DTO. And if to accumulate all fields from DTOs I'll get TicketTableStructure (check out the image)
The problem is "If there are a lot of different DTOs and every DTO has to be converted into DomainModel, it means, that the DomainModel has to be able to work with any data from DTO => DomainModel must duplicate TicketTableStructure ?"
8 Replies
your domain model must be able to contain all the data for all the stages of the same entity
each DTO responds to a particular stage, and thus each DTO is a subset of the data from the domain model, potentially even a different representation of that same data
Im not sure what you mean by
DomainModel must duplicate TicketTableStructurebut .. yeah, your model must contain all the relevant data assuming this is indeed just a singular domain entity
Okay, i got it, but there is another problem:
"This DomainModel is truly heavy. For example, if I need to getTicketById(), and for mapping to any DTO I have to get entire DomainModel. However, I have several DTOs, that uses literally 3-4 fields from DomainModel"
That doesnt need to be a problem
the trick is just to not fetch the entire domain model all the time, using a projection
how are you accessing the database? EF Core? Dapper? are you using any mapper, like Mapster or Automapper or Mapperly?
I have a separate TicketDAO class, that uses library for working with MySQL, and I wrote something like this (but all of that in php)
just don't select
*
then
thats what a projection would do
the domain model is and has to be the full thing, with all the data
thats why we recommend returning projections from your database queries, where you actually skip the domain model stage
otherwise you do have to deal with feching very large entitiesI've never used projections, just don't know what is that.
I have one more little question: "When query executed and data recieved, should I map it to TicketDomain and then TickeDomain to *DTO" ?
with projection:
SELECT a,b,c FROM Tickets ...
-> Stage3TicketDto
without projection: SELECT * from Tickets ...
-> TicketDomainObject -> Stage3TicketDtoThanks for explaining and reference, I will go deep into it