I'm trying to implement this pattern:
https://mentormate.com/bg/blog/modern-validation-patterns-in-c-sharp/
Starting from the caller I would like to do something like that:
var validatore = new Validator();
validatore.AddRule<TestRule>("OK");
validatore.AddRule<int>(45);
validatore.Validate();
The implementation of the rules:
public interface IValidationRule<T>
{
string Error { get; set; }
bool Validate(T arg);
}
public class TestRule : IValidationRule<string>
{
public string Error { get; set; }
public bool Validate(string arg)
{
return arg == "test";
}
}
The problem is the concrete implementation of the validator. I assumed something like that:
public interface IValidator
{
void AddRule<TRule>(dynamic arg);
ValidationResult Validate();
}
public class Validator : IValidator
{
public void AddRule<T>(dynamic arg)
{
???
}
public ValidationResult Validate()
{
forEach ...
}
}
Where should i put every generic rule in a single collection object (AddRule)? Is my like-implementation on the right way?
Aucun commentaire:
Enregistrer un commentaire