2

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?

Vagelis Ouranos
  • 482
  • 3
  • 14

2 Answers2

3

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.

JefClaes
  • 3,275
  • 21
  • 23
  • So iterating through all these properties and comparing them to my domain entity. This is a solution i have already thought of, but the question is, is it applicable in real life cases? – Vagelis Ouranos Sep 17 '14 at 11:36
  • 1
    I would prefer to capture intent in the UI. If there really is business logic attached to renaming a company, it would make sense for users to have an explicit concept for this operation. – JefClaes Sep 17 '14 at 11:44
  • Unfortunately this is not an option. Any other thoughts on comparing the DTO with the current state of the domain object? – Vagelis Ouranos Sep 17 '14 at 12:11
  • If you think about it, any other approach basically boils down to comparing the DTO to the current state in some way or another... – Alexander Langer Sep 17 '14 at 19:34
1

I am not sure if this fits you but it seems that instead of

public string Name { get; private set; } 

public void ChangeName(string newName) 
{
    ...

you could have

private string _name;
public string Name 
{
    get
    {
        return _name;
    }
    set
    {
        if ( value != _name )
        {
           //business logic here
        }
        _name = value;
    }
} 

As you can see, rather than having a separate method (ChangeName) that you call upon changing the value of the property, you have your business logic directly in the setter of the property. The logic is called only if a new value assigned to the property differs from the previous value.

This way when a simple mapping between DTO and your business entity is performed, the business logic will always be executed.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • Thanks for your answer. I wouldn't do that this way and this is pretty much why: http://stackoverflow.com/questions/8291116/ddd-and-the-use-of-getters-and-setters Any alternatives? – Vagelis Ouranos Sep 17 '14 at 07:43
  • I wouldn't put too much logic into setters. I'd rather put `if (value != _name) { //business logic here }` into `ChangeName` method. – Ilya Palkin Sep 17 '14 at 08:01