public void NullifyingRemoves(IKeyValueStore store) { var values = this.SetupBinder <TestBind>(store); var key = ObjectStoreBinder.GetBindingKey(typeof(TestBind), nameof(TestBind.StringProperty)); values.BoundObject.StringProperty = Guid.NewGuid().ToString(); store.Contains(key).Should().BeTrue(); values.BoundObject.StringProperty = null; store.Contains(key).Should().BeFalse(); }
public void Binding_ProtectedGetter(IKeyValueStore store) { var values = SetupBinder <TestBind>(store); var key = ObjectStoreBinder.GetBindingKey(typeof(TestBind), nameof(TestBind.ProtectedGetterProperty)); values.BoundObject.ProtectedGetterProperty = Guid.NewGuid().ToString(); store.Contains(key).Should().BeFalse(); }
/// <summary> /// Gets a required value from settings /// </summary> /// <typeparam name="T"></typeparam> /// <param name="store"></param> /// <param name="key"></param> /// <returns></returns> public static T GetRequired <T>(this IKeyValueStore store, string key) { if (!store.Contains(key)) { throw new ArgumentException($"Store key '{key}' is not set"); } return(store.Get <T>(key) !); }
/// <summary> /// Gets a generic object from the store /// </summary> /// <typeparam name="T"></typeparam> /// <param name="store"></param> /// <param name="key"></param> /// <returns></returns> public static T Get <T>(this IKeyValueStore store, string key, T defaultValue = default) { if (!store.Contains(key)) { return(defaultValue); } return((T)store.Get(typeof(T), key)); }
/// <summary> /// This will only set the value if the setting is not currently set. Will not fire Changed event /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> public static bool SetDefault <T>(this IKeyValueStore store, string key, T value) { if (store.Contains(key)) { return(false); } store.Set(key, value); return(true); }
public void DefaultValueRemoves(IKeyValueStore store) { var values = this.SetupBinder <TestBind>(store); var key = ObjectStoreBinder.GetBindingKey(typeof(TestBind), nameof(TestBind.IntValue)); values.BoundObject.IntValue = 99; store.Get <int>(key).Should().Be(99); values.BoundObject.IntValue = 0; store.Contains(key).Should().BeFalse(); }
public void Binding_Basic(IKeyValueStore store) { var key = ObjectStoreBinder.GetBindingKey(typeof(TestBind), nameof(TestBind.StringProperty)); var random = Guid.NewGuid().ToString(); var values = SetupBinder <TestBind>(store); values.BoundObject.StringProperty = random; store.Contains(key).Should().BeTrue(); store.Get <string>(key).Should().Be(random); }
public void Bind(INotifyPropertyChanged npc, IKeyValueStore store) { var type = npc.GetType(); var props = this.GetTypeProperties(type); foreach (var prop in props) { var key = GetBindingKey(type, prop); if (store.Contains(key)) { var value = store.Get(prop.PropertyType, key); prop.SetValue(npc, value); } } npc.PropertyChanged += this.OnPropertyChanged; this.bindings.Add(npc, store); }
public void Bind(INotifyPropertyChanged npc, IKeyValueStore store) { try { var type = npc.GetType(); var props = this.GetTypeProperties(type).ToList(); // Skip if there are no properties to bind if (props.Count == 0) { this.logger?.LogInformation($"Skip model bind (no public props): {npc.GetType().FullName} to store: {store.Alias}"); return; } foreach (var prop in props) { var key = GetBindingKey(type, prop); if (store.Contains(key)) { var value = store.Get(prop.PropertyType, key); try { prop.SetValue(npc, value); } catch (Exception ex) { this.logger?.LogError($"Failed to bind {prop.Name} on {type.FullName}", ex); } } } npc.PropertyChanged += this.OnPropertyChanged; this.logger?.LogInformation($"Successfully bound model: {npc.GetType().FullName} to store: {store.Alias}"); this.bindings.Add(npc, store); } catch (Exception ex) { this.logger?.LogError(ex, $"Failed to bind model: {npc?.GetType().FullName ?? "Unknown"} to store: {store.Alias}"); } }