DDD: How to refer/select a value object inside aggregate?

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 Choices 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 Choices as value objects and have a Map<Choice, Counter> choices property in your Polls. 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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật