Advertisement
GeneralGDA

Baby Dependency Object

Aug 26th, 2020
1,450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using JetBrains.Annotations;
  3.  
  4. namespace Cadwise.DressCode.JarJarBinks.Catalogue.Verification.Core.Scaffolding
  5. {
  6.     internal class PropertyTreeObject
  7.     {
  8.         [CanBeNull]
  9.         private readonly PropertyTreeObject _parent;
  10.  
  11.         [NotNull]
  12.         private readonly Dictionary<string, object> _properties;
  13.  
  14.         protected PropertyTreeObject([CanBeNull] PropertyTreeObject parent)
  15.         {
  16.             _properties = new Dictionary<string, object>();
  17.             _parent = parent;
  18.         }
  19.  
  20.         protected T GetValue<T>([NotNull] string name)
  21.         {
  22.             Guard.ThrowIf.Argument.IsNull(name, nameof(name));
  23.  
  24.             if (_properties.TryGetValue(name, out var result))
  25.             {
  26.                 return (T) result;
  27.             }
  28.  
  29.             if (null == _parent)
  30.             {
  31.                 return default(T);
  32.             }
  33.  
  34.             return _parent.GetValue<T>(name);
  35.         }
  36.  
  37.         protected void SetValue([NotNull] string name, [CanBeNull] object value)
  38.         {
  39.             Guard.ThrowIf.Argument.IsNull(name, nameof(name));
  40.  
  41.             _properties[name] = value;
  42.         }
  43.     }
  44.  
  45.     internal sealed class Example : PropertyTreeObject
  46.     {
  47.         public Example([CanBeNull] PropertyTreeObject parent)
  48.             :
  49.             base(parent)
  50.         {
  51.         }
  52.  
  53.         public Zygote Expected
  54.         {
  55.             get
  56.             {
  57.                 return GetValue<Zygote>(nameof(Expected));
  58.             }
  59.  
  60.             set
  61.             {
  62.                 SetValue(nameof(Expected), value);
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement