Exemple #1
0
        private T GetPoco(T proxy, UnrollGraph unrollGraph, Func <PropertyInfo, object, object> getPropertyProxy, Func <IChangeTrackableInternal, UnrollGraph, object> getPropertyPoco)
        {
            var original = Activator.CreateInstance <T>();

            foreach (var property in _Properties.Values)
            {
                object oldPropertyProxyValue = getPropertyProxy(property, proxy);
                if (oldPropertyProxyValue != null)
                {
                    if (oldPropertyProxyValue is IChangeTrackableInternal trackable)
                    {
                        if (unrollGraph.RegisterOrScheduleAssignPocoInsteadOfProxy(trackable, pocoValue => property.SetValue(original, pocoValue, null)))
                        {
                            object newPropertyValue;
                            newPropertyValue = getPropertyPoco(trackable, unrollGraph);
                            unrollGraph.AddMap(new ProxyTargetMap(newPropertyValue, trackable));
                            property.SetValue(original, newPropertyValue, null);
                        }
                    }
                    else if (oldPropertyProxyValue.GetType().GetInterface("IChangeTrackableCollection`1") != null)
                    {
                        if (unrollGraph.RegisterOrScheduleAssignPocoInsteadOfProxy(oldPropertyProxyValue, pocoValue => property.SetValue(original, pocoValue, null)))
                        {
                            System.Collections.IEnumerable originalPropertyValueEnumerable = (System.Collections.IEnumerable)oldPropertyProxyValue;
                            int listCount = originalPropertyValueEnumerable is IEnumerable <object> e?e.Count() : originalPropertyValueEnumerable.Cast <object>().Count();

                            var list = (System.Collections.IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(property.PropertyType.GetGenericArguments().First()), listCount);

                            foreach (var proxyListItem in originalPropertyValueEnumerable)
                            {
                                if (unrollGraph.RegisterOrScheduleAssignPocoInsteadOfProxy(proxyListItem, pocoSetter: null))
                                {
                                    object listItem = getPropertyPoco((IChangeTrackableInternal)proxyListItem, unrollGraph);
                                    unrollGraph.AddMap(new ProxyTargetMap(listItem, proxyListItem));
                                }
                            }

                            unrollGraph.AddListSetter(map =>
                            {
                                foreach (var proxyListItem in originalPropertyValueEnumerable)
                                {
                                    list.Add(map(proxyListItem));
                                }
                            });
                            unrollGraph.AddMap(new ProxyTargetMap(list, oldPropertyProxyValue));
                            property.SetValue(original, list, null);
                        }
                    }
                    else
                    {
                        property.SetValue(original, oldPropertyProxyValue, null);
                    }
                }
            }
            return(original);
        }
Exemple #2
0
        private TResult GetOriginalValue <TResult>(T target, Expression <Func <T, TResult> > selector)
        {
            var propertyInfo = GetPropertyInfo(selector);

            string  propName      = propertyInfo.Name;
            TResult originalValue = _OriginalValueDictionary.TryGetValue(propName, out object value) ?
                                    (TResult)value :
                                    selector.Compile()(target);

            if (originalValue is IChangeTrackableInternal trackable)
            {
                UnrollGraph unrollGraph = new UnrollGraph();
                originalValue = (TResult)trackable.GetOriginal(unrollGraph);
                unrollGraph.FinishWireUp();
            }
            return(originalValue);
        }
