Пример #1
0
        /// <summary>
        /// Processes the specified <see cref="IDataReader"/>
        /// when a ResultClass is specified on the statement and
        /// the ResultClass is a SimpleType.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            object        outObject = resultObject;
            AutoResultMap resultMap = request.CurrentResultMap as AutoResultMap;

            if (outObject == null)
            {
                outObject = resultMap.CreateInstanceOfResultClass();
            }

            if (!resultMap.IsInitalized)
            {
                lock (resultMap)
                {
                    if (!resultMap.IsInitalized)
                    {
                        // Create a ResultProperty
                        ResultProperty property = new ResultProperty();
                        property.PropertyName     = "value";
                        property.ColumnIndex      = 0;
                        property.TypeHandler      = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(outObject.GetType());
                        property.PropertyStrategy = PropertyStrategyFactory.Get(property);

                        resultMap.Properties.Add(property);
                        resultMap.DataExchange = request.DataExchangeFactory.GetDataExchangeForClass(typeof(int));// set the PrimitiveDataExchange
                        resultMap.IsInitalized = true;
                    }
                }
            }

            resultMap.Properties[0].PropertyStrategy.Set(request, resultMap, resultMap.Properties[0], ref outObject, reader, null);

            return(outObject);
        }
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            object        target           = resultObject;
            AutoResultMap currentResultMap = request.CurrentResultMap as AutoResultMap;

            if (target == null)
            {
                target = currentResultMap.CreateInstanceOfResultClass();
            }
            if (!currentResultMap.IsInitalized)
            {
                lock (currentResultMap)
                {
                    if (!currentResultMap.IsInitalized)
                    {
                        ResultProperty property;
                        property = new ResultProperty {
                            PropertyName     = "value",
                            ColumnIndex      = 0,
                            TypeHandler      = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(target.GetType()),
                            PropertyStrategy = PropertyStrategyFactory.Get(property)
                        };
                        currentResultMap.Properties.Add(property);
                        currentResultMap.DataExchange = request.DataExchangeFactory.GetDataExchangeForClass(typeof(int));
                        currentResultMap.IsInitalized = true;
                    }
                }
            }
            currentResultMap.Properties[0].PropertyStrategy.Set(request, currentResultMap, currentResultMap.Properties[0], ref target, reader, null);
            return(target);
        }
Пример #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 <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);
        }
Пример #4
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);
        }
