Search code examples
domain-driven-designddd-repositoriesvalue-objects

Domain driven design value object, how to ensure a unique value


I am building a questionnaire creator. A questionnaire consists of sections, sections consist of pages and pages consist of questions. Questionnaire is the aggregate root.

Sections, pages and questions can have what are called shortcodes which should be unique within a questionnaire (but not unique within the database hence they are not strictly an identity). I intended to make the shortcode a value object and wanted to include the business rule that it should be unique within the questionnaire but I am unsure how to ensure that. My understanding is that the value object should not access the repository or service layer so how does it find out if it is unique?

Thanks for any help.

Darren


Solution

  • You could assign the responsibility of generating shortcodes to the questionnaire aggregate. It can be based on a simple counter embedded into questionnaire object. Every entity that want to create a shortcode would call its containing questionnaire's NextShortCode() method to obtain next unique (in scope of current questionnaire) shortcode value.