Exemple #3
0
        public void Intercept(IInvocation invocation)
        {
            if (!IsInitialized)
            {
                return;
            }
            if (invocation.Method.IsSetter() && !_InRejectChanges)
            {
                if (_ChangeTrackingStatus == ChangeStatus.Deleted)
                {
                    throw new InvalidOperationException("Can not modify deleted object");
                }
                string propertyName = invocation.Method.PropertyName();

                object previousValue = _Properties[propertyName].GetValue(invocation.Proxy, null);

                invocation.Proceed();
                object newValue = _Properties[propertyName].GetValue(invocation.Proxy, null);

                if (!ReferenceEquals(previousValue, newValue))
                {
                    UnsubscribeFromChildStatusChanged(propertyName, previousValue);
                    SubscribeToChildStatusChanged(invocation.Proxy, propertyName, newValue);
                }
                bool originalValueFound = _OriginalValueDictionary.TryGetValue(propertyName, out object originalValue);
                if (!originalValueFound && !Equals(previousValue, newValue))
                {
                    _OriginalValueDictionary.Add(propertyName, previousValue);
                    SetAndRaiseStatusChanged(invocation.Proxy, parents: new List <object> {
                        invocation.Proxy
                    }, setStatusEvenIfStatsAddedOrDeleted: false);
                }
                else if (originalValueFound && Equals(originalValue, newValue))
                {
                    _OriginalValueDictionary.Remove(propertyName);
                    SetAndRaiseStatusChanged(invocation.Proxy, parents: new List <object> {
                        invocation.Proxy
                    }, setStatusEvenIfStatsAddedOrDeleted: false);
                }
                return;
            }
            else if (invocation.Method.IsGetter())
            {
                string propertyName = invocation.Method.PropertyName();
                if (propertyName == nameof(IChangeTrackable.ChangeTrackingStatus))
                {
                    invocation.ReturnValue = _ChangeTrackingStatus;
                }
                else if (propertyName == nameof(System.ComponentModel.IChangeTracking.IsChanged))
                {
                    invocation.ReturnValue = _ChangeTrackingStatus != ChangeStatus.Unchanged;
                }
                else if (propertyName == nameof(IChangeTrackable.ChangedProperties))
                {
                    invocation.ReturnValue = GetChangedProperties();
                }
                else
                {
                    invocation.Proceed();
                    SubscribeToChildStatusChanged(invocation.Proxy, propertyName, invocation.ReturnValue);
                }
                return;
            }
            switch (invocation.Method.Name)
            {
            case nameof(IChangeTrackable <object> .GetOriginalValue):
                invocation.ReturnValue = ((dynamic)this).GetOriginalValue((T)invocation.Proxy, (dynamic)invocation.Arguments[0]);
                break;

            case nameof(IChangeTrackable <object> .GetOriginal):
            case nameof(IChangeTrackable <object> .GetCurrent):
                object poco;
                Func <PropertyInfo, object, object> getPropertyProxy;
                Func <IChangeTrackableInternal, UnrollGraph, object> getPropertyPoco;
                if (invocation.Method.Name == nameof(IChangeTrackable <object> .GetOriginal))
                {
                    getPropertyProxy = (property, proxy) => _OriginalValueDictionary.TryGetValue(property.Name, out object value) ? value : property.GetValue(proxy, null);
                    getPropertyPoco  = (proxy, g) => proxy.GetOriginal(g);
                }
                else
                {
                    getPropertyProxy = (property, proxy) => property.GetValue(proxy, null);
                    getPropertyPoco  = (proxy, g) => proxy.GetCurrent(g);
                }

                bool isRootCall = invocation.Arguments.Length == 0;
                if (isRootCall)
                {
                    UnrollGraph unrollGraph = new UnrollGraph();
                    poco = GetPoco((T)invocation.Proxy, unrollGraph, getPropertyProxy, getPropertyPoco);
                    unrollGraph.FinishWireUp();
                }
                else
                {
                    poco = GetPoco((T)invocation.Proxy, (UnrollGraph)invocation.Arguments[0], getPropertyProxy, getPropertyPoco);
                }
                invocation.ReturnValue = poco;
                break;

            case "add_StatusChanged":
                _StatusChanged += (EventHandler)invocation.Arguments[0];
                break;

            case "remove_StatusChanged":
                _StatusChanged -= (EventHandler)invocation.Arguments[0];
                break;

            case "Delete":
                invocation.ReturnValue = Delete(invocation.Proxy);
                break;

            case "UnDelete":
                invocation.ReturnValue = UnDelete(invocation.Proxy);
                break;

            case nameof(IRevertibleChangeTrackingInternal.AcceptChanges):
                AcceptChanges(invocation.Proxy, invocation.Arguments.Length == 0 ? null : (List <object>)invocation.Arguments[0]);
                break;

            case nameof(IRevertibleChangeTrackingInternal.RejectChanges):
                RejectChanges(invocation.Proxy, invocation.Arguments.Length == 0 ? null : (List <object>)invocation.Arguments[0]);
                break;

            case nameof(IRevertibleChangeTrackingInternal.IsChanged):
                invocation.ReturnValue = GetNewChangeStatus(invocation.Proxy, (List <object>)invocation.Arguments[0]) != ChangeStatus.Unchanged;
                break;

            default:
                invocation.Proceed();
                break;
            }
        }