Esempio n. 1
0
        public CollectionInterceptor(ObjectTrackingState objectTrackingState, string property)
            : base(objectTrackingState)
        {
            Added   = new List <T>();
            Removed = new List <T>();

            Property = property;
        }
 public BaseInterceptor(ObjectTrackingState objectTrackingState)
 {
     ObjectTrackingState = objectTrackingState;
 }
        public override void Intercept(IInvocation invocation)
        {
            //get is changed?
            if (invocation.Method.IsGetterMethod(nameof(ITrackableObject.IsChanged)))
            {
                invocation.ReturnValue = ObjectTrackingState.AnyChanges();
                return;
            }

            //get changed properties
            if (invocation.Method.IsGetterMethod(nameof(ITrackableObject.ChangedProperties)))
            {
                invocation.ReturnValue = ObjectTrackingState.GetChangedProperties();
                return;
            }

            //getter?
            if (invocation.Method.IsGetterMethod())
            {
                String property = invocation.Method.GetPropertyName();

                var  p          = invocation.InvocationTarget.GetType().GetProperty(property);
                bool collection = p.PropertyType.IsCollection();

                if (collection)
                {
                    ITrackableCollection col = ObjectTrackingState.GetCollection(property);

                    if (col == null)
                    {
                        invocation.Proceed();

                        col = (ITrackableCollection)invocation.ReturnValue.AsTrackingCollection(property, ObjectTrackingState);

                        ObjectTrackingState.SetCollection(property, col);
                    }

                    invocation.ReturnValue = col;
                }
                else
                {
                    invocation.Proceed();
                }
            }
            //setter?
            else if (invocation.Method.IsSetterMethod())
            {
                string propertyName = invocation.Method.GetPropertyName();

                var accessor = PropertyAccessor.Get(invocation.TargetType.GetProperty(propertyName));

                ObjectTrackingState.AddChangedProperty(propertyName, accessor.GetValue(invocation.InvocationTarget));

                invocation.Proceed();
            }
            else
            {
                invocation.Proceed();

                //stop leaking "this"
                if (invocation.ReturnValue == invocation.InvocationTarget)
                {
                    invocation.ReturnValue = invocation.Proxy;
                }
            }
        }
 public ObjectInterceptor(ObjectTrackingState objectTrackingState)
     : base(objectTrackingState)
 {
 }