public void Add(T entity)
        {
            if (entity is IOgmConnection && !IsInverse && (entity as IOgmConnection).Destination == null)
            {
                throw new InvalidOperationException($"'{nameof(IOgmConnection.Destination)}' property must be set.");
            }
            if (entity is IOgmConnection && IsInverse && (entity as IOgmConnection).Source == null)
            {
                throw new InvalidOperationException($"'{nameof(IOgmConnection.Source)}' property must be set.");
            }

            long version = DateTimeOffset.Now.ToUnixTimeMilliseconds();

            IOgmConnection conn = (entity as IOgmConnection) ??
                                  new OgmConnection()
            {
                Order = Connections.Count, SourcePropertyName = SourceProperty?.Name ?? "", DestinationPropertyName = DestinationProperty?.Name ?? "", Version = version
            };

            Connections.Add(conn);

            if (conn is OgmConnection)
            {
                conn.Source      = IsInverse ? entity : ProxyAccessor as IOgmEntity;
                conn.Destination = IsInverse ? ProxyAccessor as IOgmEntity : entity;
            }

            Wire(conn);
        }
Exemple #2
0
        public void Set(IOgmEntity entity)
        {
            if (entity is IOgmConnection && !IsInverse && (entity as IOgmConnection).Destination == null)
            {
                throw new InvalidOperationException($"'{nameof(IOgmConnection.Destination)}' property must be set.");
            }
            if (entity is IOgmConnection && IsInverse && (entity as IOgmConnection).Source == null)
            {
                throw new InvalidOperationException($"'{nameof(IOgmConnection.Source)}' property must be set.");
            }

            DiscardEffect();

            long version = DateTimeOffset.Now.ToUnixTimeMilliseconds();

            IOgmConnection tmp = Connection;

            Connection = null;
            Unwire(tmp);

            Connection = (entity as IOgmConnection) ?? new OgmConnection()
            {
                Order = 0, SourcePropertyName = SourceProperty?.Name ?? "", DestinationPropertyName = DestinationProperty?.Name ?? "", Version = version
            };

            if (Connection is OgmConnection)
            {
                Connection.Source      = IsInverse ? entity : ProxyAccessor as IOgmEntity;
                Connection.Destination = IsInverse ? ProxyAccessor as IOgmEntity : entity;
            }

            Wire(Connection);
        }
Exemple #3
0
        public ConnectionEntity(IOgmConnection entity, long sourceId, long destinationId, long version, bool allowNull = true, IEnumerable <string> excludePorperties = null)
            : base(entity, allowNull, excludePorperties)
        {
            SourceId      = sourceId;
            DestinationId = destinationId;

            Version = version;
        }
        protected void DiscardEffect(IOgmEntity entity)
        {
            IOgmConnection conn = GetConnection(entity);

            if (conn != null)
            {
                Context.ChangeTracker.Track(new EntityChangeRelDeletion(conn));
            }
        }
Exemple #5
0
        public EntityChangeRelCreation(IOgmConnection entity, IOgmEntity source, IOgmEntity destination) : base(entity)
        {
            Source      = source ?? throw new ArgumentNullException(nameof(source));
            Destination = destination ?? throw new ArgumentNullException(nameof(destination));

            if (source is IOgmConnection)
            {
                throw new ArgumentException("A Connection cannot be a source of a connction.", nameof(source));
            }
            if (destination is IOgmConnection)
            {
                throw new ArgumentException("A Connection cannot be a destination of a connction.", nameof(destination));
            }
        }
Exemple #6
0
 protected void Unwire(IOgmConnection connection)
 {
     if (connection != null)
     {
         if (IsInverse)
         {
             if (SourceProperty != null)
             {
                 using (ManagerAccess.Manager.ScopeOMnG())
                 {
                     if (Context.TypesManager.IsGraphEntityCollection(SourceProperty.PropertyType))
                     {
                         object coll = ObjectExtensions.Configuration.Get(SourceProperty, connection.Source);
                         if (coll != null)
                         {
                             coll.GetType().GetMethod(nameof(ICollection <int> .Remove)).Invoke(coll, new[] { connection });
                         }
                     }
                     else
                     {
                         ObjectExtensions.Configuration.Set(SourceProperty, connection.Source, null);
                     }
                 }
             }
         }
         else
         {
             if (DestinationProperty != null)
             {
                 using (ManagerAccess.Manager.ScopeOMnG())
                 {
                     if (Context.TypesManager.IsGraphEntityCollection(DestinationProperty.PropertyType))
                     {
                         object coll = ObjectExtensions.Configuration.Get(DestinationProperty, connection.Destination);
                         if (coll != null)
                         {
                             coll.GetType().GetMethod(nameof(ICollection <int> .Remove)).Invoke(coll, new[] { connection });
                         }
                     }
                     else
                     {
                         ObjectExtensions.Configuration.Set(DestinationProperty, connection.Destination, null);
                     }
                 }
             }
         }
     }
 }
        public T this[int index]
        {
            get
            {
                return((IsInverse ? Connections[index].Source : Connections[index].Destination) as T);
            }
            set
            {
                IOgmEntity entity = value;
                if (entity is IOgmConnection && !IsInverse && (entity as IOgmConnection).Destination == null)
                {
                    throw new InvalidOperationException($"'{nameof(IOgmConnection.Destination)}' property must be set.");
                }
                if (entity is IOgmConnection && IsInverse && (entity as IOgmConnection).Source == null)
                {
                    throw new InvalidOperationException($"'{nameof(IOgmConnection.Source)}' property must be set.");
                }

                IOgmConnection tmp = Connections[index];
                Connections[index] = null;
                Unwire(tmp);

                long version = DateTimeOffset.Now.ToUnixTimeMilliseconds();

                IOgmConnection conn = (entity as IOgmConnection) ??
                                      new OgmConnection()
                {
                    Order = Connections.Count, SourcePropertyName = SourceProperty?.Name ?? "", DestinationPropertyName = DestinationProperty?.Name ?? "", Version = version
                };

                Connections[index] = conn;

                if (conn is OgmConnection)
                {
                    conn.Source      = IsInverse ? entity : ProxyAccessor as IOgmEntity;
                    conn.Destination = IsInverse ? ProxyAccessor as IOgmEntity : entity;
                }

                Wire(conn);

                for (int i = index; i < Connections.Count; i++)
                {
                    Connections[i].Order = i;
                }
            }
        }
        public bool Remove(T entity)
        {
            if (entity is IOgmConnection && !IsInverse && (entity as IOgmConnection).Destination == null)
            {
                throw new InvalidOperationException($"'{nameof(IOgmConnection.Destination)}' property must be set.");
            }
            if (entity is IOgmConnection && IsInverse && (entity as IOgmConnection).Source == null)
            {
                throw new InvalidOperationException($"'{nameof(IOgmConnection.Source)}' property must be set.");
            }

            DiscardEffect(entity);

            long version = DateTimeOffset.Now.ToUnixTimeMilliseconds();

            IOgmConnection conn = GetConnection(entity);

            bool result = Connections.Remove(conn);

            Unwire(conn);

            return(result);
        }
