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); } }
private void EnumerateByReletedEntity(Action <Entity, EntitiesRelation> action) { if (EntityCtx.IsNotNull()) { Context.Relations.Where(r => r.Parent.EntityName == EntityCtx.Name).ToList().ForEach((r) => { List <EntityProperty> list = EntityCtx.Properties.Where(p => p.ReletedEntity.IsNotNull() && p.ReletedEntity.Relation.Name == r.Name).ToList(); if (list.Count() > 0) { list.ForEach((p) => { if (p.ReletedEntity.Related == Releted.List) { IEnumerable <Entity> entities = this[p.Name] as IEnumerable <Entity>; if (entities.IsNotNull()) { foreach (Entity e in entities) { action(e, r); } } } else { Entity e = this[p.Name] as Entity; if (e.IsNotNull()) { action(e, r); } } }); } else { EntityClass childEntity = Context.Entites.FirstOrDefault(t => t.Name == r.Child.EntityName); if (childEntity.IsNotNull()) { childEntity.Entities.Where(row => row.State != EntityState.Deleted && r.Child.Value(row).Equals(r.Parent.Value(this))).ToList().ForEach((row) => { action(row, r); }); } } }); } }
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); } }