ValidatorContext.cs 901 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System.Collections.Generic;
  2. namespace Quadarax.Foundation.Core.Value
  3. {
  4. public class ValidatorContext
  5. {
  6. private readonly IDictionary<string, object?> _context = new Dictionary<string, object?>();
  7. public ValidatorContext()
  8. {
  9. }
  10. public ValidatorContext(IEnumerable<KeyValuePair<string, object?>> initialContext)
  11. {
  12. foreach (var item in initialContext)
  13. {
  14. _context.Add(item);
  15. }
  16. }
  17. public TValue? GetValue<TValue>(string key)
  18. {
  19. return (TValue?) _context[key];
  20. }
  21. public void SetValue<TValue>(string key, TValue value)
  22. {
  23. if (!_context.TryAdd(key, value))
  24. _context[key] = value;
  25. }
  26. public bool Exists(string key)
  27. {
  28. return _context.ContainsKey(key);
  29. }
  30. }
  31. }