Exemple #9
0
 public EntityChangeRelUpdate(IOgmConnection entity, PropertyInfo property, object oldValue, object currentValue) : base(entity, property, oldValue, currentValue)
 {
 }
Exemple #10
0
        protected void Wire(IOgmConnection connection)
        {
            if (connection != null)
            {
                using (ManagerAccess.Manager.ScopeOMnG())
                {
                    if (IsInverse)
                    {
                        if (Context.TypesManager.IsGraphEntityCollection(DestinationProperty.PropertyType))
                        {
                            object coll = ObjectExtensions.Configuration.Get(DestinationProperty, connection.Destination);
                            coll.GetType().GetMethod(nameof(ICollection <int> .Add)).Invoke(coll, new[] { connection is OgmConnection ? connection.Destination : connection });
                        }
                        else
                        {
                            ObjectExtensions.Configuration.Set(DestinationProperty, ProxyAccessor.DynProxyGetTarget(), connection is OgmConnection ? connection.Destination : connection);
                        }

                        if (SourceProperty != null)
                        {
                            if (Context.TypesManager.IsGraphEntityCollection(SourceProperty.PropertyType))
                            {
                                object coll = ObjectExtensions.Configuration.Get(SourceProperty, connection.Source);
                                if (coll == null)
                                {
                                    if (SourceProperty.CanWrite)
                                    {
                                        coll = Activator.CreateInstance(SourceProperty.PropertyType);
                                        ObjectExtensions.Configuration.Set(SourceProperty, connection.Source, coll);
                                    }
                                    else
                                    {
                                        throw new InvalidOperationException($"Unable to set property {SourceProperty.ReflectedType.FullName}.{SourceProperty.Name}");
                                    }
                                }
                                coll.GetType().GetMethod(nameof(ICollection <int> .Add)).Invoke(coll, new[] { connection.Destination });
                            }
                            else
                            {
                                ObjectExtensions.Configuration.Set(SourceProperty, connection.Source, connection.Destination);
                            }
                        }
                    }
                    else
                    {
                        if (Context.TypesManager.IsGraphEntityCollection(SourceProperty.PropertyType))
                        {
                            object coll = ObjectExtensions.Configuration.Get(SourceProperty, connection.Source);
                            coll.GetType().GetMethod(nameof(ICollection <int> .Add)).Invoke(coll, new[] { connection is OgmConnection ? connection.Destination : connection });
                        }
                        else
                        {
                            ObjectExtensions.Configuration.Set(SourceProperty, ProxyAccessor.DynProxyGetTarget(), connection is OgmConnection ? connection.Destination : connection);
                        }

                        if (DestinationProperty != null)
                        {
                            if (Context.TypesManager.IsGraphEntityCollection(DestinationProperty.PropertyType))
                            {
                                object coll = ObjectExtensions.Configuration.Get(DestinationProperty, connection.Destination);
                                if (coll == null)
                                {
                                    if (DestinationProperty.CanWrite)
                                    {
                                        coll = Activator.CreateInstance(DestinationProperty.PropertyType);
                                        ObjectExtensions.Configuration.Set(DestinationProperty, connection.Destination, coll);
                                    }
                                    else
                                    {
                                        throw new InvalidOperationException($"Unable to set property {DestinationProperty.ReflectedType.FullName}.{DestinationProperty.Name}");
                                    }
                                }
                                coll.GetType().GetMethod(nameof(ICollection <int> .Add)).Invoke(coll, new[] { connection.Source });
                            }
                            else
                            {
                                ObjectExtensions.Configuration.Set(DestinationProperty, connection.Destination, connection.Source);
                            }
                        }
                    }
                }
            }
        }
Exemple #11
0
 public EntityChangeRelDeletion(IOgmConnection entity) : base(entity)
 {
 }