Benoit Martin
Benoit Martin

Reputation: 3303

Collection of Domain Objects in a Domain Model

This may be a basic question but I am pretty new to DDD. I have an domain object that we'll call Adjustment which can be processed in bulk from the UI. Before we process the Adjustments, we need to validate the date those adjustments will be applied. My problem is with the location of that IsValidDate() method in my domain object.

  1. Should it be a static method in the Adjustment class?
  2. Should it be part of an AdjustmentService class?
  3. Should I create a AdjustmentsGroup domain object to contain a collection of adjustments and which would also implement IsValidDate?

I would tend to think that the 3rd option is the best one but I have a hard time thinking of a domain term for the group of Adjustment objects. Is it ok to "force" a container type domain object for this type of scenario? Is there a common practice to handle this?

Thank you

Edit: IsValidDate actually contains business logic. This is not just a simple date validation method

Upvotes: 4

Views: 1486

Answers (3)

ericvg
ericvg

Reputation: 3957

I would favor the 3rd option, but create make a more generic function on Adjustment, like Validate(), which would return a collection of errors/validation faults on itself. That way you can add validation rules at a later time without changing your interface. The Validate() function of AdjustmentsGroup would simply call Validate() on each member in the collection. An equally valid (no pun intended) way would also be to have a separate validator class for the object that contains all validation logic.

Your AdjustmentService would then call Validate() on AdjustmentsGroup before processing the adjustments.

Upvotes: 1

Charles Bretana
Charles Bretana

Reputation: 146557

I would vote for 2) Make it a DomainService. The code to implement it could be in either a DomainServices class, an AdjustmentServices class, or a ValidateAdjustmentService class, depending on what other services are in the domain model, and what makes the most sense from an organizational perspective.

Another option, (if the rules implemented by this service are business rules) is to implement this as a SPECIFICATION. (Check out pages 224 - 240 in DDD)

Upvotes: 2

iandisme
iandisme

Reputation: 6396

If you're doing simple is-this-actually-a-date string validation, the correct place for that method, in my opinion, is inside a Service class, as Charles suggested.

However, if data within the Adjustment or AdjustmentCollection objects can change the date validation logic, e.g. disallowing certain date ranges, the method belongs in that object.

Upvotes: 1

Related Questions