Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD: Who is responsible for the creation of value object and entity?

In domain driven design, client talk to aggregate root directly to implement the business logic, aggregate root could be created by factory or loaded from repository. In order to implement the business logic, we have to create a new value object in some cases, so my question is:

  1. Should aggregate responsible to the creation of value object, meaning that we encapsulate the creation of value object in aggregate root.
  2. Can I create value object directly in application service without aggregate?
like image 333
LoremIpsum Avatar asked Feb 06 '23 23:02

LoremIpsum


1 Answers

Can I create value object directly in application service without aggregate?

Sure, go right ahead. That's a pretty common pattern - for example, in a client/server architecture, the client will send a message to the application inviting some action (command/query) on the domain model. That message will be designed to cross process boundaries - in other words, it's a data transfer object. The application component will take that message, and re-express it as value objects that will be understood by the domain.

Example: a query of the domain might ask for details of some Account, which is identified by a UUID. So the initial DTO might be a string. Application layer throws that string into a JSON parser, then finds the account id argument (which is "just data"), and use it to create an AccountId value object (which the domain will recognize).

Part of the point of value objects is that equal objects freely substitute for one another. Nobody cares which 7 you have.

we encapsulate the creation of value object in aggregate root.

Your aggregate code is likely to create value objects too. Changes to your aggregate's state will often require creating/computing new values. Nothing wrong with that.

like image 60
VoiceOfUnreason Avatar answered May 19 '23 14:05

VoiceOfUnreason