I have quite difficult validation, before calling external API endpoints. Each endpoint requires it's own model, and each model has it's own bunch of validation steps.
Scheme is similar: Model A Model B
And
Validation steps: VS1, VS2, VS3, VS4
Model A requires VS1, VS3 and VS4.
Model B - VS2, VS4
Example of validation step:
public ValidationBuilder IsOldModel() {
if (_model.IsNew) {
AddError("");
}
return this;
}
Example of validation service setup:
public IEnumerable Validate(Model model) { var validationResultsBuilder = new ValidationBuilder (model);
validationResultsBuilder
.IsOldModel()
.NotDeleted()
.NotSynchronized();
return validationResultsBuilder.GetMessages();
}
Plus, validation steps might write some messages, that I will show to user. My decision was to create fluent builder, and it looks good, easy to understand by the team, easy to support. But occasionally, customer requested to make several steps "Critical". Critical step - step, that writes something to "output" and stops validation process. Now, I faced drawback of builder - i cannot simply interop pipeline. To skip all the rest steps, I have to add check(similar behavior) to all steps.
public ValidationBuilder IsOldModel() {
if (!_canContinue) {
return this;
}
if (_model.IsNew) {
AddError("");
}
return this;
}
I investigated possibility to mix my solution with Decorator design pattern, but it increases code size even better, than simplest solution. I searched, but haven't found something useful. Anyone has bright idea on how to add behavior to builder?