Beispiel #1
0
        private void GetChildNode(ConfigurationScope configScope)
        {
            ResultProperty property = null;
            SubMap         subMap   = null;
            XmlNodeList    list     = configScope.NodeContext.SelectNodes(DomSqlMapBuilder.ApplyMappingNamespacePrefix("constructor/argument"), configScope.XmlNamespaceManager);

            if (list.Count > 0)
            {
                Type[]   types          = new Type[list.Count];
                string[] parametersName = new string[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                    ArgumentProperty property2 = ArgumentPropertyDeSerializer.Deserialize(list[i], configScope);
                    this._parameters.Add(property2);
                    parametersName[i] = property2.ArgumentName;
                }
                ConstructorInfo constructor = this.GetConstructor(this._class, parametersName);
                for (int j = 0; j < this._parameters.Count; j++)
                {
                    ArgumentProperty property3 = (ArgumentProperty)this._parameters[j];
                    configScope.ErrorContext.MoreInfo = "initialize argument property : " + property3.ArgumentName;
                    property3.Initialize(configScope, constructor);
                    types[j] = property3.MemberType;
                }
                this._objectFactory = configScope.SqlMapper.ObjectFactory.CreateFactory(this._class, types);
            }
            else if (Type.GetTypeCode(this._class) == TypeCode.Object)
            {
                this._objectFactory = configScope.SqlMapper.ObjectFactory.CreateFactory(this._class, Type.EmptyTypes);
            }
            foreach (XmlNode node in configScope.NodeContext.SelectNodes(DomSqlMapBuilder.ApplyMappingNamespacePrefix("result"), configScope.XmlNamespaceManager))
            {
                property = ResultPropertyDeSerializer.Deserialize(node, configScope);
                configScope.ErrorContext.MoreInfo = "initialize result property: " + property.PropertyName;
                property.Initialize(configScope, this._class);
                this._properties.Add(property);
            }
            XmlNode node2 = configScope.NodeContext.SelectSingleNode(DomSqlMapBuilder.ApplyMappingNamespacePrefix("discriminator"), configScope.XmlNamespaceManager);

            if (node2 != null)
            {
                configScope.ErrorContext.MoreInfo = "initialize discriminator";
                this.Discriminator = DiscriminatorDeSerializer.Deserialize(node2, configScope);
                this.Discriminator.SetMapping(configScope, this._class);
            }
            if ((configScope.NodeContext.SelectNodes(DomSqlMapBuilder.ApplyMappingNamespacePrefix("subMap"), configScope.XmlNamespaceManager).Count > 0) && (this.Discriminator == null))
            {
                throw new ConfigurationException("The discriminator is null, but somehow a subMap was reached.  This is a bug.");
            }
            foreach (XmlNode node3 in configScope.NodeContext.SelectNodes(DomSqlMapBuilder.ApplyMappingNamespacePrefix("subMap"), configScope.XmlNamespaceManager))
            {
                configScope.ErrorContext.MoreInfo = "initialize subMap";
                subMap = SubMapDeSerializer.Deserialize(node3, configScope);
                this.Discriminator.Add(subMap);
            }
        }
        /// <summary>
        /// Initilaize the underlying mapping
        /// </summary>
        /// <param name="configScope"></param>
        /// <param name="resultClass"></param>
        public void SetMapping(ConfigurationScope configScope, Type resultClass)
        {
            configScope.ErrorContext.MoreInfo = "Initialize discriminator mapping";
            _mapping              = new ResultProperty();
            _mapping.ColumnName   = _columnName;
            _mapping.ColumnIndex  = _columnIndex;
            _mapping.CLRType      = _clrType;
            _mapping.CallBackName = _callBackName;
            _mapping.DbType       = _dbType;
            _mapping.NullValue    = _nullValue;

            _mapping.Initialize(configScope, resultClass);
        }
        /// <summary>
        /// Get the result properties and the subMap properties.
        /// </summary>
        /// <param name="configScope"></param>
        private void GetChildNode(ConfigurationScope configScope)
        {
            ResultProperty mapping = null;
            SubMap         subMap  = null;

            #region Load the parameters constructor
            XmlNodeList nodeList = configScope.NodeContext.SelectNodes(DomSqlMapBuilder.ApplyMappingNamespacePrefix(XML_CONSTRUCTOR_ARGUMENT), configScope.XmlNamespaceManager);
            if (nodeList.Count > 0)
            {
                Type[]   parametersType = new Type[nodeList.Count];
                string[] parametersName = new string[nodeList.Count];
                for (int i = 0; i < nodeList.Count; i++)
                {
                    ArgumentProperty argumentMapping = ArgumentPropertyDeSerializer.Deserialize(nodeList[i], configScope);
                    _parameters.Add(argumentMapping);
                    parametersName[i] = argumentMapping.ArgumentName;
                }
                ConstructorInfo constructorInfo = this.GetConstructor(_class, parametersName);
                for (int i = 0; i < _parameters.Count; i++)
                {
                    ArgumentProperty argumentMapping = (ArgumentProperty)_parameters[i];

                    configScope.ErrorContext.MoreInfo = "initialize argument property : " + argumentMapping.ArgumentName;
                    argumentMapping.Initialize(configScope, constructorInfo);
                    parametersType[i] = argumentMapping.MemberType;
                }
                // Init the object factory
                _objectFactory = configScope.SqlMapper.ObjectFactory.CreateFactory(_class, parametersType);
            }
            else
            {
                if (Type.GetTypeCode(_class) == TypeCode.Object)
                {
                    _objectFactory = configScope.SqlMapper.ObjectFactory.CreateFactory(_class, Type.EmptyTypes);
                }
            }

            #endregion

            #region Load the Result Properties

            foreach (XmlNode resultNode in configScope.NodeContext.SelectNodes(DomSqlMapBuilder.ApplyMappingNamespacePrefix(XML_RESULT), configScope.XmlNamespaceManager))
            {
                mapping = ResultPropertyDeSerializer.Deserialize(resultNode, configScope);

                configScope.ErrorContext.MoreInfo = "initialize result property: " + mapping.PropertyName;

                mapping.Initialize(configScope, _class);

                _properties.Add(mapping);
            }
            #endregion

            #region Load the Discriminator Property

            XmlNode discriminatorNode = configScope.NodeContext.SelectSingleNode(DomSqlMapBuilder.ApplyMappingNamespacePrefix(XML_DISCRIMNATOR), configScope.XmlNamespaceManager);
            if (discriminatorNode != null)
            {
                configScope.ErrorContext.MoreInfo = "initialize discriminator";

                this.Discriminator = DiscriminatorDeSerializer.Deserialize(discriminatorNode, configScope);
                this.Discriminator.SetMapping(configScope, _class);
            }
            #endregion

            #region Load the SubMap Properties

            if (configScope.NodeContext.SelectNodes(DomSqlMapBuilder.ApplyMappingNamespacePrefix(XML_SUBMAP), configScope.XmlNamespaceManager).Count > 0 && this.Discriminator == null)
            {
                throw new ConfigurationException("The discriminator is null, but somehow a subMap was reached.  This is a bug.");
            }
            foreach (XmlNode resultNode in configScope.NodeContext.SelectNodes(DomSqlMapBuilder.ApplyMappingNamespacePrefix(XML_SUBMAP), configScope.XmlNamespaceManager))
            {
                configScope.ErrorContext.MoreInfo = "initialize subMap";
                subMap = SubMapDeSerializer.Deserialize(resultNode, configScope);

                this.Discriminator.Add(subMap);
            }
            #endregion
        }
        /// <summary>
        /// Initilaize the underlying mapping
        /// </summary>
        /// <param name="configScope"></param>
        /// <param name="resultClass"></param>
        public void SetMapping(ConfigurationScope configScope, Type resultClass)
        {
            configScope.ErrorContext.MoreInfo = "Initialize discriminator mapping";
            _mapping = new ResultProperty();
            _mapping.ColumnName =  _columnName;
            _mapping.ColumnIndex = _columnIndex;
            _mapping.CLRType = _clrType;
            _mapping.CallBackName = _callBackName;
            _mapping.DbType = _dbType;
            _mapping.NullValue = _nullValue;

            _mapping.Initialize( configScope, resultClass );
        }
        /// <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();

                Hashtable 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[].
                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;

                    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;
        }