improve ObjectValidation.md

This commit is contained in:
az 2021-07-26 21:04:27 +02:00
parent 50afdfaa7e
commit 92bf1e8934

View file

@ -22,6 +22,12 @@ Most languages are offering the following possibilities for failure handling:
* Return values * Return values
* Exceptions * Exceptions
### Context
Validations can be
* context-dependent
* context-independent
### Validation time ### Validation time
* Before object creation * Before object creation
* After creation (but before actual usage of the data) * After creation (but before actual usage of the data)
@ -45,31 +51,47 @@ Most languages are offering the following possibilities for failure handling:
## Potential strategies ## Potential strategies
### Strategies for validations after object creation When putting the basics above together, you might consider the strategies below.
(From https://enterprisecraftsmanship.com/posts/validation-and-ddd/):
* The Execute / TryExecute pattern works best for task-based scenarios.
* For CRUD scenarios, you need to choose between Execute / TryExecute and validation in application services. The choice comes down to purity versus ease of implementation.
### Strategies for validations before object creation ### For mutable or context-dependent objects
Use strategies for validations **after** object creation
(from https://enterprisecraftsmanship.com/posts/validation-and-ddd/):
* The Execute / TryExecute pattern works best for task-based scenarios.
* For CRUD scenarios, you need to choose between Execute / TryExecute and validation in application services. The choice comes down to purity versus ease of implementation.
* With (static/class level) validation AND factory method (for less complex cases) ### Immutable and context-independent objects
* Essence pattern (for complex cases)
### Strategies for validation results: Use validations **before** object creation
* Detailed, containing a list of violations => for crud operations of multi-field input (first, you may want to validate as early as possible,
* Simple: reporting one violation either with a simple or a generic message => for task-based operations (user just wants to execute a task and wants to know if the task was successful or not resp. why not) and secondly, it normally makes no sense to create object that never will be valid.)
### Technical mechanisms: * Factory method AND validation method (for less complex cases)
* Return value for “expected” failures * Note: validation method must be static/at class level
* Exception for “unexpected” failures * Essence pattern (for more complex cases)
### Validation results
Consider
* Detailed results (containing a list of violations)
* for crud-y operations e.g. when using multi-field input
* Simple results
* if detailed results would add no benefits, e.g. for task-based operations (if user just wants to execute a task and/or just wants to know if the task was successful or not resp. why not)
### Technical mechanisms
* Return values
* for “expected” failures
* Exceptions
* for “unexpected” failures
https://stackoverflow.com/questions/99683/which-and-why-do-you-prefer-exceptions-or-return-codes https://stackoverflow.com/questions/99683/which-and-why-do-you-prefer-exceptions-or-return-codes
https://stackoverflow.com/questions/5460101/choosing-between-exception-and-return-value https://stackoverflow.com/questions/5460101/choosing-between-exception-and-return-value
https://enterprisecraftsmanship.com/posts/error-handling-exception-or-result/ https://enterprisecraftsmanship.com/posts/error-handling-exception-or-result/
--- ---
###Appendix ###Appendix
Possibilities for creation of objects (patterns, etc):
Object creation (patterns, etc):
* Constructor * Constructor
* Factory method (e.g. create method) * Factory method (e.g. create method)
* Factory * Factory