public ITypeHandler ResolveTypeHandler(Type clazz, string memberName, string clrType, string dbType, bool forSetter) { ITypeHandler typeHandler = null; if (clazz == null) { return(this.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler()); } if (typeof(IDictionary).IsAssignableFrom(clazz)) { if ((clrType == null) || (clrType.Length == 0)) { return(this.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler()); } try { Type type = TypeUtils.ResolveType(clrType); return(this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType)); } catch (Exception exception) { throw new ConfigurationErrorsException("Error. Could not set TypeHandler. Cause: " + exception.Message, exception); } } if (this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(clazz, dbType) != null) { return(this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(clazz, dbType)); } if ((clrType == null) || (clrType.Length == 0)) { Type memberTypeForSetter = null; if (forSetter) { memberTypeForSetter = ObjectProbe.GetMemberTypeForSetter(clazz, memberName); } else { memberTypeForSetter = ObjectProbe.GetMemberTypeForGetter(clazz, memberName); } return(this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(memberTypeForSetter, dbType)); } try { Type type3 = TypeUtils.ResolveType(clrType); typeHandler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type3, dbType); } catch (Exception exception2) { throw new ConfigurationErrorsException("Error. Could not set TypeHandler. Cause: " + exception2.Message, exception2); } return(typeHandler); }
/// <summary> /// Builds a <see cref="ResultPropertyCollection"/> for an <see cref="AutoResultMap"/>. /// </summary> /// <param name="dataExchangeFactory">The data exchange factory.</param> /// <param name="reader">The reader.</param> /// <param name="resultObject">The result object.</param> public static ResultPropertyCollection Build(DataExchangeFactory dataExchangeFactory, IDataReader reader, ref object resultObject) { Type targetType = resultObject.GetType(); ResultPropertyCollection properties = new ResultPropertyCollection(); try { // Get all PropertyInfo from the resultObject properties ReflectionInfo reflectionInfo = ReflectionInfo.GetInstance(targetType); string[] membersName = reflectionInfo.GetWriteableMemberNames(); IDictionary <string, ISetAccessor> propertyMap = new Dictionary <string, ISetAccessor>(); int length = membersName.Length; for (int i = 0; i < length; i++) { ISetAccessorFactory setAccessorFactory = dataExchangeFactory.AccessorFactory.SetAccessorFactory; ISetAccessor setAccessor = setAccessorFactory.CreateSetAccessor(targetType, membersName[i]); propertyMap.Add(membersName[i], setAccessor); } // Get all column Name from the reader // and build a resultMap from with the help of the PropertyInfo[]. DataTable dataColumn = reader.GetSchemaTable(); int count = dataColumn.Rows.Count; for (int i = 0; i < count; i++) { string propertyName = string.Empty; string columnName = dataColumn.Rows[i][0].ToString(); ISetAccessor matchedSetAccessor = null; propertyMap.TryGetValue(columnName, out matchedSetAccessor); int columnIndex = i; if (resultObject is Hashtable) { propertyName = columnName; ResultProperty property = new ResultProperty( propertyName, columnName, columnIndex, string.Empty, string.Empty, string.Empty, false, string.Empty, null, string.Empty, targetType, dataExchangeFactory, null); properties.Add(property); } Type propertyType = null; if (matchedSetAccessor == null) { try { propertyType = ObjectProbe.GetMemberTypeForSetter(resultObject, columnName); } catch { _logger.Error("The column [" + columnName + "] could not be auto mapped to a property on [" + resultObject + "]"); } } else { propertyType = matchedSetAccessor.MemberType; } if (propertyType != null || matchedSetAccessor != null) { propertyName = (matchedSetAccessor != null ? matchedSetAccessor.Name : columnName); ITypeHandler typeHandler = null; if (matchedSetAccessor != null) { //property.Initialize(dataExchangeFactory.TypeHandlerFactory, matchedSetAccessor); typeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(matchedSetAccessor.MemberType); } else { typeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(propertyType); } //property.PropertyStrategy = PropertyStrategyFactory.Get(property); ResultProperty property = new ResultProperty( propertyName, columnName, columnIndex, string.Empty, string.Empty, string.Empty, false, string.Empty, null, string.Empty, targetType, dataExchangeFactory, typeHandler); properties.Add(property); } } } catch (Exception e) { throw new DataMapperException("Error automapping columns. Cause: " + e.Message, e); } return(properties); }
/// <summary> /// Resolves the type handler. /// </summary> /// <param name="clazz">The clazz.</param> /// <param name="memberName">Name of the member.</param> /// <param name="clrType">Type of the CLR.</param> /// <param name="dbType">Type of the db.</param> /// <param name="forSetter">if set to <c>true</c> [for setter].</param> /// <returns></returns> public ITypeHandler ResolveTypeHandler(Type clazz, string memberName, string clrType, string dbType, bool forSetter) { ITypeHandler handler = null; if (clazz == null) { handler = this.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else if (typeof(IDictionary).IsAssignableFrom(clazz)) { // IDictionary if (clrType == null || clrType.Length == 0) { handler = this.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else { try { Type type = TypeUtils.ResolveType(clrType); handler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } catch (Exception e) { #if dotnet2 throw new ConfigurationErrorsException("Error. Could not set TypeHandler. Cause: " + e.Message, e); #else throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); #endif } } } else if (this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(clazz, dbType) != null) { // Primitive handler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(clazz, dbType); } else { // .NET object if (clrType == null || clrType.Length == 0) { Type type = null; if (forSetter) { type = ObjectProbe.GetMemberTypeForSetter(clazz, memberName); } else { type = ObjectProbe.GetMemberTypeForGetter(clazz, memberName); } handler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } else { try { Type type = TypeUtils.ResolveType(clrType); handler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } catch (Exception e) { #if dotnet2 throw new ConfigurationErrorsException("Error. Could not set TypeHandler. Cause: " + e.Message, e); #else throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); #endif } } } return(handler); }
/// <summary> /// Builds a <see cref="ResultPropertyCollection"/> for an <see cref="AutoResultMap"/>. /// </summary> /// <param name="dataExchangeFactory">The data exchange factory.</param> /// <param name="reader">The reader.</param> /// <param name="resultObject">The result object.</param> public static ResultPropertyCollection Build <T>(DataExchangeFactory dataExchangeFactory, IDataReader reader, ref T resultObject) { Type targetType = resultObject.GetType(); ResultPropertyCollection properties = new ResultPropertyCollection(); try { // Get all PropertyInfo from the resultObject properties var reflectionInfo = ReflectionInfo.GetInstance(targetType); var membersName = reflectionInfo.GetWriteableMemberNames(); var propertyMap = new Hashtable(); int length = membersName.Length; for (int i = 0; i < length; i++) { ISetAccessorFactory setAccessorFactory = dataExchangeFactory.AccessorFactory.SetAccessorFactory; ISetAccessor setAccessor = setAccessorFactory.CreateSetAccessor(targetType, membersName[i]); propertyMap.Add(membersName[i], setAccessor); } // Get all column Name from the reader // and build a resultMap from with the help of the PropertyInfo[]. var dataColumn = reader.GetSchemaTable(); if (dataColumn == null) { return(new ResultPropertyCollection()); } int count = dataColumn.Rows.Count; for (int i = 0; i < count; i++) { var columnName = dataColumn.Rows[i][0].ToString(); var matchedSetAccessor = propertyMap[columnName] as ISetAccessor; var property = new ResultProperty { ColumnName = columnName, ColumnIndex = i }; if (resultObject is Hashtable) { property.PropertyName = columnName; properties.Add(property); } Type propertyType = null; if (matchedSetAccessor == null) { try { propertyType = ObjectProbe.GetMemberTypeForSetter(resultObject, columnName); } catch { // ignored } } else { propertyType = matchedSetAccessor.MemberType; } if (propertyType != null || matchedSetAccessor != null) { property.PropertyName = (matchedSetAccessor != null ? matchedSetAccessor.Name : columnName); if (matchedSetAccessor != null) { property.Initialize(dataExchangeFactory.TypeHandlerFactory, matchedSetAccessor); } else { property.TypeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(propertyType); } property.PropertyStrategy = PropertyStrategyFactory.Get(property); properties.Add(property); } } } catch (Exception e) { throw new DataMapperException("Error automapping columns. Cause: " + e.Message, e); } return(properties); }
public static ResultPropertyCollection Build(DataExchangeFactory dataExchangeFactory, IDataReader reader, ref object resultObject) { Type type = resultObject.GetType(); ResultPropertyCollection propertys = new ResultPropertyCollection(); try { string[] writeableMemberNames = ReflectionInfo.GetInstance(type).GetWriteableMemberNames(); Hashtable hashtable = new Hashtable(); int length = writeableMemberNames.Length; for (int i = 0; i < length; i++) { ISetAccessor accessor = dataExchangeFactory.AccessorFactory.SetAccessorFactory.CreateSetAccessor(type, writeableMemberNames[i]); hashtable.Add(writeableMemberNames[i], accessor); } DataTable schemaTable = reader.GetSchemaTable(); int count = schemaTable.Rows.Count; for (int j = 0; j < count; j++) { string memberName = schemaTable.Rows[j][0].ToString(); ISetAccessor setAccessor = hashtable[memberName] as ISetAccessor; ResultProperty property = new ResultProperty { ColumnName = memberName, ColumnIndex = j }; if (resultObject is Hashtable) { property.PropertyName = memberName; propertys.Add(property); } Type memberTypeForSetter = null; if (setAccessor == null) { try { memberTypeForSetter = ObjectProbe.GetMemberTypeForSetter(resultObject, memberName); } catch { _logger.Error("The column [" + memberName + "] could not be auto mapped to a property on [" + resultObject.ToString() + "]"); } } else { memberTypeForSetter = setAccessor.MemberType; } if ((memberTypeForSetter != null) || (setAccessor != null)) { property.PropertyName = (setAccessor != null) ? setAccessor.Name : memberName; if (setAccessor != null) { property.Initialize(dataExchangeFactory.TypeHandlerFactory, setAccessor); } else { property.TypeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(memberTypeForSetter); } property.PropertyStrategy = PropertyStrategyFactory.Get(property); propertys.Add(property); } } } catch (Exception exception) { throw new DataMapperException("Error automapping columns. Cause: " + exception.Message, exception); } return(propertys); }
/// <summary> /// Resolves the type handler for Get/Set members. /// </summary> /// <param name="classType">Type of the class.</param> /// <param name="memberName">Name of the member.</param> /// <param name="memberType">Type of the member.</param> /// <param name="dbType">Type of the db.</param> /// <param name="forSetter">if set to <c>true</c> [for setter].</param> /// <returns></returns> public ITypeHandler ResolveTypeHandler( Type classType, string memberName, string memberType, string dbType, bool forSetter) { ITypeHandler handler = null; if (classType == null) { handler = GetUnkownTypeHandler(); } else if (typeof(IDictionary).IsAssignableFrom(classType) || typeof(DataRow).IsAssignableFrom(classType)) { // IDictionary or DataTable if (string.IsNullOrEmpty(memberType)) { handler = GetUnkownTypeHandler(); } else { try { Type type = TypeUtils.ResolveType(memberType); handler = GetTypeHandler(type, dbType); } catch (Exception e) { throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); } } } else if (GetTypeHandler(classType, dbType) != null) { // Primitive handler = GetTypeHandler(classType, dbType); } else { // .NET object if (string.IsNullOrEmpty(memberType)) { Type type = null; if (forSetter) { type = ObjectProbe.GetMemberTypeForSetter(classType, memberName); } else { type = ObjectProbe.GetMemberTypeForGetter(classType, memberName); } handler = GetTypeHandler(type, dbType); } else { try { Type type = TypeUtils.ResolveType(memberType); handler = GetTypeHandler(type, dbType); } catch (Exception e) { throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); } } } return(handler); }
/// <summary> /// Builds a <see cref="ResultPropertyCollection"/> for an <see cref="AutoResultMap"/>. /// </summary> /// <param name="dataExchangeFactory">The data exchange factory.</param> /// <param name="reader">The reader.</param> /// <param name="resultObject">The result object.</param> /// <param name="isUseAutoMapCompatibilityMode">The result object.</param> public static ResultPropertyCollection Build(DataExchangeFactory dataExchangeFactory, IDataReader reader, ref object resultObject, bool isUseAutoMapCompatibilityMode) { Type targetType = resultObject.GetType(); ResultPropertyCollection properties = new ResultPropertyCollection(); try { // Get all PropertyInfo from the resultObject properties ReflectionInfo reflectionInfo = ReflectionInfo.GetInstance(targetType); string[] membersName = reflectionInfo.GetPublicWriteableMemberNames(); Hashtable propertyMap = new Hashtable(); int length = membersName.Length; for (int i = 0; i < length; i++) { var memberName = membersName[i]; ISetAccessorFactory setAccessorFactory = dataExchangeFactory.AccessorFactory.SetAccessorFactory; ISetAccessor setAccessor = setAccessorFactory.CreateSetAccessor(targetType, memberName); var menberInfo = reflectionInfo.GetSetter(memberName); //ʹ�ñ�ע������Ϊ�к����ԵĹ�ϵ var attrs = menberInfo.GetCustomAttribute <AliasAttribute>(); if (attrs != null) { memberName = attrs.Name; } else if (isUseAutoMapCompatibilityMode)//���ü���ģʽ { memberName = memberName.Replace("_", "").ToLower(); } propertyMap.Add(memberName, setAccessor); } // Get all column Name from the reader // and build a resultMap from with the help of the PropertyInfo[]. DataTable dataColumn = reader.GetSchemaTable(); int count = dataColumn.Rows.Count; for (int i = 0; i < count; i++) { string columnName = dataColumn.Rows[i][0].ToString(); ISetAccessor matchedSetAccessor = propertyMap[columnName] as ISetAccessor; if (matchedSetAccessor == null && isUseAutoMapCompatibilityMode) { columnName = columnName.Replace("_", "").ToLower(); matchedSetAccessor = propertyMap[columnName] as ISetAccessor; } ResultProperty property = new ResultProperty(); property.ColumnName = columnName; property.ColumnIndex = i; if (resultObject is Hashtable) { property.PropertyName = columnName; properties.Add(property); } Type propertyType = null; if (matchedSetAccessor == null) { try { propertyType = ObjectProbe.GetMemberTypeForSetter(resultObject, columnName); } catch { _logger.Error("The column [" + columnName + "] could not be auto mapped to a property on [" + resultObject.ToString() + "]"); } } else { propertyType = matchedSetAccessor.MemberType; } if (propertyType != null || matchedSetAccessor != null) { property.PropertyName = (matchedSetAccessor != null ? matchedSetAccessor.Name : columnName); if (matchedSetAccessor != null) { property.Initialize(dataExchangeFactory.TypeHandlerFactory, matchedSetAccessor); } else { property.TypeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(propertyType); } property.PropertyStrategy = PropertyStrategyFactory.Get(property); properties.Add(property); } } } catch (Exception e) { throw new DataMapperException("Error automapping columns. Cause: " + e.Message, e); } return(properties); }