Guest User

Untitled

a guest
Oct 16th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. public class RuleValidator : IRuleValidator
  2. {
  3. private readonly Dictionary<Type, object> _map = new Dictionary<Type, object>();
  4.  
  5. public IRuleValidator Register<T>(IValidationRule<T> rule)
  6. {
  7. if (!_map.ContainsKey(typeof(T)))
  8. {
  9. _map.Add(typeof(T), rule);
  10. }
  11.  
  12. return this;
  13. }
  14.  
  15. public IRuleValidator Validate<T>(T input) where T : IValidationRuleInput
  16. {
  17. if (!_map.ContainsKey(input.GetType()))
  18. throw new InvalidOperationException($"No rule defined for input type {input.GetType()}");
  19.  
  20. var rule = (IValidationRule<T>)_map[input.GetType()];
  21. rule.Validate(input);
  22.  
  23. return this;
  24. }
  25. }
Add Comment
Please, Sign In to add comment