0

I have an Age Value Object which validates age betweens 2 const ( MinimumAge and MaximumAge )

At some point i want to be able to change this min and max range dynamically without editing code after project got published ( for example reading it from DB or fetching it from somewhere else... )

How can i do this without breaking DDD rules and be loyal to Value Object Self-Validating ?

I tried few ways but all broke DDD rules at some point

  • Would it be possible to treat your `const` minmax values as fallbacks, and use them if there's nothing in the DB? Or if you know there'll be something in the DB, to check whether you should use the DB values? For example, let's say that your DB has a `config` table, and the three columns which are of interest in this case - `minage (INT)`, `maxage (INT)`, `shoulduse (TINYINT, DEFAULT 0)`. If `shoulduse` is set to 1, and the DB values are set, you'd use them. If not, you'd use your fallback values. – FiddlingAway Jan 21 '23 at 23:23

1 Answers1

0

My suggestion is to not treat a value that can change as an invariant.

If you have different, but invariant, age ranges then you may have more than one Age value object that is abstracted behind an interface and the object making use of the value object has some indicator of the AgeType. For example, you may have a RegistrationAge that validates between ages 2-16 and another for VotingAge that validates from 18-anything. However, if the ages may change then they really shouldn't be hard-coded as invariants in the value object. Instead, they may be provided to the value object as parameters that may be defined outside the domain (database, or some other settings, for instance).

It may be that what you are after is more of a Specification, or your Age value object may act as a specification.

Eben Roux
  • 12,983
  • 2
  • 27
  • 48