(also check out the alternative solution below!)
var valMethods = new List<Func<ValidationResult>> { ()=>Method1(number,family), ()=>Method2(name), // ... }; foeach(var valMethod in valMethods) { var valResult = valMethod(); if (!valResult.IsValid) { return valResult.Errors.First(); } }
This happens, however, with some performance degradation for delegates, and since you need parameters, it's hard to put it outside, for example. in the method returning delegates.
Alternatively, you can create a batch of values ββas a parameter (maybe your calling class with the appropriate interface), which is the same for all methods, and each method selects what it needs (which, in turn, makes the verification methods less clear )
Another alternative would be to simply return the bool values ββand save the verification details in separate objects, perhaps implement extension methods for verification (also check the response of Myleo predicates):
internal static class ValidationMethods { public static bool CheckIsValid1(this IList<ValidationResult> valResults, int number, string family) { var validationResult = new validationResult(); if(number > 10 || family="akbari") { validationResult.Errors.Add(new ValidationFailure("", "Invalid Number")); } valResults.Add(validationResult); return validationResult.IsValid; } public static bool CheckIsValid2(this IList<ValidationResult> valResults, string name) {
then, when checking the code:
var valResults = new List<ValidationResult>(); if (!valResults.CheckIsValid1(number, family) || !valResults.CheckIsValid2(name) ||
This way you do not have to talk to delegates. The checks stop when the first failure (or the status check stops at the first success, IsValid is denied here). And your valResults still contain validation data.
From your code, it might also just be easy to use exceptions. Such an exception check may be required if errors occur frequently and exceptions kill performace, but if errors are exceptional, then feel free to use the exception!