I’m new in “DDD with C#” world and I have a question about where ensure some validation rules.
For example, one of my entities have a concept of closure where, if his status is locked , no other propertie can be changed.
Where’s is the best place to positioning this validation? I don’t seem right bring this rule to the aplication layer, only inhibiting the field edition and inside of each setter seems to me broke the DRY principle. Any suggestion?
If that validation rule is part of your domain, then the domain object is definitely the right place to put the logic.
If you’re concerned about keeping your code DRY, you have a few options:
Look at using interception with Unity or Castle
Make all your setters private and have an
UpdateIfNotLocked()method which takes anExpressionto indicate which property to update, a new value for the property, and creates, compiles and executes anExpressionto set that property if the object isn’t in a locked stateBite the bullet and have guards in the setters for this class.
Methods 1 and 2 could both be a reasonable amount of work / hassle, so you might want to take the pragmatic approach in this case.