Пример #1
0
 internal void HandleDeleteEntity(object source, string sourceProperty, string sourceEntitySet, ICollection collection, object target, string targetEntitySet)
 {
     if (!this.Context.ApplyingChanges && ((source == null) || !this.IsDetachedOrDeletedFromContext(source)))
     {
         if ((this.IsContextTrackingEntity(target) && !this.DetachBehavior) && (this.CollectionChanged != null))
         {
             EntityCollectionChangedParams arg = new EntityCollectionChangedParams(this.Context, source, sourceProperty, sourceEntitySet, collection, target, targetEntitySet, NotifyCollectionChangedAction.Remove);
             if (this.CollectionChanged(arg))
             {
                 return;
             }
         }
         if ((source != null) && !this.IsContextTrackingEntity(source))
         {
             throw new InvalidOperationException(Strings.DataBinding_BindingOperation_DetachedSource);
         }
         if (this.IsContextTrackingEntity(target))
         {
             if (this.DetachBehavior)
             {
                 this.Context.Detach(target);
             }
             else
             {
                 this.Context.DeleteObject(target);
             }
         }
     }
 }
Пример #2
0
 internal void HandleAddEntity(object source, string sourceProperty, string sourceEntitySet, ICollection collection, object target, string targetEntitySet)
 {
     if (!this.Context.ApplyingChanges && ((source == null) || !this.IsDetachedOrDeletedFromContext(source)))
     {
         EntityDescriptor entityDescriptor = this.Context.GetEntityDescriptor(target);
         if ((!this.AttachBehavior && ((entityDescriptor == null) || (((source != null) && !this.IsContextTrackingLink(source, sourceProperty, target)) && (entityDescriptor.State != EntityStates.Deleted)))) && (this.CollectionChanged != null))
         {
             EntityCollectionChangedParams arg = new EntityCollectionChangedParams(this.Context, source, sourceProperty, sourceEntitySet, collection, target, targetEntitySet, NotifyCollectionChangedAction.Add);
             if (this.CollectionChanged(arg))
             {
                 return;
             }
         }
         if ((source != null) && this.IsDetachedOrDeletedFromContext(source))
         {
             throw new InvalidOperationException(Strings.DataBinding_BindingOperation_DetachedSource);
         }
         entityDescriptor = this.Context.GetEntityDescriptor(target);
         if (source != null)
         {
             if (!this.AttachBehavior)
             {
                 if (entityDescriptor == null)
                 {
                     this.Context.AddRelatedObject(source, sourceProperty, target);
                 }
                 else if ((entityDescriptor.State != EntityStates.Deleted) && !this.IsContextTrackingLink(source, sourceProperty, target))
                 {
                     this.Context.AddLink(source, sourceProperty, target);
                 }
             }
             else if (entityDescriptor == null)
             {
                 BindingUtils.ValidateEntitySetName(targetEntitySet, target);
                 this.Context.AttachTo(targetEntitySet, target);
                 this.Context.AttachLink(source, sourceProperty, target);
             }
             else if ((entityDescriptor.State != EntityStates.Deleted) && !this.IsContextTrackingLink(source, sourceProperty, target))
             {
                 this.Context.AttachLink(source, sourceProperty, target);
             }
         }
         else if (entityDescriptor == null)
         {
             BindingUtils.ValidateEntitySetName(targetEntitySet, target);
             if (this.AttachBehavior)
             {
                 this.Context.AttachTo(targetEntitySet, target);
             }
             else
             {
                 this.Context.AddObject(targetEntitySet, target);
             }
         }
     }
 }
