Exemplo n.º 1
0
        /// <summary>
        /// Gets the value of the specified <see cref="ResultProperty"/> that must be set on the target object.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultMap">The result map.</param>
        /// <param name="mapping">The mapping.</param>
        /// <param name="reader">The reader.</param>
		/// <param name="target">The target object</param>
        public object Get(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader)
        {
            // The property is a IList
            IList list = null;

            // Get the IList property 获取target类属性mapping.PropertyName的值
            object property = ObjectProbe.GetMemberValue(target, mapping.PropertyName,
                                       request.DataExchangeFactory.AccessorFactory);
            
            if (property == null)// Create the list if need
            {
                property = mapping.ListFactory.CreateInstance(null);
                mapping.Set(target, property);
            }
            list = (IList)property;

            object result = null;
            //返回的结果中包含其他类对象的情况
            IResultMap propertyRresultMap = mapping.NestedResultMap.ResolveSubMap(reader);

            if (propertyRresultMap.GroupByProperties.Count > 0)
            {
                 string uniqueKey = GetUniqueKey(propertyRresultMap, reader);

                // Gets the [key, result object] already build
                IDictionary<string, object> buildObjects = request.GetUniqueKeys(propertyRresultMap);

                if (buildObjects != null && buildObjects.ContainsKey(uniqueKey))
                {
                    // Unique key is already known, so get the existing result object and process additional results.
                    result = buildObjects[uniqueKey];

                    //In some cases (nested groupings) our object may be null, so there is
                    //no point in going on
                    if (result != null)
                    {
                        // process resulMapping attribute which point to a groupBy attribute
                        for (int index = 0; index < propertyRresultMap.Properties.Count; index++)
                        {
                            ResultProperty resultProperty = propertyRresultMap.Properties[index];
                            if (resultProperty.PropertyStrategy is GroupByStrategy)
                            {
                                // 跳到PropertyStrategy中的类
                                resultProperty.PropertyStrategy.Set(request, propertyRresultMap, resultProperty, ref result, reader, null);
                            }
                        }
                    }
                    result = SKIP;
                }
                else if (uniqueKey == null || buildObjects == null || !buildObjects.ContainsKey(uniqueKey))
                {
                    // Unique key is NOT known, so create a new result object and then process additional results.
                    //在ResultMapStrategy类中实现对target类mapping属性的赋值
                    result = resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);

                    if (buildObjects == null)
                    {
                        buildObjects = new Dictionary<string, object>();
                        request.SetUniqueKeys(propertyRresultMap, buildObjects);
                    }
                    buildObjects[uniqueKey] = result;                       
                }               
            }
            else // Last resultMap have no groupBy attribute
            {
                if (propertyRresultMap.KeyPropertyNames.Count > 0)
                {
                    result = circularResultMapStrategy.Get(request, resultMap, mapping, ref target, reader);
                }
                else
                {
                    //在ResultMapStrategy类中实现对target类mapping属性的赋值
                    result = resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);
                }
            }                

            
            if ((result != null) && (result != SKIP))
            {
                list.Add(result);
            }

            return result;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Processes the specified <see cref="IDataReader"/>.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        /// <returns>The result object</returns>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            object outObject = resultObject;
            //实现对当前CurrentResultMap类中的discrimitor子节点类的设置
            IResultMap resultMap = request.CurrentResultMap.ResolveSubMap(reader);

            //获取resultMap分组名对应的值
            string uniqueKey = GetUniqueKey(resultMap, reader);
            // Gets the [key, result object] already build
            //根据IResultMap获取对应的IDictionary
            IDictionary<string, object> buildObjects = request.GetUniqueKeys(resultMap);

            if (buildObjects != null && buildObjects.ContainsKey(uniqueKey))
            {
                // Unique key is already known, so get the existing result object and process additional results.
                outObject = buildObjects[uniqueKey];
                // process resulMapping attribute which point to a groupBy attribute
                //遍历resultMap.Properties属性中为GroupByStrategy的属性
                for (int index = 0; index < resultMap.Properties.Count; index++)
                {
                    ResultProperty resultProperty = resultMap.Properties[index];
                    if (resultProperty.PropertyStrategy is PropertStrategy.GroupByStrategy)
                    {
                        //.................??? 跳到PropertyStrategy中的GroupByStrategy类
                        resultProperty.PropertyStrategy.Set(request, resultMap, resultProperty, ref outObject, reader, null);
                    }
                }
                outObject = SKIP;
            }
            else if (uniqueKey == null || buildObjects == null || !buildObjects.ContainsKey(uniqueKey))
            {
                // Unique key is NOT known, so create a new result object and process additional results.

                // Fix IBATISNET-241
                if (outObject == null)
                {
                    // temp ?, we don't support constructor tag with groupBy attribute
                    outObject = resultMap.CreateInstanceOfResult(null);//创建返回类的对象
                }

                for (int index = 0; index < resultMap.Properties.Count; index++)
                {
                    ResultProperty resultProperty = resultMap.Properties[index];
                    //为当前的resultProperty属性设置其从数据库中读到的值到outObject类中
                    resultProperty.PropertyStrategy.Set(request, resultMap, resultProperty, ref outObject, reader, null);                   
                }

                if (buildObjects == null)
                {
                    buildObjects = new Dictionary<string, object>();
                    request.SetUniqueKeys(resultMap, buildObjects);
                }
                buildObjects[uniqueKey] = outObject;
            }

            return outObject;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Processes the specified <see cref="IDataReader"/>.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        /// <returns>The result object</returns>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            object outObject = resultObject;

            IResultMap resultMap = request.CurrentResultMap.ResolveSubMap(reader);

            string uniqueKey = GetUniqueKey(resultMap, reader);
            // Gets the [key, result object] already build
            IDictionary<string, object> buildObjects = request.GetUniqueKeys(resultMap);

            if (buildObjects != null && buildObjects.ContainsKey(uniqueKey))
            {
                // Unique key is already known, so get the existing result object and process additional results.
                outObject = buildObjects[uniqueKey];
                // process resulMapping attribute which point to a groupBy attribute
                for (int index = 0; index < resultMap.Properties.Count; index++)
                {
                    ResultProperty resultProperty = resultMap.Properties[index];
                    if (resultProperty.PropertyStrategy is PropertStrategy.GroupByStrategy)
                    {
                        resultProperty.PropertyStrategy.Set(request, resultMap, resultProperty, ref outObject, reader, null);
                    }
                }
                outObject = SKIP;
            }
            else if (uniqueKey == null || buildObjects == null || !buildObjects.ContainsKey(uniqueKey))
            {
                // Unique key is NOT known, so create a new result object and process additional results.

                // Fix IBATISNET-241
                if (outObject == null)
                {
                    // temp ?, we don't support constructor tag with groupBy attribute
                    outObject = resultMap.CreateInstanceOfResult(null);
                }

                for (int index = 0; index < resultMap.Properties.Count; index++)
                {
                    ResultProperty resultProperty = resultMap.Properties[index];
                    resultProperty.PropertyStrategy.Set(request, resultMap, resultProperty, ref outObject, reader, null);                   
                }

                if (buildObjects == null)
                {
                    buildObjects = new Dictionary<string, object>();
                    request.SetUniqueKeys(resultMap, buildObjects);
                }
                buildObjects[uniqueKey] = outObject;
            }

            return outObject;
        }