/// <summary>
        /// Creates all commands needed to perform a self-replace operation within this collection end point.
        /// </summary>
        /// <remarks>
        /// A self-replace operation of the form "customer.Orders[index] = customer.Orders[index]" needs two steps:
        /// <list type="bullet">
        ///   <item>customer.Orders.Touch() and</item>
        ///   <item>customer.Orders[index].Touch().</item>
        /// </list>
        /// No change notifications are sent for this operation.
        /// </remarks>
        public override ExpandedCommand ExpandToAllRelatedObjects()
        {
            var endPointOfRelatedObject = ModifiedEndPoint.GetEndPointWithOppositeDefinition <IRealObjectEndPoint> (OldRelatedObject);

            return(new ExpandedCommand(
                       this,
                       new RelationEndPointTouchCommand(endPointOfRelatedObject)));
        }
        /// <summary>
        /// Creates all commands needed to perform a bidirectional set-same operation on this <see cref="ObjectEndPoint"/>. One of the steps is
        /// this command, the other steps are the opposite commands on the new/old related objects.
        /// </summary>
        /// <remarks>
        /// A same-set operation of the form "order.OrderTicket = order.OrderTicket" needs two steps:
        /// <list type="bullet">
        ///   <item>order.Touch() and</item>
        ///   <item>order.OrderTicket.Touch.</item>
        /// </list>
        /// No change notifications are sent for this operation.
        /// </remarks>
        public override ExpandedCommand ExpandToAllRelatedObjects()
        {
            var oppositeEndPointDefinition = ModifiedEndPoint.Definition.GetOppositeEndPointDefinition();

            if (oppositeEndPointDefinition.IsAnonymous)
            {
                return(new ExpandedCommand(this));
            }
            else
            {
                var oppositeEndPoint = ModifiedEndPoint.GetEndPointWithOppositeDefinition <IRelationEndPoint> (NewRelatedObject);
                return(new ExpandedCommand(this, new RelationEndPointTouchCommand(oppositeEndPoint)));
            }
        }
        /// <summary>
        /// Creates all commands needed to perform a bidirectional collection replace operation within this collection end point.
        /// </summary>
        /// <remarks>
        /// A replace operation of the form "customer.Orders = newOrders" involves the following steps:
        /// <list type="bullet">
        ///   <item>for each oldOrder the old collection (Orders) that's not in the new one: oldOrder.Customer = <see langword="null" />,</item>
        ///   <item>for each newOrder in the new collection (newOrders) that's not in the old one: newOrder.Customer.Orders.Remove (newOrder),</item>
        ///   <item>for each newOrder in the new collection (newOrders) that's not in the old one: newOrder.Customer = customer,</item>
        ///   <item>customer.Orders = newOrders.</item>
        /// </list>
        /// </remarks>
        public override ExpandedCommand ExpandToAllRelatedObjects()
        {
            var domainObjectOfCollectionEndPoint = base.ModifiedEndPoint.GetDomainObject();

            var commandsForRemoved = from oldObject in RemovedObjects
                                     let endPoint = ModifiedEndPoint.GetEndPointWithOppositeDefinition <IRealObjectEndPoint> (oldObject)
                                                    select endPoint.CreateRemoveCommand(domainObjectOfCollectionEndPoint); // oldOrder.Customer = null

            var commandsForAdded = from newObject in AddedObjects
                                   let endPointOfNewObject = ModifiedEndPoint.GetEndPointWithOppositeDefinition <IRealObjectEndPoint> (newObject)                                                                                          // newOrder.Customer
                                                             let oldRelatedOfNewObject = endPointOfNewObject.GetOppositeObject()                                                                                                           // newOrder.Customer
                                                                                         let endPointOfOldRelatedOfNewObject = endPointOfNewObject.GetEndPointWithOppositeDefinition <ICollectionEndPoint> (oldRelatedOfNewObject)         // newOrder.Customer.Orders
                                                                                                                               let removeCommand = endPointOfOldRelatedOfNewObject.CreateRemoveCommand(newObject)                          // newOrder.Customer.Orders.Remove (newOrder)
                                                                                                                                                   let setCommand = endPointOfNewObject.CreateSetCommand(domainObjectOfCollectionEndPoint) // newOrder.Customer = customer
                                                                                                                                                                    from command in new[] { removeCommand, setCommand }
            select command;

            return(new ExpandedCommand(commandsForRemoved).CombineWith(commandsForAdded).CombineWith(this));
        }
        /// <summary>
        /// Creates all commands needed to perform a bidirectional replace operation within this collection end point.
        /// </summary>
        /// <remarks>
        /// A replace operation of the form "customer.Orders[index] = newOrder" needs four steps:
        /// <list type="bullet">
        ///   <item>customer.Order[index].Customer = null,</item>
        ///   <item>newOrder.Customer = customer,</item>
        ///   <item>customer.Orders[index] = newOrder,</item>
        ///   <item>oldCustomer.Orders.Remove (insertedOrder) - with oldCustomer being the old customer of the new order (if non-null).</item>
        /// </list>
        /// </remarks>
        public override ExpandedCommand ExpandToAllRelatedObjects()
        {
            // the end point that will be linked to the collection end point after the operation
            var endPointOfNewObject = ModifiedEndPoint.GetEndPointWithOppositeDefinition <IRealObjectEndPoint> (NewRelatedObject);
            // the end point that was linked to the collection end point before the operation
            var endPointOfOldObject = ModifiedEndPoint.GetEndPointWithOppositeDefinition <IRealObjectEndPoint> (OldRelatedObject);
            // the object that was linked to the new related object before the operation
            var oldRelatedObjectOfNewObject = endPointOfNewObject.GetOppositeObject();
            // the end point that was linked to the new related object before the operation
            var oldRelatedEndPointOfNewObject = endPointOfNewObject.GetEndPointWithOppositeDefinition <ICollectionEndPoint> (oldRelatedObjectOfNewObject);

            return(new ExpandedCommand(
                       // customer.Order[index].Customer = null
                       endPointOfOldObject.CreateRemoveCommand(ModifiedEndPoint.GetDomainObject()),
                       // newOrder.Customer = customer
                       endPointOfNewObject.CreateSetCommand(ModifiedEndPoint.GetDomainObject()),
                       // customer.Orders[index] = newOrder
                       this,
                       // oldCustomer.Orders.Remove (insertedOrder)
                       oldRelatedEndPointOfNewObject.CreateRemoveCommand(NewRelatedObject)));
        }