Пример #3
0
        internal void HandleDeleteEntity(
            object source,
            string sourceProperty,
            string sourceEntitySet,
            ICollection collection,
            object target,
            string targetEntitySet)
        {
            if (this.Context.ApplyingChanges)
            {
                return;
            }

            Debug.Assert(
                (source == null && sourceProperty == null) || (source != null && !String.IsNullOrEmpty(sourceProperty)),
                "source and sourceProperty should either both be present or both be absent.");

            Debug.Assert(target != null, "target must be provided by the caller.");
            Debug.Assert(BindingEntityInfo.IsEntityType(target.GetType()), "target must be an entity type.");

            Debug.Assert(!this.AttachBehavior, "AttachBehavior is only allowed during Construction and Load when this method should never be entered.");

            if (source != null && this.IsDetachedOrDeletedFromContext(source))
            {
                return;
            }

            bool contextOperationRequired = this.IsContextTrackingEntity(target) && !this.DetachBehavior;

            if (contextOperationRequired)
            {
                if (this.CollectionChanged != null)
                {
                    EntityCollectionChangedParams args = new EntityCollectionChangedParams(
                        this.Context,
                        source,
                        sourceProperty,
                        sourceEntitySet,
                        collection,
                        target,
                        targetEntitySet,
                        NotifyCollectionChangedAction.Remove);

                    if (this.CollectionChanged(args))
                    {
                        return;
                    }
                }
            }

            if (source != null && !this.IsContextTrackingEntity(source))
            {
                throw new InvalidOperationException(Strings.DataBinding_BindingOperation_DetachedSource);
            }

            if (this.IsContextTrackingEntity(target))
            {
                if (this.DetachBehavior)
                {
                    this.Context.Detach(target);
                }
                else
                {
                    this.Context.DeleteObject(target);
                }
            }
        }
