Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RuleValidator : IRuleValidator
- {
- private readonly Dictionary<Type, object> _map = new Dictionary<Type, object>();
- public IRuleValidator Register<T>(IValidationRule<T> rule)
- {
- if (!_map.ContainsKey(typeof(T)))
- {
- _map.Add(typeof(T), rule);
- }
- return this;
- }
- public IRuleValidator Validate<T>(T input) where T : IValidationRuleInput
- {
- if (!_map.ContainsKey(input.GetType()))
- throw new InvalidOperationException($"No rule defined for input type {input.GetType()}");
- var rule = (IValidationRule<T>)_map[input.GetType()];
- rule.Validate(input);
- return this;
- }
- }
Add Comment
Please, Sign In to add comment