Пример #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ResultProperty"/> class.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <param name="columnIndex">Index of the column.</param>
        /// <param name="clrType">Type of the CLR.</param>
        /// <param name="callBackName">Name of the call back.</param>
        /// <param name="dbType">Type of the db.</param>
        /// <param name="isLazyLoad">if set to <c>true</c> [is lazy load].</param>
        /// <param name="nestedResultMapName">Name of the nested result map.</param>
        /// <param name="nullValue">The null value.</param>
        /// <param name="select">The select.</param>
        /// <param name="resultClass">The result class.</param>
        /// <param name="dataExchangeFactory">The data exchange factory.</param>
        /// <param name="typeHandler">The type handler.</param>
        public ResultProperty(
            string propertyName,
            string columnName,
            int columnIndex,
            string clrType,
            string callBackName,
            string dbType,
            bool isLazyLoad,
            string nestedResultMapName,
            string nullValue,
            string select,
            Type resultClass,
            DataExchangeFactory dataExchangeFactory,
            ITypeHandler typeHandler
            )
        {
            Contract.Require.That(propertyName, Is.Not.Null & Is.Not.Empty).When("retrieving argument propertyName in ResultProperty constructor");

            this.propertyName = propertyName;

            this.columnName          = columnName;
            this.callBackName        = callBackName;
            this.dbType              = dbType;
            this.clrType             = clrType;
            this.select              = select;
            this.nestedResultMapName = nestedResultMapName;
            this.isLazyLoad          = isLazyLoad;
            this.nullValue           = nullValue;
            this.columnIndex         = columnIndex;
            this.typeHandler         = typeHandler;

            #region isComplexMemberName
            if (propertyName.IndexOf('.') < 0)
            {
                isComplexMemberName = false;
            }
            else // complex member name FavouriteLineItem.Id
            {
                isComplexMemberName = true;
            }
            #endregion

            if (propertyName.Length > 0 &&
                propertyName != "value" &&
                !typeof(IDictionary).IsAssignableFrom(resultClass) &&
                !typeof(DataRow).IsAssignableFrom(resultClass))
            {
                #region SetAccessor
                if (!isComplexMemberName)
                {
                    setAccessor = dataExchangeFactory.AccessorFactory.SetAccessorFactory.CreateSetAccessor(resultClass, propertyName);
                }
                else // complex member name FavouriteLineItem.Id
                {
                    MemberInfo propertyInfo = ObjectProbe.GetMemberInfoForSetter(resultClass, propertyName);
                    string     memberName   = propertyName.Substring(propertyName.LastIndexOf('.') + 1);
                    setAccessor = dataExchangeFactory.AccessorFactory.SetAccessorFactory.CreateSetAccessor(propertyInfo.ReflectedType, memberName);
                }
                #endregion

                isGenericIList = TypeUtils.IsImplementGenericIListInterface(MemberType);
                isIList        = typeof(IList).IsAssignableFrom(MemberType);

                #region Set the list factory
                if (isGenericIList)
                {
                    if (MemberType.IsArray)
                    {
                        listFactory = arrayListFactory;
                    }
                    else
                    {
                        Type[] typeArgs = MemberType.GetGenericArguments();

                        if (typeArgs.Length == 0)// Custom collection which derive from List<T>
                        {
                            listFactory = dataExchangeFactory.ObjectFactory.CreateFactory(MemberType, Type.EmptyTypes);
                        }
                        else
                        {
                            Type genericIList      = typeof(IList <>);
                            Type interfaceListType = genericIList.MakeGenericType(typeArgs);

                            Type genericList = typeof(List <>);
                            Type listType    = genericList.MakeGenericType(typeArgs);

                            if ((interfaceListType == MemberType) || (listType == MemberType))
                            {
                                Type constructedType = genericList.MakeGenericType(typeArgs);
                                listFactory = dataExchangeFactory.ObjectFactory.CreateFactory(
                                    constructedType,
                                    Type.EmptyTypes);
                            }
                            else // Custom collection which derive from List<T>
                            {
                                listFactory = dataExchangeFactory.ObjectFactory.CreateFactory(MemberType, Type.EmptyTypes);
                            }
                        }
                    }
                }
                else if (isIList)
                {
                    if (MemberType.IsArray)
                    {
                        listFactory = arrayListFactory;
                    }
                    else
                    {
                        if (MemberType == typeof(IList))
                        {
                            listFactory = arrayListFactory;
                        }
                        else // custom collection
                        {
                            listFactory = dataExchangeFactory.ObjectFactory.CreateFactory(MemberType, Type.EmptyTypes);
                        }
                    }
                }
                #endregion
            }

            #region TypeHandler
            if (!string.IsNullOrEmpty(CallBackName))
            {
                try
                {
                    Type type = dataExchangeFactory.TypeHandlerFactory.GetType(CallBackName);
                    ITypeHandlerCallback typeHandlerCallback = (ITypeHandlerCallback)Activator.CreateInstance(type);
                    this.typeHandler = new CustomTypeHandler(typeHandlerCallback);
                }
                catch (Exception e)
                {
                    throw new ConfigurationException("Error occurred during custom type handler configuration.  Cause: " + e.Message, e);
                }
            }
            else
            {
                if (typeHandler == null)
                {
                    //configScope.ErrorContext.MoreInfo = "Result property '" + propertyName + "' set the typeHandler attribute.";
                    this.typeHandler = dataExchangeFactory.TypeHandlerFactory.ResolveTypeHandler(resultClass, propertyName, clrType, dbType, true);
                }
            }
            #endregion

            #region LazyLoad
            if (IsLazyLoad)
            {
                lazyFactory = new LazyFactoryBuilder().GetLazyFactory(setAccessor.MemberType);
            }
            #endregion

            if (!GetType().IsSubclassOf(typeof(ResultProperty)))
            {
                propertyStrategy = PropertyStrategyFactory.Get(this);
            }
        }
Пример #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>
        /// <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);
        }