Search code examples
c#domain-driven-designdata-transfer-objects

Tracking changes from data transfer object and apply them to domain entity (ddd)


I have for example this data transfer object that carries data from presentation layer:

public class CompanyDTO
{
    public int Id { get; set; }
    public string Name { get; set; } 
    ...
    ...
    ...
    ...
    public string BlaBla { get; set; }       
}

And the corresponding domain entity:

public class Company
{
    public int Id { get; private set; }
    public string Name { get; private set; } 
    ...
    ...
    ...
    ...
    public string BlaBla { get; private set; }   

    public void ChangeName(string newName)
    {
        //business logic here

        Name = newName;
    }    
}

There is business logic when changing the Name property of the Company class, so i can't just map the properties of the dto to my domain entity without caring what has changed.

The same situation is present for other properties of the entity (e.g. collections that contain other entities that may have changes etc.)

So the question is, how do i track that a property has changed in dto, so i could apply the appropriate methods to do what has to be done?

Is there a better way than iterating through all these properties and comparing them to my domain entity?


Solution

  • You either make sure intent is already obvious when the command comes from the UI a la RenameCompanyCommand or you try to reverse engineer the client's intent by comparing the DTO with the current state of the domain object.