ruby-on-railsdomain-driven-designcqrsevent-sourcingrails-event-store

Where to create Value Objects in CQRS & DDD flow


I am struggling with construction of ValueObjects during life cycle of a command.

Here is my situation:

  1. Command Request comes to controller action.
  2. Create command object with request params.
  3. Pass command object to app service
  4. Command handler first validate command attributes
  5. then create aggregate and pass command attributes to aggregate function.
  6. and aggregate function pass attributes to domain event.

My question is where I should put Object Creation logic. or in other words which component of DDD is responsible for initialisation of Objects (Value Objects, Entities etc.) for Aggregates to work with?


Solution

  • My question is where I should put Object Creation logic. or in other words which component of DDD is responsible for initialisation of Objects (Value Objects, Entities etc.) for Aggregates to work with?

    The usual answer is that domain object creation happens via "factories" (see Evans, chapter 6), that are usually exported from the domain model and invoked by the application code that needs them.

    A factory might be an object in its own right, or it just be a function, or even a constructor.

    A review of the DDDSample by Citerus might help illustrate:

    https://github.com/citerus/dddsample-core/blob/master/src/main/java/se/citerus/dddsample/interfaces/booking/web/CargoAdminController.java#L133

    Here, the controller extracts the necessary data (as primitives) and passes that information toward the changeDestination logic.

    https://github.com/citerus/dddsample-core/blob/master/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/BookingServiceFacadeImpl.java#L74

    At the next class in, the Strings are replaced with Value Objects; the TrackingId constructor and the UnLocode constructor implement the factory role in this case. The value objects are then passed toward the changeDestination logic.