Пример #4
0
        internal void HandleAddEntity(
            object source,
            string sourceProperty,
            string sourceEntitySet,
            ICollection collection,
            object target,
            string targetEntitySet)
        {
            if (this.Context.ApplyingChanges)
            {
                return;
            }

            Debug.Assert(
                (source == null && sourceProperty == null) || (source != null && !String.IsNullOrEmpty(sourceProperty)),
                "source and sourceProperty should either both be present or both be absent.");

            Debug.Assert(target != null, "target must be provided by the caller.");
            Debug.Assert(BindingEntityInfo.IsEntityType(target.GetType()), "target must be an entity type.");

            if (source != null && this.IsDetachedOrDeletedFromContext(source))
            {
                return;
            }

            EntityDescriptor targetDescriptor = this.Context.GetEntityDescriptor(target);

            bool contextOperationRequired = !this.AttachBehavior &&
                                            (targetDescriptor == null ||
                                             (source != null && !this.IsContextTrackingLink(source, sourceProperty, target) && targetDescriptor.State != EntityStates.Deleted));

            if (contextOperationRequired)
            {
                if (this.CollectionChanged != null)
                {
                    EntityCollectionChangedParams args = new EntityCollectionChangedParams(
                        this.Context,
                        source,
                        sourceProperty,
                        sourceEntitySet,
                        collection,
                        target,
                        targetEntitySet,
                        NotifyCollectionChangedAction.Add);

                    if (this.CollectionChanged(args))
                    {
                        return;
                    }
                }
            }

            if (source != null && this.IsDetachedOrDeletedFromContext(source))
            {
                throw new InvalidOperationException(Strings.DataBinding_BindingOperation_DetachedSource);
            }

            targetDescriptor = this.Context.GetEntityDescriptor(target);

            if (source != null)
            {
                if (this.AttachBehavior)
                {
                    if (targetDescriptor == null)
                    {
                        BindingUtils.ValidateEntitySetName(targetEntitySet, target);

                        this.Context.AttachTo(targetEntitySet, target);
                        this.Context.AttachLink(source, sourceProperty, target);
                    }
                    else
                    if (targetDescriptor.State != EntityStates.Deleted && !this.IsContextTrackingLink(source, sourceProperty, target))
                    {
                        this.Context.AttachLink(source, sourceProperty, target);
                    }
                }
                else
                {
                    if (targetDescriptor == null)
                    {
                        this.Context.AddRelatedObject(source, sourceProperty, target);
                    }
                    else
                    if (targetDescriptor.State != EntityStates.Deleted && !this.IsContextTrackingLink(source, sourceProperty, target))
                    {
                        this.Context.AddLink(source, sourceProperty, target);
                    }
                }
            }
            else
            if (targetDescriptor == null)
            {
                BindingUtils.ValidateEntitySetName(targetEntitySet, target);

                if (this.AttachBehavior)
                {
                    this.Context.AttachTo(targetEntitySet, target);
                }
                else
                {
                    this.Context.AddObject(targetEntitySet, target);
                }
            }
        }
        /// <summary>Handle Deletes from a tracked DataServiceCollection. Perform operations on context to reflect the changes.</summary>
        /// <param name="source">The source object that reference the target object through a navigation property.</param>
        /// <param name="sourceProperty">The navigation property in the source object that reference the target object.</param>
        /// <param name="sourceEntitySet">The entity set of the source object.</param>
        /// <param name="collection">The collection containing the target object.</param>
        /// <param name="target">The target entity.</param>
        /// <param name="targetEntitySet">The entity set name of the target object.</param>
        internal void HandleDeleteEntity(
            object source,
            string sourceProperty,
            string sourceEntitySet,
            ICollection collection,
            object target,
            string targetEntitySet)
        {
            if (this.Context.ApplyingChanges)
            {
                return;
            }

            Debug.Assert(
                (source == null && sourceProperty == null) || (source != null && !String.IsNullOrEmpty(sourceProperty)),
                "source and sourceProperty should either both be present or both be absent.");

            Debug.Assert(target != null, "target must be provided by the caller.");
            Debug.Assert(BindingEntityInfo.IsEntityType(target.GetType()), "target must be an entity type.");

            Debug.Assert(!this.AttachBehavior, "AttachBehavior is only allowed during Construction and Load when this method should never be entered.");

            // Do not handle delete for already detached and deleted entities.
            if (source != null && this.IsDetachedOrDeletedFromContext(source))
            {
                return;
            }

            // Do we need an operation on context to handle the Delete operation.
            // Detach behavior is special because it is only applicable in Clear
            // cases, where we don't callback users for detach nofications.
            bool contextOperationRequired = this.IsContextTrackingEntity(target) && !this.DetachBehavior;

            if (contextOperationRequired)
            {
                // First give the user code a chance to handle Delete operation.
                if (this.CollectionChanged != null)
                {
                    EntityCollectionChangedParams args = new EntityCollectionChangedParams(
                        this.Context,
                        source,
                        sourceProperty,
                        sourceEntitySet,
                        collection,
                        target,
                        targetEntitySet,
                        NotifyCollectionChangedAction.Remove);

                    if (this.CollectionChanged(args))
                    {
                        return;
                    }
                }
            }

            // The user callback code could detach the source.
            if (source != null && !this.IsContextTrackingEntity(source))
            {
                throw new InvalidOperationException(Strings.DataBinding_BindingOperation_DetachedSource);
            }

            // Default implementation.
            // Remove the entity from the context if it is currently being tracked.
            if (this.IsContextTrackingEntity(target))
            {
                if (this.DetachBehavior)
                {
                    this.Context.Detach(target);
                }
                else
                {
                    this.Context.DeleteObject(target);
                }
            }
        }
        /// <summary>Handle Adds to a tracked DataServiceCollection. Perform operations on context to reflect the changes.</summary>
        /// <param name="source">The source object that reference the target object through a navigation property.</param>
        /// <param name="sourceProperty">The navigation property in the source object that reference the target object.</param>
        /// <param name="sourceEntitySet">The entity set of the source object.</param>
        /// <param name="collection">The collection containing the target object.</param>
        /// <param name="target">The target entity to attach.</param>
        /// <param name="targetEntitySet">The entity set name of the target object.</param>
        internal void HandleAddEntity(
            object source,
            string sourceProperty,
            string sourceEntitySet,
            ICollection collection,
            object target,
            string targetEntitySet)
        {
            if (this.Context.ApplyingChanges)
            {
                return;
            }

            Debug.Assert(
                (source == null && sourceProperty == null) || (source != null && !String.IsNullOrEmpty(sourceProperty)),
                "source and sourceProperty should either both be present or both be absent.");

            Debug.Assert(target != null, "target must be provided by the caller.");
            Debug.Assert(BindingEntityInfo.IsEntityType(target.GetType()), "target must be an entity type.");

            // Do not handle add for already detached and deleted entities.
            if (source != null && this.IsDetachedOrDeletedFromContext(source))
            {
                return;
            }

            // Do we need an operation on context to handle the Add operation.
            EntityDescriptor targetDescriptor = this.Context.GetEntityDescriptor(target);

            // Following are the conditions where context operation is required:
            // 1. Not a call to Load or constructions i.e. we have Add behavior and not Attach behavior
            // 2. Target entity is not being tracked
            // 3. Target is being tracked but there is no link between the source and target entity and target is in non-deleted state
            bool contextOperationRequired = !this.AttachBehavior &&
                                            (targetDescriptor == null ||
                                             (source != null && !this.IsContextTrackingLink(source, sourceProperty, target) && targetDescriptor.State != EntityStates.Deleted));

            if (contextOperationRequired)
            {
                // First give the user code a chance to handle Add operation.
                if (this.CollectionChanged != null)
                {
                    EntityCollectionChangedParams args = new EntityCollectionChangedParams(
                        this.Context,
                        source,
                        sourceProperty,
                        sourceEntitySet,
                        collection,
                        target,
                        targetEntitySet,
                        NotifyCollectionChangedAction.Add);

                    if (this.CollectionChanged(args))
                    {
                        return;
                    }
                }
            }

            // The user callback code could detach the source.
            if (source != null && this.IsDetachedOrDeletedFromContext(source))
            {
                throw new InvalidOperationException(Strings.DataBinding_BindingOperation_DetachedSource);
            }

            // Default implementation.
            targetDescriptor = this.Context.GetEntityDescriptor(target);

            if (source != null)
            {
                if (this.AttachBehavior)
                {
                    // If the target entity is not being currently tracked, we attach both the
                    // entity and the link between source and target entity.
                    if (targetDescriptor == null)
                    {
                        BindingUtils.ValidateEntitySetName(targetEntitySet, target);

                        this.Context.AttachTo(targetEntitySet, target);
                        this.Context.AttachLink(source, sourceProperty, target);
                    }
                    else
                    if (targetDescriptor.State != EntityStates.Deleted && !this.IsContextTrackingLink(source, sourceProperty, target))
                    {
                        // If the target is already being tracked, then we attach the link if it
                        // does not already exist between the source and target entities and the
                        // target entity is not already in Deleted state.
                        this.Context.AttachLink(source, sourceProperty, target);
                    }
                }
                else
                {
                    // The target will be added and link from source to target will get established in the code
                    // below. Note that if there is already target present then we just try to establish the link
                    // however, if the link is also already established then we don't do anything.
                    if (targetDescriptor == null)
                    {
                        // If the entity is not tracked, that means the entity needs to
                        // be added to the context. We need to call AddRelatedObject,
                        // which adds via the parent (for e.g. POST Customers(0)/Orders).
                        this.Context.AddRelatedObject(source, sourceProperty, target);
                    }
                    else
                    if (targetDescriptor.State != EntityStates.Deleted && !this.IsContextTrackingLink(source, sourceProperty, target))
                    {
                        // If the entity is already tracked, then we just add the link.
                        // However, we would not do it if the target entity is already
                        // in a Deleted state.
                        this.Context.AddLink(source, sourceProperty, target);
                    }
                }
            }
            else
            if (targetDescriptor == null)
            {
                // The source is null when the DataServiceCollection is the root collection.
                BindingUtils.ValidateEntitySetName(targetEntitySet, target);

                if (this.AttachBehavior)
                {
                    // Attach the target entity.
                    this.Context.AttachTo(targetEntitySet, target);
                }
                else
                {
                    // Add the target entity.
                    this.Context.AddObject(targetEntitySet, target);
                }
            }
        }