コード例 #1
0
 protected override void InsertItem(int index, T item)
 {
     if (Releted.IsNull())
     {
         if (item.GetType() == typeof(T) &&
             !item.GetType().Assembly.FullName.Contains("DynamicProxy"))
         {
             throw new ModelException("Please use CreateInstance function CreateInstance or AddNew to create proper object entity");
         }
         if (EntityCtx.IsNotNull())
         {
             if (item.Session.IsNullOrEmpty())
             {
                 item.Validate((v) => { return(v.RealTimeValidation); },
                               (v) => { throw new ValidationException(v.ErrrorMessage); });
             }
             if (EntityCtx.Entities.FirstOrDefault(e => e.Equals(item)).IsNotNull())
             {
                 throw new ValidationException("You can't add twice the same object[" + typeof(T).Name + "] to collection");
             }
             EntityCtx.MarkedAsModified();
         }
         base.InsertItem(index, item);
         if (EntityCtx.Synchronizing)
         {
             foreach (string entityName in EntityCtx.SynchronizedCollection.Keys)
             {
                 EntityClass releted = Context.Entites.FirstOrDefault(e => e.Name == entityName);
                 Debug.Assert(releted.IsNotNull(), "Internal error");
                 if (releted.IsNotNull())
                 {
                     foreach (Entity e in releted.Entities)
                     {
                         foreach (string propertyName in EntityCtx.SynchronizedCollection[entityName])
                         {
                             object o = e[propertyName];
                         }
                     }
                 }
             }
         }
     }
     else
     {
         if (!CopyFromMainCollection)
         {
             if (EntityCtx.IsNotNull())
             {
                 IList list = EntityCtx.Entities as IList;
                 if (list.IsNotNull())
                 {
                     EntityCtx.Synchronizing = false;
                     list.Add(item);
                     EntityCtx.Synchronizing = true;
                 }
             }
         }
         base.InsertItem(index, item);
     }
 }
コード例 #2
0
ファイル: Entity.cs プロジェクト: gkudel/mvc-for-net
        public void AcceptChanges()
        {
            switch (State)
            {
            case EntityState.Modified:
            case EntityState.Added: State = EntityState.Unchanged;
                break;

            case EntityState.Deleted: IList iList = EntityCtx.Entities as IList;
                if (iList.IsNotNull())
                {
                    iList.Remove(this);
                }
                break;
            }
            EntityCtx.MarkedAsModified();
        }
コード例 #3
0
        protected override void RemoveItem(int index)
        {
            if (Releted.IsNull())
            {
                Entity obj = null;
                if (AllowRemove && index < Count)
                {
                    obj = base[index];
                    if (obj.IsFrozen)
                    {
                        throw new SecurityException("Security Exception. You try to modified object for which you don't have an accesss");
                    }
                    switch (obj.State)
                    {
                    case EntityState.Modified:
                    case EntityState.Unchanged: obj.State = EntityState.Deleted;
                        break;

                    case EntityState.Added: obj.State = EntityState.Deleted;
                        base.RemoveItem(index);
                        break;

                    case EntityState.Deleted: base.RemoveItem(index);
                        break;
                    }
                }
                else
                {
                    throw new InvalidOperationException();
                }
                if (EntityCtx.IsNotNull())
                {
                    EntityCtx.MarkedAsModified();
                    foreach (string entityName in EntityCtx.SynchronizedCollection.Keys)
                    {
                        EntityClass releted = Context.Entites.FirstOrDefault(e => e.Name == entityName);
                        Debug.Assert(releted.IsNotNull(), "Internal error");
                        if (releted.IsNotNull())
                        {
                            foreach (Entity e in releted.Entities)
                            {
                                foreach (string propertyName in EntityCtx.SynchronizedCollection[entityName])
                                {
                                    object o = e[propertyName];
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                if (!CopyFromMainCollection)
                {
                    if (EntityCtx.IsNotNull())
                    {
                        IList list = EntityCtx.Entities as IList;
                        if (list.IsNotNull())
                        {
                            if (index < Count)
                            {
                                Entity e = base[index];
                                if (list.Contains(e))
                                {
                                    list.Remove(e);
                                }
                            }
                        }
                    }
                }
                base.RemoveItem(index);
            }
        }