private void FillChangeTrackerValues(IEntity entity, ITransaction tx, IEntityContext entityContext) { if (entity.Status == EntityStatus.New || entity.Status == EntityStatus.Deleted) { return; } EntityInfo entityInfo = CacheManager.GetEntityInfo(entity); while (entityInfo != null) { ITypeFieldValueList values = ExtractCurrentRowValues(entity, entityInfo.EntityType, tx); entityContext.ChangeTracker.AddFields(values.FieldValues); ICollection <IRelation> dbRelations = entityInfo.Relations; foreach (IRelation relation in dbRelations) { ICollection <IReadOnlyEntity> children = ReadRelationChildrenFromDb(entity, entityInfo.EntityType, tx, relation); foreach (IReadOnlyEntity childEntity in children) { ITypeFieldValueList valueTypeList = OperationUtils.ExtractRelationKeyValues(childEntity, relation); if (valueTypeList != null) { entityContext.ChangeTracker.AddChildEntityKey(valueTypeList); } } } entityInfo = entityInfo.SuperEntityInfo; } }
protected static ICollection <ITypeFieldValueList> GetChildEntityValueList(IEntity parentEntity, bool takeDeleted) { EntityInfo entityInfo = CacheManager.GetEntityInfo(parentEntity); ICollection <ITypeFieldValueList> existingEntityChildRelations = new List <ITypeFieldValueList>(); while (entityInfo != null) { ICollection <IRelation> typeRelations = entityInfo.Relations; foreach (IRelation typeRelation in typeRelations) { if (typeRelation.ReverseRelationship) { continue; } if (IsProxyObject(parentEntity, typeRelation)) { continue; } ICollection <IEntity> childEntities = OperationUtils.GetRelationEntities(parentEntity, typeRelation); foreach (IEntity childEntity in childEntities) { if (parentEntity.Status == EntityStatus.Deleted && typeRelation.DeleteRule == ReferentialRuleType.Cascade) { childEntity.Status = EntityStatus.Deleted; } if (childEntity.Status == EntityStatus.Deleted && !takeDeleted) { continue; } ITypeFieldValueList childKeyValueList = OperationUtils.ExtractRelationKeyValues(childEntity, typeRelation); if (childKeyValueList != null) { existingEntityChildRelations.Add(childKeyValueList); } } } entityInfo = entityInfo.SuperEntityInfo; } return(existingEntityChildRelations); }
public void LoadChildrenFromRelation(IReadOnlyEntity parentRoEntity, Type entityType, ITransaction tx , IRelation relation, bool lazy) { EntityInfo entityInfo = CacheManager.GetEntityInfo(entityType); IEntityContext entityContext = parentRoEntity.Context; PropertyInfo property = entityInfo.GetProperty(relation.AttributeName); Object value = ReflectionUtils.GetValue(property, parentRoEntity); if (!lazy && relation.FetchStrategy == FetchStrategy.Lazy) { CreateProxy(parentRoEntity, entityType, tx, relation, value, property); return; } ICollection <IReadOnlyEntity> children = ReadRelationChildrenFromDb(parentRoEntity, entityType, tx, relation); if (entityContext != null && !relation.ReverseRelationship) { foreach (IReadOnlyEntity childEntity in children) { ITypeFieldValueList valueTypeList = OperationUtils.ExtractRelationKeyValues(childEntity, relation); if (valueTypeList != null) { entityContext.ChangeTracker.AddChildEntityKey(valueTypeList); } } } if ((value == null || ProxyUtil.IsProxyType(value.GetType())) && ReflectionUtils.IsImplementInterface(property.PropertyType, typeof(ICollection <>))) { Type propertyType = property.PropertyType; if (propertyType.IsInterface) { Type generic = propertyType.GetGenericArguments()[0]; propertyType = typeof(List <>).MakeGenericType(new Type[] { generic }); } value = Activator.CreateInstance(propertyType); IList genCollection = (IList)value; foreach (IReadOnlyEntity serverRoDbClass in children) { genCollection.Add(serverRoDbClass); } ReflectionUtils.SetValue(property, parentRoEntity, genCollection); } else if (value != null && ReflectionUtils.IsImplementInterface(property.PropertyType, typeof(ICollection <>))) { IList genCollection = (IList)value; foreach (IReadOnlyEntity serverRoDbClass in children) { genCollection.Add(serverRoDbClass); } } else { IEnumerator <IReadOnlyEntity> childEnumarator = children.GetEnumerator(); if (childEnumarator.MoveNext()) { IReadOnlyEntity singleRoDbClass = childEnumarator.Current; if (property.PropertyType.IsAssignableFrom(singleRoDbClass.GetType())) { ReflectionUtils.SetValue(property, parentRoEntity, singleRoDbClass); } else { string message = singleRoDbClass.GetType().FullName + " is not matching the getter " + property.Name; Logger.GetLogger(Config.LoggerName).Fatal(message); throw new NoSetterFoundToSetChildObjectListException(message); } } } }