C
C#6mo ago
Kuno

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 ?"
No description
8 Replies
Pobiega
Pobiega6mo ago
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 TicketTableStructure
but .. yeah, your model must contain all the relevant data assuming this is indeed just a singular domain entity
Kuno
KunoOP6mo ago
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"
Pobiega
Pobiega6mo ago
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?
Kuno
KunoOP6mo ago
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)
class TicketDAO
{
private $database;
public function __construct(DataBase $database)
{
$this->database = $database;
}

public function getById($id)
{
$query = "SELECT * FROM tickets WHERE id = " . $id;
return $this->database->selectRow($query);
}

public function getAll(): array
{
$query = "SELECT * FROM tickets";
$result = $this->database->select($query);
return $result;
}

public function createTicket(createTicketDTO $dto): int | bool
{
$query = "INSERT INTO tickets (serial_number, ticket_descr, status, isPacked, device_id, user_id, mileage)
VALUES ({?},{?},{?},{?},{?},{?},{?})";
$result = $this->database->query($query, array(
$dto->getSerialNumber(),
$dto->getProblemDescription(),
$dto->getStatus()->toString(),
$dto->getCompleteness(),
$dto->getDeviceId(),
$dto->getUserId(),
$dto->getMileage()
));
return $result;
}
class TicketDAO
{
private $database;
public function __construct(DataBase $database)
{
$this->database = $database;
}

public function getById($id)
{
$query = "SELECT * FROM tickets WHERE id = " . $id;
return $this->database->selectRow($query);
}

public function getAll(): array
{
$query = "SELECT * FROM tickets";
$result = $this->database->select($query);
return $result;
}

public function createTicket(createTicketDTO $dto): int | bool
{
$query = "INSERT INTO tickets (serial_number, ticket_descr, status, isPacked, device_id, user_id, mileage)
VALUES ({?},{?},{?},{?},{?},{?},{?})";
$result = $this->database->query($query, array(
$dto->getSerialNumber(),
$dto->getProblemDescription(),
$dto->getStatus()->toString(),
$dto->getCompleteness(),
$dto->getDeviceId(),
$dto->getUserId(),
$dto->getMileage()
));
return $result;
}
Pobiega
Pobiega6mo ago
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 entities
Kuno
KunoOP6mo ago
I'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" ?
Pobiega
Pobiega6mo ago
with projection: SELECT a,b,c FROM Tickets ... -> Stage3TicketDto without projection: SELECT * from Tickets ... -> TicketDomainObject -> Stage3TicketDto
Kuno
KunoOP6mo ago
Thanks for explaining and reference, I will go deep into it
Want results from more Discord servers?
Add your server