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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With