/// <summary>
        /// Builds the result properties.
        /// </summary>
        /// <param name="resultMapId">The result map id.</param>
        /// <param name="resultMapConfig">The result map config.</param>
        /// <param name="resultClass">The result class.</param>
        /// <param name="prefix">The prefix.</param>
        /// <param name="suffix">The suffix.</param>
        /// <param name="dataExchangeFactory">The data exchange factory.</param>
        /// <param name="waitResultPropertyResolution">The wait result property resolution.</param>
        /// <returns></returns>
        private static ResultPropertyCollection BuildResultProperties(
            string resultMapId,
            IConfiguration resultMapConfig,
            Type resultClass,
            string prefix,
            string suffix,
            DataExchangeFactory dataExchangeFactory,
            WaitResultPropertyResolution waitResultPropertyResolution)
        {
            ResultPropertyCollection properties = new ResultPropertyCollection();

            ConfigurationCollection resultsConfig = resultMapConfig.Children.Find(ConfigConstants.ELEMENT_RESULT);

            for (int i = 0; i < resultsConfig.Count; i++)
            {
                ResultProperty mapping = null;
                try
                {
                    mapping = ResultPropertyDeSerializer.Deserialize(resultsConfig[i], resultClass, prefix, suffix, dataExchangeFactory);
                }
                catch (Exception e)
                {
                    throw new ConfigurationException("In ResultMap (" + resultMapId + ") can't build the result property: " + ConfigurationUtils.GetStringAttribute(resultsConfig[i].Attributes, ConfigConstants.ATTRIBUTE_PROPERTY) + ". Cause " + e.Message, e);
                }
                if (mapping.NestedResultMapName.Length > 0)
                {
                    waitResultPropertyResolution(mapping);
                }
                properties.Add(mapping);
            }

            return(properties);
        }
Пример #2
0
 /// <summary>
 /// Stores ResultProperty from which the NestedResultMap property must be resolved
 /// Delay resolution until all the ResultMap are processed.
 /// </summary>
 /// <param name="property">The property.</param>
 private void WaitResultPropertyResolution(ResultProperty property)
 {
     nestedProperties.Add(property);
 }
Пример #3
0
        /// <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);
        }
Пример #4
0
        /// <summary>
        /// Builds the result properties.
        /// </summary>
        /// <param name="resultMapId">The result map id.</param>
        /// <param name="resultMapConfig">The result map config.</param>
        /// <param name="resultClass">The result class.</param>
        /// <param name="prefix">The prefix.</param>
        /// <param name="suffix">The suffix.</param>
        /// <param name="dataExchangeFactory">The data exchange factory.</param>
        /// <param name="waitResultPropertyResolution">The wait result property resolution.</param>
        /// <returns></returns>
        private static ResultPropertyCollection BuildResultProperties(
            string resultMapId,
            IConfiguration resultMapConfig, 
            Type resultClass,
            string prefix,
            string suffix,
            DataExchangeFactory dataExchangeFactory,
            WaitResultPropertyResolution waitResultPropertyResolution)
        {
            ResultPropertyCollection properties = new ResultPropertyCollection();
            //获取result节点的集合配置信息
            ConfigurationCollection resultsConfig = resultMapConfig.Children.Find(ConfigConstants.ELEMENT_RESULT);
            for (int i = 0; i < resultsConfig.Count; i++)
            {
                ResultProperty mapping = null;
                try
                {
                    mapping = ResultPropertyDeSerializer.Deserialize(resultsConfig[i], resultClass, prefix, suffix, dataExchangeFactory);
                }
                catch(Exception e)
                {
                    throw new ConfigurationException("In ResultMap (" + resultMapId + ") can't build the result property: " + ConfigurationUtils.GetStringAttribute(resultsConfig[i].Attributes, ConfigConstants.ATTRIBUTE_PROPERTY) + ". Cause " + e.Message, e);
                }
                if (mapping.NestedResultMapName.Length > 0)//resultMapping属性如果有值 此处一般会有
                {
                    //添加到DefaultModelBuilder中的ResultPropertyCollection nestedProperties集合中
                    waitResultPropertyResolution(mapping);
                }
                properties.Add(mapping);
            }

            return properties;
        }
Пример #5
0
        /// <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;
		}
Пример #6
0
        /// <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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        /// <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);
        }