protected override void MapInternal(Reflection.InitContext initContext, IMapDataSource source, object sourceObject, IMapDataDestination dest, object destObject, params object[] parameters) { FullObjectMapper mapper = (FullObjectMapper)initContext.ObjectMapper; IDataReader dataReader = (IDataReader)sourceObject; //int[] index = GetIndex(source, dest); //IValueMapper[] mappers = GetValueMappers(source, dest, index); //foreach (var valueMapper in mappers) //{ //} InitSchema(dataReader); if (mapper.ColParent) { FillObject(mapper, dataReader, destObject); while (dataReader.Read()) { destObject = FillObject(destObject, mapper, dataReader); } } else { FillObject(mapper, dataReader, destObject); } }
public override object MapDataReaderToObject( IDataReader dataReader, Type destObjectType, params object[] parameters) { // Get mapping for the type if (destObjectType == null) { throw new ArgumentNullException("type"); } if (dataReader.FieldCount == 0) { return(null); } int index = 0; FullObjectMapper mapper = GetObjectMapper(destObjectType, ref index); if (mapper.ColParent) { object result = FillObject(mapper, dataReader); while (dataReader.Read()) { result = FillObject(result, mapper, dataReader); } return(result); } else { return(FillObject(mapper, dataReader)); } }
public FullObjectMapper GetObjectMapper(Type mapperType, ref int startIndex) { var mapper = new FullObjectMapper { PropertyType = mapperType }; return((FullObjectMapper)GetObjectMapper(mapper, ref startIndex)); }
private T FillObject <T>(FullObjectMapper mapper, IDataReader datareader) { T result = mapper.ContainsLazyChild ? (T)Proxy.CreateClassProxy(typeof(T), new LazyValueLoadInterceptor(mapper, LoadLazy)) : FunctionFactory.Remote.CreateInstance <T>(); foreach (IMapper map in mapper.PropertiesMapping) { if (map is IObjectMapper && (map as IObjectMapper).IsLazy) { continue; } if (!(map.DataReaderIndex < datareader.FieldCount)) { continue; } if (!datareader.IsDBNull(map.DataReaderIndex)) { if (map is ValueMapper) { object value = datareader.GetValue(map.DataReaderIndex); map.Setter(result, value); } else if (map is FullObjectMapper) { object fillObject = FillObject((FullObjectMapper)map, datareader); map.Setter(result, fillObject); } if (map is CollectionFullObjectMapper) { object collectionInstance = Activator.CreateInstance((map as CollectionFullObjectMapper).PropertyCollectionType); map.Setter(result, collectionInstance); object fillObject = FillObject((CollectionFullObjectMapper)map, datareader); ((IList)collectionInstance).Add(fillObject); } } } return(result); }
public IMapper GetObjectMapper(IObjectMapper mapper, ref int startIndex) { Type mapperType = mapper.PropertyType; var objectMappers = new List <IObjectMapper>(); TableDescription tableDescription = GetTableDescription(mapperType); lock (_setterHandlersLock) { if (!_settersHandlers.ContainsKey(mapperType)) { _settersHandlers.Add(mapperType, new Dictionary <string, SetHandler>()); } if (!_gettersHandlers.ContainsKey(mapperType)) { _gettersHandlers.Add(mapperType, new Dictionary <string, GetHandler>()); } } PropertyInfo[] properties = mapperType.GetProperties(); PropertyInfo primaryKeyPropInfo = null; foreach (PropertyInfo prop in properties) { // Setters lock (_setterHandlersLock) { if (!_settersHandlers[mapper.PropertyType].ContainsKey(prop.Name)) { SetHandler setHandler = FunctionFactory.Il.CreateSetHandler(mapper.PropertyType, prop); _settersHandlers[mapper.PropertyType].Add(prop.Name, setHandler /* IL.Setter*/); } } object[] pkFields = prop.GetCustomAttributes(typeof(PrimaryKeyAttribute), true); if (pkFields.Length > 0) { primaryKeyPropInfo = prop; } } foreach (PropertyInfo prop in properties) { bool isLazy = false; if (!_ignoreLazyLoad) { object[] lazy = prop.GetCustomAttributes(typeof(LazyInstanceAttribute), true); if (lazy.Length > 0) { if (((LazyInstanceAttribute)lazy[0]).IsLazy) { isLazy = true; mapper.ContainsLazyChild = true; // Getters lock (_setterHandlersLock) if (!_gettersHandlers[mapperType].ContainsKey(primaryKeyPropInfo.Name)) { GetHandler getHandler = FunctionFactory.Il.CreateGetHandler(mapperType, primaryKeyPropInfo); _gettersHandlers[mapperType].Add(primaryKeyPropInfo.Name, getHandler); } } } } // Check if the accessor is an association object[] associationAttr = prop.GetCustomAttributes(typeof(AssociationAttribute), true); if (associationAttr.Length > 0) { if (associationAttr.Length > 1) { throw new Exception("AssociationAttribute is used several times on the property " + prop.Name); } var ass = (AssociationAttribute)associationAttr[0]; // Getters for IObjectMapper lock (_setterHandlersLock) if (!_gettersHandlers[mapperType].ContainsKey(prop.Name)) { GetHandler getHandler = FunctionFactory.Il.CreateGetHandler(mapperType, prop); _gettersHandlers[mapperType].Add(prop.Name, getHandler); } bool isCollection = prop.PropertyType.GetInterfaces().ToList().Contains(typeof(IList)); IObjectMapper propertiesMapping; if (!isCollection) { propertiesMapping = new FullObjectMapper { PropertyType = prop.PropertyType, IsNullable = ass.CanBeNull }; } else { Type listElementType = GetGenericType(prop.PropertyType); TableDescription colElementTableDescription = GetTableDescription(listElementType); propertiesMapping = new CollectionFullObjectMapper { PropertyType = listElementType, Getter = _gettersHandlers[mapperType][prop.Name], TableName = colElementTableDescription.TableName, PropertyCollectionType = prop.PropertyType, }; (mapper as FullObjectMapper).ColParent = true; } propertiesMapping.PropertyName = prop.Name; propertiesMapping.IsLazy = isLazy; propertiesMapping.Setter = _settersHandlers[mapperType][prop.Name]; if (propertiesMapping.IsLazy) { propertiesMapping.ParentKeyGetter = _gettersHandlers[mapperType][primaryKeyPropInfo.Name]; } objectMappers.Add(propertiesMapping); } else { object[] nomapAttr = prop.GetCustomAttributes(typeof(NoMapAttribute), true); if (nomapAttr.Length > 0) { continue; } object[] mapFields = prop.GetCustomAttributes(typeof(MapFieldAttribute), true); if (mapFields.Length > 1) { throw new Exception("AssociationAttribute is used several times on the property " + prop.Name); } var map = new ValueMapper { PropertyName = prop.Name, PropertyType = prop.PropertyType, DataReaderIndex = startIndex, Setter = _settersHandlers[mapperType][prop.Name], TableName = tableDescription.TableName, /* Optimize with Provider.BuildTableName */ ColumnName = mapFields.Length > 0 ? ((MapFieldAttribute)mapFields[0]).MapName : prop.Name }; mapper.PropertiesMapping.Add(map); object[] pkFields = prop.GetCustomAttributes(typeof(PrimaryKeyAttribute), true); if (pkFields.Length > 1) { throw new Exception("PrimaryKeyAttribute is used several times on the property " + prop.Name); } if (pkFields.Length == 1) { mapper.DataReaderIndex = startIndex; } startIndex++; } } foreach (IObjectMapper objMap in objectMappers) { IObjectMapper cel = mapper; while (cel != null) { if (mapper.PropertyType == objMap.PropertyType) { continue; } cel = (IObjectMapper)cel.ParentMapping; } objMap.ParentMapping = mapper; mapper.PropertiesMapping.Add(GetObjectMapper(objMap, ref startIndex)); //TODO startIndex++ ??? If we dont show the association column, dont increase the index } return(mapper); }
protected void AddWherePK(DbManager db, SqlQueryInfo query, StringBuilder sb, int nParameter, FullObjectMapper mapper) { sb.Append("WHERE\n"); foreach (IMapper mm in mapper.PropertiesMapping) { if (mm is ValueMapper && mm.DataReaderIndex == mapper.DataReaderIndex) { var valueMapper = (ValueMapper) mm; string tableAlias = mapper.PropertyType.Name; //mm.Name = ID_TRACK SqlQueryParameterInfo p = query.AddParameter( db.DataProvider.Convert(valueMapper.ColumnName + "_W", ConvertType.NameToQueryParameter). ToString(), valueMapper.ColumnName); sb.AppendFormat("\t{0}.{1} = ", "T" /* tableAlias */, db.DataProvider.Convert(p.FieldName, ConvertType.NameToQueryField)); if (nParameter < 0) sb.AppendFormat("{0} AND\n", p.ParameterName); else sb.AppendFormat("{{{0}}} AND\n", nParameter++); } } sb.Remove(sb.Length - 5, 5); }
private IMapper GetObjectMapper(IObjectMapper mapper, ref int startIndex, TypeAccessor akTypeAccessor) { //Todo: Remove this Call! _extension = TypeExtension.GetTypeExtension(mapper.PropertyType /*_typeAccessor.OriginalType*/, MappingSchema.Extensions); Type mapperType = mapper.PropertyType; var objectMappers = new List<IObjectMapper>(); TableDescription tableDescription = GetTableDescription(mapperType); lock (SetterHandlersLock) { if (!SettersHandlers.ContainsKey(mapperType)) SettersHandlers.Add(mapperType, new Dictionary<string, SetHandler>()); if (!GettersHandlers.ContainsKey(mapperType)) GettersHandlers.Add(mapperType, new Dictionary<string, GetHandler>()); } PropertyInfo[] properties = mapperType.GetProperties(); MemberAccessor primaryKeyMemberAccessor = null; foreach (MemberAccessor ma in akTypeAccessor) { // Setters lock (SetterHandlersLock) { if (!SettersHandlers[mapper.PropertyType].ContainsKey(ma.Name)) { SettersHandlers[mapper.PropertyType].Add(ma.Name, ma.SetValue); } } if (GetPrimaryKey(ma) != null) { primaryKeyMemberAccessor = ma; lock (SetterHandlersLock) { if (!GettersHandlers[mapperType].ContainsKey(ma.Name)) { GettersHandlers[mapperType].Add(ma.Name, ma.GetValue); } } mapper.PrimaryKeyValueGetters.Add(GettersHandlers[mapperType][ma.Name]); mapper.PrimaryKeyNames.Add(ma.Name); if (mapper.Association != null && (mapper.Association.OtherKey == null || mapper.Association.OtherKey.Length == 0)) { mapper.Association.OtherKey = new[] {ma.Name}; } } } if (primaryKeyMemberAccessor == null) throw new Exception("PrimaryKey attribute not found on type: " + mapperType); foreach (PropertyInfo prop in properties) { var ma = akTypeAccessor.First(x => x.Name == prop.Name); // Check if the accessor is an association var association = GetAssociation(ma); if (association != null) { // Getters for IObjectMapper lock (SetterHandlersLock) if (!GettersHandlers[mapperType].ContainsKey(prop.Name)) { GettersHandlers[mapperType].Add(prop.Name, ma.GetValue); } bool isCollection = prop.PropertyType.GetInterfaces().ToList().Contains(typeof (IList)); IObjectMapper propertiesMapping; if (!isCollection) { // TODO Generate this instance using the CreateObjectMapperInstance method of fullMappingSchema // _db.MappingSchema.CreateObjectMapperInstance(prop.PropertyType) propertiesMapping = new FullObjectMapper(_db, _ignoreLazyLoad, _factoryType) { PropertyType = prop.PropertyType, IsNullable = association.CanBeNull, Getter = GettersHandlers[mapperType][prop.Name], }; } else { Type listElementType = GetGenericType(prop.PropertyType); TableDescription colElementTableDescription = GetTableDescription(listElementType); // TODO Generate this instance using the CreateObjectMapperInstance method of fullMappingSchema propertiesMapping = new CollectionFullObjectMapper(_db, _factoryType) { PropertyType = listElementType, Getter = GettersHandlers[mapperType][prop.Name], TableName = colElementTableDescription.TableName, PropertyCollectionType = prop.PropertyType, }; if (mapper is FullObjectMapper) ((FullObjectMapper) mapper).ColParent = true; } if (association.ThisKey == null || association.ThisKey.Length == 0) association.ThisKey = new[] {primaryKeyMemberAccessor.Name}; bool isLazy = false; if (!_ignoreLazyLoad) { var lazy = GetLazyInstance(ma); // prop.GetCustomAttributes(typeof(LazyInstanceAttribute), true); if (lazy) { isLazy = true; mapper.ContainsLazyChild = true; // Getters lock (SetterHandlersLock) if (!GettersHandlers[mapperType].ContainsKey(primaryKeyMemberAccessor.Name)) { GettersHandlers[mapperType].Add(primaryKeyMemberAccessor.Name, primaryKeyMemberAccessor.GetValue); } } } propertiesMapping.Association = association; propertiesMapping.PropertyName = prop.Name; propertiesMapping.IsLazy = isLazy; propertiesMapping.Setter = SettersHandlers[mapperType][prop.Name]; if (propertiesMapping.IsLazy) { propertiesMapping.ParentKeyGetter = GettersHandlers[mapperType][primaryKeyMemberAccessor.Name]; } objectMappers.Add(propertiesMapping); } else { var mapIgnore = GetMapIgnore(ma); if (mapIgnore) continue; var mapField = GetMapField(ma); string columnName = mapField != null ? mapField.MapName : prop.Name; var map = new ValueMapper { PropertyName = prop.Name, PropertyType = prop.PropertyType, DataReaderIndex = startIndex, Setter = SettersHandlers[mapperType][prop.Name], TableName = tableDescription.TableName, ColumnName = columnName, }; var mapColumnName = map.GetColumnName(columnName); map.ColumnAlias = columnName == mapColumnName ? null : mapColumnName; mapper.PropertiesMapping.Add(map); var pkField = GetPrimaryKey(ma); if (pkField != null) mapper.DataReaderIndex = startIndex; startIndex++; } } foreach (IObjectMapper objMap in objectMappers) { #region Check mapping recursion IObjectMapper cel = mapper; while (cel != null) { if (mapper.PropertyType == objMap.PropertyType) continue; cel = (IObjectMapper) cel.ParentMapping; } #endregion objMap.ParentMapping = mapper; mapper.PropertiesMapping.Add(GetObjectMapper(objMap, ref startIndex, MappingSchema.GetObjectMapper(objMap.PropertyType).TypeAccessor)); } return mapper; }
private IMapper GetObjectMapper(IObjectMapper mapper, ref int startIndex, TypeAccessor akTypeAccessor) { //Todo: Remove this Call! _extension = TypeExtension.GetTypeExtension(mapper.PropertyType /*_typeAccessor.OriginalType*/, MappingSchema.Extensions); Type mapperType = mapper.PropertyType; var objectMappers = new List <IObjectMapper>(); TableDescription tableDescription = GetTableDescription(mapperType); lock (SetterHandlersLock) { if (!SettersHandlers.ContainsKey(mapperType)) { SettersHandlers.Add(mapperType, new Dictionary <string, SetHandler>()); } if (!GettersHandlers.ContainsKey(mapperType)) { GettersHandlers.Add(mapperType, new Dictionary <string, GetHandler>()); } } PropertyInfo[] properties = mapperType.GetProperties(); MemberAccessor primaryKeyMemberAccessor = null; foreach (MemberAccessor ma in akTypeAccessor) { // Setters lock (SetterHandlersLock) { if (!SettersHandlers[mapper.PropertyType].ContainsKey(ma.Name)) { SettersHandlers[mapper.PropertyType].Add(ma.Name, ma.SetValue); } } if (GetPrimaryKey(ma) != null) { primaryKeyMemberAccessor = ma; lock (SetterHandlersLock) { if (!GettersHandlers[mapperType].ContainsKey(ma.Name)) { GettersHandlers[mapperType].Add(ma.Name, ma.GetValue); } } mapper.PrimaryKeyValueGetters.Add(GettersHandlers[mapperType][ma.Name]); mapper.PrimaryKeyNames.Add(ma.Name); if (mapper.Association != null && (mapper.Association.OtherKey == null || mapper.Association.OtherKey.Length == 0)) { mapper.Association.OtherKey = new[] { ma.Name }; } } } if (primaryKeyMemberAccessor == null) { throw new Exception("PrimaryKey attribute not found on type: " + mapperType); } foreach (PropertyInfo prop in properties) { var ma = akTypeAccessor.First(x => x.Name == prop.Name); // Check if the accessor is an association var association = GetAssociation(ma); if (association != null) { // Getters for IObjectMapper lock (SetterHandlersLock) if (!GettersHandlers[mapperType].ContainsKey(prop.Name)) { GettersHandlers[mapperType].Add(prop.Name, ma.GetValue); } bool isCollection = prop.PropertyType.GetInterfaces().ToList().Contains(typeof(IList)); IObjectMapper propertiesMapping; if (!isCollection) { // TODO Generate this instance using the CreateObjectMapperInstance method of fullMappingSchema // _db.MappingSchema.CreateObjectMapperInstance(prop.PropertyType) propertiesMapping = new FullObjectMapper(_db, _ignoreLazyLoad, _factoryType) { PropertyType = prop.PropertyType, IsNullable = association.CanBeNull, Getter = GettersHandlers[mapperType][prop.Name], }; } else { Type listElementType = GetGenericType(prop.PropertyType); TableDescription colElementTableDescription = GetTableDescription(listElementType); // TODO Generate this instance using the CreateObjectMapperInstance method of fullMappingSchema propertiesMapping = new CollectionFullObjectMapper(_db, _factoryType) { PropertyType = listElementType, Getter = GettersHandlers[mapperType][prop.Name], TableName = colElementTableDescription.TableName, PropertyCollectionType = prop.PropertyType, }; if (mapper is FullObjectMapper) { ((FullObjectMapper)mapper).ColParent = true; } } if (association.ThisKey == null || association.ThisKey.Length == 0) { association.ThisKey = new[] { primaryKeyMemberAccessor.Name } } ; bool isLazy = false; if (!_ignoreLazyLoad) { var lazy = GetLazyInstance(ma); // prop.GetCustomAttributes(typeof(LazyInstanceAttribute), true); if (lazy) { isLazy = true; mapper.ContainsLazyChild = true; // Getters lock (SetterHandlersLock) if (!GettersHandlers[mapperType].ContainsKey(primaryKeyMemberAccessor.Name)) { GettersHandlers[mapperType].Add(primaryKeyMemberAccessor.Name, primaryKeyMemberAccessor.GetValue); } } } propertiesMapping.Association = association; propertiesMapping.PropertyName = prop.Name; propertiesMapping.IsLazy = isLazy; propertiesMapping.Setter = SettersHandlers[mapperType][prop.Name]; if (propertiesMapping.IsLazy) { propertiesMapping.ParentKeyGetter = GettersHandlers[mapperType][primaryKeyMemberAccessor.Name]; } objectMappers.Add(propertiesMapping); } else { var mapIgnore = GetMapIgnore(ma); if (mapIgnore) { continue; } var mapField = GetMapField(ma); string columnName = mapField != null ? mapField.MapName : prop.Name; var map = new ValueMapper { PropertyName = prop.Name, PropertyType = prop.PropertyType, DataReaderIndex = startIndex, Setter = SettersHandlers[mapperType][prop.Name], TableName = tableDescription.TableName, ColumnName = columnName, }; var mapColumnName = map.GetColumnName(columnName); map.ColumnAlias = columnName == mapColumnName ? null : mapColumnName; mapper.PropertiesMapping.Add(map); var pkField = GetPrimaryKey(ma); if (pkField != null) { mapper.DataReaderIndex = startIndex; } startIndex++; } } foreach (IObjectMapper objMap in objectMappers) { #region Check mapping recursion IObjectMapper cel = mapper; while (cel != null) { if (mapper.PropertyType == objMap.PropertyType) { continue; } cel = (IObjectMapper)cel.ParentMapping; } #endregion objMap.ParentMapping = mapper; mapper.PropertiesMapping.Add(GetObjectMapper(objMap, ref startIndex, MappingSchema.GetObjectMapper(objMap.PropertyType).TypeAccessor)); } return(mapper); }
private IList internalMapDataReaderToList( IDataReader reader, IList list, Type destObjectType, params object[] parameters) { FullObjectMapper mapper = (FullObjectMapper)GetObjectMapper(destObjectType); InitSchema(reader); object currentItem = null; List <int> pkIndexes = new List <int>(); foreach (var nm in mapper.PrimaryKeyNames) { pkIndexes.Add(mapper.PropertiesMapping.First(x => x.PropertyName == nm).DataReaderIndex); } while (reader.Read()) { var result = mapper.CreateInstance(); FillObject(mapper, reader, result); if (currentItem == null) { currentItem = result; list.Add(result); continue; } bool pkIsNull = false; bool allPksEqual = true; //This is needed, because DBValue can be Null, but the Field can be Guid, wich then is filled with Guid.Empty and this is also a valid value! foreach (var pkIndex in pkIndexes) { var dbValue = reader.GetValue(pkIndex); if (dbValue == DBNull.Value) { pkIsNull = true; break; } } if (!pkIsNull) { foreach (var pkGetter in mapper.PrimaryKeyValueGetters) { object resultPk = pkGetter.Invoke(result); object currentItemPk = pkGetter.Invoke(currentItem); if (!resultPk.Equals(currentItemPk)) { allPksEqual = false; break; } } } if (!pkIsNull && !allPksEqual) { currentItem = result; list.Add(result); //continue; } if (mapper.ColParent) { FillObject(currentItem, mapper, reader); } } return(list); }