//This method below in case we want to implement eager loading. private bool CheckAndRemoveDupes(EntityMap entityMap, EntityAccessor accessor, object entity, Dictionary <object, object> resultStore) { if (entityMap.PrimaryKey != null) { var k = entityMap.PrimaryKey.Keys[0].Key; PropertyAccessor propAccessor = accessor.GetPropertyAccessor(k.PropertyName); var keyVal = propAccessor.GetMethod(entity); object storedEntity; if (resultStore.TryGetValue(keyVal, out storedEntity)) { //we have duplicate due to eager-loaded relations foreach (var rel in entityMap.Relations) { if (!rel.LazyLoad) { var rPropAccessor = accessor.GetPropertyAccessor(rel.PropertyName); var collection = rPropAccessor.GetMethod(storedEntity); var newCollection = rPropAccessor.GetMethod(entity); rPropAccessor.PropertyType.GetMethod("AddRange").Invoke(collection, new[] { newCollection }); } } } else { resultStore.Add(keyVal, entity); } return(true); } return(false); }
public override void Serialize(IDatabaseSettings settings, EntitySerializer serializer, Relation rel, object instanceEntity, PropertyAccessor pInfo, EntityMap entityMap, EntityAccessor entityAccessor, DbDataReader dbReader) { var relMap = settings.Map.GetEntityMap(rel.ReferenceEntityName); var relCols = new List <string>(); var session = serializer.SessionCreator(); int iteration = 0; int recursion = 0; var relQueryMap = new TableQueryMap(relMap.FullName, ref recursion, ref iteration); QueryBuilder q = new QueryBuilder(session, relCols); relQueryMap.LoadColumns(relMap, session, q, relCols); var prop = entityMap.FirstOrDefault(c => c.ColumnName == rel.ColumnName); if (prop == null) { throw new GoliathDataException(string.Format("{0}: Reference {1} does not have matching property.", entityMap.FullName, rel.PropertyName)); } var valAccessor = entityAccessor.GetPropertyAccessor(prop.PropertyName); var val = valAccessor.GetMethod(instanceEntity); var queryBuilder = q.From(relMap.TableName, relQueryMap.Prefix) .Where(rel.ReferenceColumn).EqualToValue(val); IProxyHydrator hydrator = new ProxyHydrator(queryBuilder as QueryBuilder, pInfo.PropertyType, relMap, serializer, session); ProxyBuilder pbuilder = new ProxyBuilder(); var proxyType = pbuilder.CreateProxyType(pInfo.PropertyType, relMap); object proxyobj = Activator.CreateInstance(proxyType, new object[] { pInfo.PropertyType, hydrator }); pInfo.SetMethod(instanceEntity, proxyobj); }
/// <summary> /// Serializes the specified settings. /// </summary> /// <param name="settings">The settings.</param> /// <param name="serializer">The serializer.</param> /// <param name="rel">The rel.</param> /// <param name="instanceEntity">The instance entity.</param> /// <param name="pInfo">The p info.</param> /// <param name="entityMap">The entity map.</param> /// <param name="entityAccessor">The entity accessor.</param> /// <param name="columns">The columns.</param> /// <param name="dbReader">The db reader.</param> /// <exception cref="MappingException"></exception> public override void Serialize(IDatabaseSettings settings, EntitySerializer serializer, Relation rel, object instanceEntity, PropertyAccessor pInfo, EntityMap entityMap, EntityAccessor entityAccessor, DbDataReader dbReader) { var propType = pInfo.PropertyType; var relEntMap = settings.Map.GetEntityMap(rel.ReferenceEntityName); Type refEntityType = propType.GetGenericArguments().FirstOrDefault(); var prop = entityMap.FirstOrDefault(c => rel.ColumnName.Equals(c.ColumnName)); if (prop == null) { throw new GoliathDataException(string.Format("{0}: Reference {1} does not have matching property.", entityMap.FullName, rel.PropertyName)); } if (refEntityType == null) { throw new MappingException(string.Format("property type mismatch: {0} should be IList<T>", rel.PropertyName)); } if (propType == typeof(IList <>).MakeGenericType(new Type[] { refEntityType })) { var valAccessor = entityAccessor.GetPropertyAccessor(prop.PropertyName); var val = valAccessor.GetMethod(instanceEntity); if (val != null) { var relCols = new List <string>(); var session = serializer.SessionCreator(); int iteration = 0; int recursion = 0; var relQueryMap = new TableQueryMap(relEntMap.FullName, ref recursion, ref iteration); QueryBuilder q = new QueryBuilder(session, relCols); relQueryMap.LoadColumns(relEntMap, session, q, relCols, true); q.QueryMap = relQueryMap; var queryBuilder = q.From(relEntMap.TableName, relQueryMap.Prefix) .Where(rel.ReferenceColumn).EqualToValue(val) as QueryBuilder; var collectionType = typeof(Collections.LazyList <>).MakeGenericType(new Type[] { refEntityType }); var lazyCol = Activator.CreateInstance(collectionType, queryBuilder, relEntMap, serializer, session); pInfo.SetMethod(instanceEntity, lazyCol); } else { var collectionType = typeof(List <>).MakeGenericType(new Type[] { refEntityType }); pInfo.SetMethod(instanceEntity, Activator.CreateInstance(collectionType)); } } else { throw new MappingException(string.Format("property type mismatch: {0} should be IList<T>", rel.PropertyName)); } }