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; } } } }
/// <summary> /// Exclui um Entity baseado em seu Id. /// </summary> /// <param name="id"></param> public virtual void Delete(object id) { T entity = default(T); try { if (session == null || !session.IsOpen) { session = NHibernateManager.Instance.GetSession(typeof(T).Name); } transaction = session.BeginTransaction(); entity = session.Get <T>(id); if (entity == null) { throw new EntityNotFoundException(); } session.Delete(entity); transaction.Commit(); } catch (Exception E) { transaction.Rollback(); throw new Exception(E.Message); } finally { NHibernateManager.Instance.CloseSession(typeof(T).Name); } }
public virtual object Clone() { EntityPersistence entityClone = MemberwiseClone() as EntityPersistence; PropertyInfo[] properties = this.GetType().GetProperties(); foreach (PropertyInfo property in properties) { if (EntityReflection.Instance.GetAttribute(property, typeof(NotClonablePropertyAttribute)) != null) { continue; } if (property.PropertyType.IsSubclassOf(typeof(EntityPersistence))) { PropertyInvisibleAttribute propertyInvisibleAttribute = EntityReflection.Instance.GetAttribute(property, typeof(PropertyInvisibleAttribute)) as PropertyInvisibleAttribute; if (propertyInvisibleAttribute == null) { EntityPersistence entityPropertyValue = property.GetValue(this, null) as EntityPersistence; if (entityPropertyValue != null) { property.SetValue(entityClone, entityPropertyValue.Clone(), null); } } } else if (property.PropertyType.Name == "IList") { IList entityPropertyValueList = property.GetValue(this, null) as IList; if (entityPropertyValueList != null) { List <EntityPersistence> listTarget = new List <EntityPersistence>(entityPropertyValueList.Count); property.SetValue(entityClone, listTarget, null); foreach (EntityPersistence objeto in entityPropertyValueList) { listTarget.Add((objeto as EntityPersistence).Clone() as EntityPersistence); } } } } return(entityClone); }
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); }
/// <summary> /// Retorna um Entity baseado em seu Id. /// </summary> /// <param name="id">Id do entity mapeado.</param> /// <returns></returns> public virtual IEntityPersistence Read(int id) { T entity = default(T); try { if (session == null || !session.IsOpen) { session = NHibernateManager.Instance.GetSession(typeof(T).Name); } entity = session.Get <T>(id); } finally { NHibernateManager.Instance.CloseSession(typeof(T).Name); } return(entity); }
/// <summary> /// Exlui uma lista de entities de uma só vez. /// </summary> public static void Delete(EntityPersistence entity, IList <T> listSource) { if (entity == null) { throw new ArgumentNullException("O parâmetro entity não pode ser nulo !"); } if (listSource == null) { throw new ArgumentNullException("O parâmetro listSource não pode ser nulo !"); } for (int i = 0; i < listSource.Count; i++) { if (entity.Id == (listSource[i] as EntityPersistence).Id) { listSource.Remove(listSource[i]); } } }
public virtual void CreateNewEntity() { entity = EntityReflection.Instance.GetNewEntity(typeof(T)) as T; entityClone = entity.Clone() as EntityPersistence; }
protected virtual void save(EntityPersistence entity) { session.SaveOrUpdate(entity); }
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); } } } } } } }
public void RollBackEdit(EntityPersistence entity) { RollBackEdit(entityClone, entity); }
public ListEntityChild(EntityPersistence parent) { this.parent = parent; }
public virtual IList RemoveChild(EntityPersistence entityChild) { return(operationListChild(entityChild, OperationListChild.Remove)); }
public virtual IList SetEntityUpdatedInList(EntityPersistence entityChild) { return(operationListChild(entityChild, OperationListChild.Set)); }
public virtual IList AddChild(EntityPersistence entityChild) { return(operationListChild(entityChild, OperationListChild.Add)); }