private void setEntityParent(EntityPersistence entityChild) { PropertyInfo[] properties = EntityReflection.Instance.Properties(entityChild.GetType(), typeof(EntityParentAttribute)); if (properties.Length == 1) { properties[0].SetValue(entityChild, this, null); } else if (properties.Length == 0) { throw new EntityParentException("Deve haver pelo menos um entity parent para o entity child " + entityChild.GetType().Name); } else { foreach (PropertyInfo prop in properties) { if (prop.PropertyType == this.GetType()) { prop.SetValue(entityChild, this, null); break; } } } }
private IList operationListChild(EntityPersistence entityChild, OperationListChild operation) { PropertyInfo[] properties = EntityReflection.Instance.Properties(this.GetType(), typeof(EntityListChildAttribute)); IList listEntity = null; if (properties.Length > 0) { foreach (PropertyInfo property in properties) { if (EntityReflection.Instance.GetTypeEntityChild(property) == entityChild.GetType()) { listEntity = property.GetValue(this, null) as IList; if (listEntity != null) { if (operation == OperationListChild.Add) { listEntity.Add(entityChild); setEntityParent(entityChild); } else if (operation == OperationListChild.Remove) { listEntity.Remove(entityChild); } else { for (int i = 0; i < listEntity.Count; i++) { if ((listEntity[i] as EntityPersistence).Id == entityChild.Id) { listEntity[i] = entityChild; break; } } } } } } } return(listEntity); }
public void RollBackEdit(EntityPersistence entityClone, EntityPersistence entity) { if (entityClone == null) { throw new ArgumentNullException("RollBack() não funciona com a variável entityClone == null !"); } if (entityClone.Id != entity.Id) { return; } PropertyInfo[] properties = entity.GetType().GetProperties(); object sourceValue; MethodInfo setAccessor; foreach (PropertyInfo property in properties) { setAccessor = property.GetSetMethod(); if (setAccessor != null) { if (property.PropertyType.Name != "IList") { EntityParentAttribute entityParentAttribute = EntityReflection.Instance.GetAttribute(property, typeof(EntityParentAttribute)) as EntityParentAttribute; if (entityParentAttribute == null) { sourceValue = property.GetValue(entityClone, null); property.SetValue(entity, sourceValue, null); } } else { IList entityClonePropertyValueList = property.GetValue(entityClone, null) as IList; IList entityPropertyValueList = property.GetValue(entity, null) as IList; if (entityClonePropertyValueList.Count == 0) { entityPropertyValueList.Clear(); } else { // Se foi adicionado um item novo, então remova-o ! Array tempEntityPropertyValueList = System.Array.CreateInstance(typeof(EntityPersistence), entityPropertyValueList.Count); entityPropertyValueList.CopyTo(tempEntityPropertyValueList, 0); foreach (EntityPersistence entityChild in tempEntityPropertyValueList) { if (entityChild.Id == -2) { entityPropertyValueList.Remove(entityChild); } } // Se foi alterado algum item da lista, então retorna os valores anteriores // Se foi excluído algum item, então readiciona este item bool execRollBack; foreach (EntityPersistence entityCloneChild in entityClonePropertyValueList) { execRollBack = false; foreach (EntityPersistence entityChild in entityPropertyValueList) { if (entityCloneChild.Id == entityChild.Id) { RollBackEdit(entityCloneChild, entityChild); execRollBack = true; break; } } if (!execRollBack) { entityPropertyValueList.Add(entityCloneChild); } } } } } } }