We have an aggregate of:
- entity:
Poll
(representing a question) - two or more value objects
Choice
Adding choices is done through Poll
, repository stores only the aggregate, i.e. everything is done as expected.
Now, I need to select specific Choice
that user chose. From the UI I will have only the choiceId
of selected choice. So would my Poll.voteForChoice()
method be:
voteForChoice(choiceId)
– where I pass the choice id to the aggregate root, so he iterates it’s choices and finds the target choice so to increment the count. However, we are using ID here for the choice, and choice is value object.voteForChoice(Choice)
– where we are sending the full value object, meaning I need to create it first before I do the voting. That also means I will need to have a Repo for choices, and they are value objects, so that clashes with all what I’ve learned 😉- or it should be:
Poll.getChoice(choiceId).vote()
. - or the
Choice
is actually an entity, so we would use Repository to fetch choice:ChoiceRepo.find(id).vote()
.
Simple case, but what is the correct answer in DDD world?
6
Depending on the collaborative degree of your application, you might find yourself in trouble if 2 or more users try to increment the same Choice
concurrently. That will happen whether it’s a VO or an Entity in the same Aggregate as the Poll
.
To me the natural consistency boundary implied by the domain action of voting includes the user. So I would suggest a full fledged PollChoice
Aggregate Root with a link to the User, or if there are multiple choices per poll, a PollResponse
AR containing all choices (which can then be VO’s) made by a particular user to a specific poll.
Edit : something along these lines
class PollResponse {
int pollId;
int userId;
Choice choice;
Datetime dateResponded;
}
3
I believe that a value object should be referred by its value because that’s what makes it unique, neither a GUID nor a local ID.
Consider a real world example
1) What web browser do you use at work?
- A) Chrome
- B) Firefox
- C) Internet Explorer
- D) Opera
- E) Safari
2) What web browser do you use at home?
- A) Chrome
- B) Firefox
- C) Internet Explorer
- D) Opera
- E) Safari
How would you vote in the real world? You would probably say something along the lines “For the first poll I vote for ‘Firefox'” or “For the second poll I choose ‘A'”. Hence, there are few ways you can go about modeling the voting behavior
// 1st poll (GUID = 1)
Poll poll1 = pollRepo.find(1);
// 2nd poll (GUID = 2)
Poll poll2 = pollRepo.find(2);
poll1.voteForChoice("Firefox");
poll2.voteForChoice("Chrome");
On the other hand, there’s nothing wrong in creating an instance of a value object outside the scope of its aggregate without using a repository
poll1.voteForChoice(new Choice("Firefox"));
poll2.voteForChoice(new Choice("Chrome"));
So, it seems like you can use the second option for the voteForChoice
method. The first one uses ID which suggests that Choice
s be modeled as entities. The third one feels wrong like a leaky abstraction and breach of encapsulation. The forth one is wrong because of using a repository for a value object.
UPDATE
Important point. There’s no a single recipe how to model your domain even if it’s a common case in the real world. You may design your Choice
s as value objects and have a Map<Choice, Counter> choices
property in your Poll
s. Or, you may design it as entities and have the counter as a part of a Choice
. Additionally, you might want to have a List<User> users
collection in the Choice
entity to keep a track of users who voted for a particular Choice
.
I know I’ve made it even more confusing for you, but the thing that I want to emphasize is that you have to decide for yourself what kind of domain model you need. My answer was based on the assumption derived from your question that value objects is what you need.
UPDATE 2
It turns out that according to Eric Evans when you tempted to create some sort of identifier for a value object even if it’s a local one you should consider making it an entity instead.
9