public EntityReference SwitchFor(IEntity entity)
        {
            EntityReference reference = this;

            using (LogGroup logGroup = LogGroup.StartDebug("Switching the reference to the perspective of '" + entity.ShortTypeName + "' entity."))
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                LogWriter.Debug("Existing source entity type: " + Type1Name);
                LogWriter.Debug("Existing reference entity type: " + Type2Name);
                LogWriter.Debug("Existing source entity ID: " + Entity1ID.ToString());
                LogWriter.Debug("Existing reference entity ID: " + Entity2ID.ToString());
                LogWriter.Debug("Existing source property name: " + Property1Name.ToString());
                LogWriter.Debug("Existing reference property name: " + Property2Name.ToString());

                SwitchFor(entity.ShortTypeName, entity.ID);
            }

            return(reference);
        }
        public virtual EntityReference SwitchFor(string typeName, Guid id)
        {
            //EntityReference reference = (EntityReference)Clone();
            EntityReference reference = this;

            // TODO: Comment out logging to boost performance
            using (LogGroup logGroup = LogGroup.StartDebug("Switching reference data to the perspective of a '" + typeName + "' entity."))
            {
                if (typeName == null)
                {
                    throw new ArgumentNullException("typeName");
                }

                LogWriter.Debug("Existing source entity type: " + Type1Name);
                LogWriter.Debug("Existing reference entity type: " + Type2Name);
                LogWriter.Debug("Existing source entity ID: " + Entity1ID.ToString());
                LogWriter.Debug("Existing reference entity ID: " + Entity2ID.ToString());
                LogWriter.Debug("Existing source property name: " + Property1Name.ToString());
                LogWriter.Debug("Existing reference property name: " + Property2Name.ToString());

                if (EntitiesUtilities.MatchAlias(typeName, Type1Name) &&
                    Entity1ID == id)
                {
                    LogWriter.Debug("The reference is already in the perspective of the specified entity. No need to switch.");
                }
                else
                {
                    LogWriter.Debug("Switching to the perspective of entity type: " + typeName);

                    Guid entity1ID = Entity1ID;
                    Guid entity2ID = Entity2ID;

                    string type1Name = Type1Name;
                    string type2Name = Type2Name;

                    string property1Name = Property1Name;
                    string property2Name = Property2Name;

                    IEntity originalSourceEntity    = SourceEntity;
                    IEntity originalReferenceEntity = ReferenceEntity;

                    reference.SourceEntity    = originalReferenceEntity;
                    reference.ReferenceEntity = originalSourceEntity;

                    reference.Entity1ID = entity2ID;
                    reference.Entity2ID = entity1ID;

                    reference.Type1Name = type2Name;
                    reference.Type2Name = type1Name;

                    reference.Property1Name = property2Name;
                    reference.Property2Name = property1Name;


                    LogWriter.Debug("New source entity type: " + reference.Type1Name);
                    LogWriter.Debug("New reference entity type: " + reference.Type2Name);
                    LogWriter.Debug("New source entity ID: " + reference.Entity1ID.ToString());
                    LogWriter.Debug("New reference entity ID: " + reference.Entity2ID.ToString());
                    LogWriter.Debug("New source property name: " + reference.Property1Name.ToString());
                    LogWriter.Debug("New reference property name: " + reference.Property2Name.ToString());
                }
            }

            return(reference);
        }
        /// <summary>
        /// Gets the entity in the reference that wasn't provided. Hence the "other" entity.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public IEntity GetOtherEntity(IEntity entity)
        {
            IEntity otherEntity = null;

            //using (LogGroup logGroup = LogGroup.Start("Retrieving the entity from the reference that is not the one provided (ie. the other entity).", NLog.LogLevel.Debug))
            //{
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            if (referenceEntity == null)
            {
                throw new InvalidOperationException("The referenced entity is null.");
            }

            if (sourceEntity == null)
            {
                throw new InvalidOperationException("The source entity is null.");
            }

            if (referenceEntity.ID == entity.ID)
            {
                otherEntity = sourceEntity;
            }
            else if (sourceEntity.ID == entity.ID)
            {
                otherEntity = referenceEntity;
            }
            else
            {
                throw new InvalidOperationException("Can't get the other entity. Neither entity matches.\nParameter entity type: " + entity.ToString() + "\nParameter entity ID: " + entity.ID.ToString() + "\nEntity #1 type: " + Type1Name + "\nEntity #2 type: " + Type2Name + "\nProperty #1 type: " + Property1Name + "\nProperty #2 type: " + Property2Name + "\nEntity #1 ID: " + Entity1ID.ToString() + "\nEntity #2 ID: " + Entity2ID.ToString());
            }

            //LogWriter.Debug("Other entity type: " + otherEntity.GetType().ToString());
            //LogWriter.Debug("Other entity ID: " + otherEntity.ID.ToString());
            //}

            return(otherEntity);
        }