示例#1
0
        //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);
        }