C
C#3w 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
Pobiega3w 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
Kuno3w 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
Pobiega3w 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
Kuno3w 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
Pobiega3w 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
Kuno3w 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
Pobiega3w ago
with projection: SELECT a,b,c FROM Tickets ... -> Stage3TicketDto without projection: SELECT * from Tickets ... -> TicketDomainObject -> Stage3TicketDto
Kuno
Kuno3w ago
Thanks for explaining and reference, I will go deep into it
Want results from more Discord servers?
Add your server
More Posts
cancellation token to stop Console.ReadLine is only working on the first tryI have the following method I got from stackoverflow ```cs async Task<string?> ReadLineAsync(Cancelllearning moq frameworkI'm trying to get my head around the moq framework but i can't seem to understand. Anyone got some g✅ .net best practices & useful patternsHi all, Looking for relevant material on the subject. Would appreciate your help. - main subjects mapping a dynamic object to an existing object and only map properties that exist in the source objI need help with patching an object in ASP Core, on my api i only send a partial data of the object when I want to enter to open my path, the click event is activatedthis is the code https://paste.ofcode.org/t3AqXpwPMuULScQt7vNkXF I do not know what to do✅ updated to Swashbuckle 6.6 from 6.4 and now my code doesn't compileI have this example from a book I'm reading about asp.net, it was working fine until I updated Swash✅ i feel like there is a really easy fix that i am missingwhy is this returning 0?✅ I might be stupid, but my score system adds two every time a collision happens instead of one.I can't figure out why. Then again, I don't know what I'm doing. I'm following a tutorial and this iplease help me understand this warninghttps://www.reddit.com/r/dotnet/s/5CSCmAlroV I have already posted the question on dotnet subreddit✅ enter the button,I need this thing very urgentlySo I have a form that has 3 buttons, each button sits in the groupbox a panel with various buttons t