protected bool Update <TType, TValue>(object sender, TType targetObject, NDBasePropertyKey <TKey, TType, TValue> property, TValue newValue, bool hasNewValue, Func <TValue, bool> updateCode) where TType : class { var(currentValue, currentProvider) = PropertyRegistar <TKey> .GetValueAndProvider(property, targetObject); var(oldValue, hasOldValue) = GetValue(targetObject, property); return(Update(sender, targetObject, property, newValue, hasNewValue, updateCode, oldValue, hasOldValue, currentProvider, currentValue)); }
public void TestSettingsCallOnChangedEquals() { var t = new TestObject(); const string str1 = "Hallo Welt!"; var p1 = PropertyRegistar <Configuration> .Register <TestObject, string>(x => x.TestChangeMethod, str1, NDPropertySettings.None); var p2 = PropertyRegistar <Configuration> .Register <TestObject, string>(x => x.TestChangeMethod, str1, NDPropertySettings.CallOnChangedHandlerOnEquals); PropertyRegistar <Configuration> .SetValue(p1, t, str1); t.testArguments = null; PropertyRegistar <Configuration> .SetValue(p1, t, str1); Assert.IsNull(t.testArguments); PropertyRegistar <Configuration> .SetValue(p2, t, str1); Assert.IsNotNull(t.testArguments); Assert.AreEqual(str1, t.testArguments.Property.NewValue); //Assert.AreEqual(str1, t.testArguments.OldValue); }
//protected bool Update<TType, TValue>(object sender, TType targetObject, NDBasePropertyKey<TKey, TType, TValue> property, TValue newValue, bool hasNewValue, TValue oldValue, bool hasOldValue, ValueProvider<TKey> currentProvider, TValue currentValue) // where TType : class //{ // return Update(sender, targetObject, property, newValue, hasNewValue, () => true, oldValue, hasOldValue, currentProvider, currentValue); //} internal bool Update <TType, TValue>(object sender, TType targetObject, NDBasePropertyKey <TKey, TType, TValue> property, TValue newProviderValue, bool hasNewValue, Func <TValue, bool> updateCode, TValue oldProviderValue, bool hasOldValue, ValueProvider <TKey> oldActualProvider, TValue oldActualValue) where TType : class { var otherProviderIndex = PropertyRegistar <TKey> .ProviderOrder[oldActualProvider]; var thisIndex = PropertyRegistar <TKey> .ProviderOrder[this]; if (!property.Settigns.HasFlag(NDPropertySettings.CallOnChangedHandlerOnEquals) && Object.Equals(oldProviderValue, newProviderValue) && hasOldValue == hasNewValue) { return(true); } TValue newActualValue = default; ValueProvider <TKey> newActualProvider = null; if (this == oldActualProvider && !hasNewValue) { // the current value was provided by the changing provider but now it will no longer have a value // we need to find out what the new value will be. bool found = false; foreach (var item in PropertyRegistar <TKey> .ValueProviders) { if (item == this) { continue; } var(providerValue, hasValue) = item.GetValue(targetObject, property); if (hasValue) { found = true; newActualProvider = item; newActualValue = providerValue; break; } } if (!found) { throw new InvalidOperationException("No Value Found"); } } else if (otherProviderIndex >= thisIndex) { newActualProvider = this; newActualValue = newProviderValue; } else { newActualProvider = oldActualProvider; newActualValue = oldActualValue; } // We need to call the actial update after we recived the current old value. Otherwise we could already read the OnChangingArg <TKey, TValue> onChangingArg; if (property as object is NDAttachedPropertyKey <TKey, TType, TValue> attach) { var attachArg = OnChangingArg.Create(targetObject, oldProviderValue, hasOldValue, newProviderValue, hasNewValue, this, oldActualProvider, newActualProvider, oldActualValue, newActualValue, hasNewValue || !this.canDeletionBePrevented); onChangingArg = attachArg; attach.changedMethod(attachArg); } else if (property as object is NDPropertyKey <TKey, TType, TValue> p) { onChangingArg = OnChangingArg.Create(oldProviderValue, hasOldValue, newProviderValue, hasNewValue, this, oldActualProvider, newActualProvider, oldActualValue, newActualValue, hasNewValue || !this.canDeletionBePrevented); p.changedMethod(targetObject)(onChangingArg); } else { throw new NotSupportedException(); } var result = PropertyRegistar <TKey> .ChangeValue(sender, property, targetObject, onChangingArg, updateCode); FireEventHandler(property, targetObject, sender, ChangedEventArgs.Create(targetObject, property, oldProviderValue, newProviderValue)); return(result); }