Example #1
0
 private protected void ExecuteAction(RelationshipAction action)
 {
     if (Parent is OGMImpl || (Parent is DynamicEntity && ((DynamicEntity)Parent).ShouldExecute))
     {
         DbTransaction?.Register(action);
     }
 }
Example #2
0
        private protected void ExecuteAction(ClearRelationshipsAction action)
        {
            if (DbTransaction != null)
            {
                DbTransaction?.Register(action);
            }
            else
            {
                foreach (Property property in GetEntity().Properties.Where(item => item.PropertyType != PropertyType.Attribute))
                {
                    if (property.ForeignProperty == null)
                    {
                        continue;
                    }

                    IEnumerable <OGM> instances;
                    object            value = property.GetValue(this, null);
                    if (property.PropertyType == PropertyType.Lookup)
                    {
                        instances = new OGM[] { (OGM)value }
                    }
                    ;
                    else
                    {
                        instances = (IEnumerable <OGM>)value;
                    }

                    foreach (OGM instance in instances)
                    {
                        if (property.ForeignProperty.PropertyType == PropertyType.Lookup)
                        {
                            property.ForeignProperty.SetValue(instance, null);
                        }
                        else
                        {
                            EntityCollectionBase collection = (EntityCollectionBase)property.ForeignProperty.GetValue(instance, null);
                            action.ExecuteInMemory(collection);
                        }
                    }
                    property.SetValue(this, null);
                }
            }
        }
        public EntityCollectionBase(OGM parent, Property property)
        {
            if (property.Relationship is null)
            {
                throw new NotSupportedException("The property is not a relationship property.");
            }

            Parent        = parent;
            Relationship  = property.Relationship;
            Direction     = property.Direction;
            DbTransaction = Transaction.Current;

            switch (Direction)
            {
            case DirectionEnum.In:
                ParentEntity    = Relationship.InEntity;
                ParentProperty  = Relationship.InProperty;
                ForeignEntity   = Relationship.OutEntity;
                ForeignProperty = Relationship.OutProperty;
                break;

            case DirectionEnum.Out:
                ParentEntity    = Relationship.OutEntity;
                ParentProperty  = Relationship.OutProperty;
                ForeignEntity   = Relationship.InEntity;
                ForeignProperty = Relationship.InProperty;
                break;

            case DirectionEnum.None:
                throw new NotSupportedException("You cannot initialize a collection without a direction.");

            default:
                throw new NotImplementedException();
            }

            IsLoaded = false;

            if (parent is OGMImpl || (parent is DynamicEntity && ((DynamicEntity)parent).ShouldExecute))
            {
                DbTransaction?.Register(this);
            }
        }