I am designing library which is supposed to help me with designing domains in future projects. I made some basic Value Objects like EmailAddress or PhoneNumber. Those are easy. It starts to get problematic for me when such Value Object may have different ruleset that say if it is valid or not.
Lets use Password
as example here since it is actual reason of me writing this question. In previous projects I had password rulesets validated in Password
construtor. But it makes this implementation project specific, and violates - I think - concern separation principle.
I was thinking of Strategy Patten as solution here. But construction such VO in this way: new Password("123456", new AwfullyLoosePasswordValidationStrategy())
sounds terrible to me. I was also thinking of static setter in VO setValidationStrategy(PasswordValidationStrategy strategy)
, but this still is against concern separation and sounds really odd to me.
So the first part of response is - I think - that Password should do only basic sanity check like isNullOrEmptyString
and nothing more. But when I should do proper validation? During entity creation? Before persisting somewhere in service layer? Entity idea sounds not that bad for me, but how setPassword(Password password
) would know about strategy I want to use? I try to avoid singletons in my projects and almost everything is done via DI there.
tl;dr: Where should I validate Value Object that cannot be validated in its constructor?