DDD / Good way to update an item of a collection of value objects

277 views
Skip to first unread message

Michael Azerhad

unread,
Apr 10, 2018, 8:20:22 PM4/10/18
to DDD/CQRS
Hi,

Let's suppose a Meeting entity, on which I can define some speakers. 

For instance, the Meeting "Budget 2018" would have Thomas LORD and Roberto Smith as speakers. 

I thought about considering speakers as Value Objects, since they don't have any lifecycle; just acting as values.
So Meeting Entity would keep a collection of "Speakers" Value Objects (firstName/lastName).

Suppose this use case triggering from a Web UI: 

I want to detach/remove a speaker from a meeting.  

As speakers being value objects, should the action (Rest API call for instance) bring the whole remaining list of attached speakers to the server, so that the speakers collection inside Meeting Entity is fully replaced ? 

Indeed, I learnt that a typical pro of Value Object is the ability to be replaced.

Or should I only send to the server the firstName/lastName of the speaker expected to be detached, so that the server can select the right item in collection using structural equality  and then remove it ? 

What is a good practice in this case? 




Rickard Öberg

unread,
Apr 11, 2018, 12:24:00 AM4/11/18
to ddd...@googlegroups.com
Hi,

Great question! I typically do one of these 3:
1) give the value object an uuid so that it's easier to manipulate
(add,remove,move position in list)
2) if the list is the result of a computation (in my case, list of
latest videos in a category), I let the command carry the full list
and do a replace
3) let command carry all values to do add/remove/move.

Whenever I do 3) I usually regret it, because awkward. So my go to is
1), with a contextual usage of 2). The value object effectively
becomes an entity, but that's fine. It's all encapsulated within the
aggregate anyway.

/Rickard
> --
> You received this message because you are subscribed to the Google Groups
> "DDD/CQRS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to dddcqrs+u...@googlegroups.com.
> Visit this group at https://groups.google.com/group/dddcqrs.
> For more options, visit https://groups.google.com/d/optout.

Michael Azerhad

unread,
Apr 11, 2018, 2:05:26 AM4/11/18
to DDD/CQRS
What is the difference between your 2) and 3) since it seems that both deal with the command carrying all values... 

May you have a brief example of 3) ?

Thanks :)  

Rickard Öberg

unread,
Apr 11, 2018, 2:21:15 AM4/11/18
to ddd...@googlegroups.com
Hey,

2) would be a command of type "set list containing these values",
whereas 3) needs commands add/remove/move for list manipulation.
Without a generated UUID for each entry each add/remove/move command
needs to have all fields of the value, or if you can let it have one
field which is the value itself that's fine too. Just more complicated
to serialize/deserialize to a form, as in REST all interactions are
done by submitting forms.

Example of 3) in my codebase might be:
public class AddSpeaker
extends Command
{
public String given_name;
public String family_name;
}
public class MoveSpeaker
extends
{
public String given_name;
public String family_name;
public int position;
}
public class RemoveSpeaker
extends Command
{
public String given_name;
public String family_name;
}
whereas 2) might be:
public class SetSpeakers
extends Command
{
public List<SpeakerModel> speakers;
}

with 1) it becomes:
public class AddSpeaker
extends Command
{
public String given_name;
public String family_name;
}
public class MoveSpeaker
extends
{
public String speaker_id;
public int position;
}
public class RemoveSpeaker
extends Command
{
public String speaker_id;
}

/Rickard

Michael Azerhad

unread,
Apr 11, 2018, 2:37:29 AM4/11/18
to DDD/CQRS
Very clear Rickard!
Thanks a lot for your answers :)
Reply all
Reply to author
Forward
0 new messages