///<summary> /// Sets value of the specified <see cref="ResultProperty"/> on the target object /// when a 'select' attribute exists and fills an <see cref="IList"/> property /// on the <see cref="ResultProperty"/> are empties. /// </summary> /// <param name="request">The request.</param> /// <param name="resultMap">The result map.</param> /// <param name="mapping">The ResultProperty.</param> /// <param name="target">The target.</param> /// <param name="reader">The current <see cref="IDataReader"/></param> /// <param name="keys">The keys</param> public void Set(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader, object keys) { // Get the select statement IMappedStatement selectStatement = request.MappedStatement.ModelStore.GetMappedStatement(mapping.Select); PostBindind postSelect = new PostBindind(); postSelect.Statement = selectStatement; postSelect.Keys = keys; postSelect.Target = target; postSelect.ResultProperty = mapping; if (mapping.IsLazyLoad) { object values = mapping.LazyFactory.CreateProxy(request.MappedStatement.ModelStore.DataMapper, selectStatement, keys, target, mapping.SetAccessor); mapping.Set(target, values); } else { if (mapping.SetAccessor.MemberType.GetGenericTypeDefinition() == typeof(IList <>)) { postSelect.Method = PostBindind.ExecuteMethod.ExecuteQueryForGenericIList; } request.DelayedLoad.Enqueue(postSelect); } }
///<summary> /// Sets value of the specified <see cref="ResultProperty"/> on the target object /// when a 'select' attribute exists and fills an object property. /// on the <see cref="ResultProperty"/> are empties. /// </summary> /// <param name="request">The request.</param> /// <param name="resultMap">The result map.</param> /// <param name="mapping">The ResultProperty.</param> /// <param name="target">The target.</param> /// <param name="reader">The current <see cref="IDataReader"/></param> /// <param name="keys">The keys</param> public void Set(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader, object keys) { // Get the select statement IMappedStatement selectStatement = request.MappedStatement.ModelStore.GetMappedStatement(mapping.Select); PostBindind postSelect = new PostBindind(); postSelect.Statement = selectStatement; postSelect.Keys = keys; postSelect.Target = target; postSelect.ResultProperty = mapping; if (mapping.IsLazyLoad) { object values = mapping.LazyFactory.CreateProxy( request.MappedStatement.ModelStore.DataMapper, selectStatement, keys, target, mapping.SetAccessor); //为target类的mapping属性设置值为values mapping.Set(target, values); } else { postSelect.Method = PostBindind.ExecuteMethod.ExecuteQueryForObject; //将postSelect类对象添加到队列中 request.DelayedLoad.Enqueue(postSelect); } }
///<summary> /// Sets value of the specified <see cref="ResultProperty"/> on the target object /// when a 'select' attribute exists /// on the <see cref="ResultProperty"/> are empties. /// </summary> /// <param name="request">The request.</param> /// <param name="resultMap">The result map.</param> /// <param name="mapping">The ResultProperty.</param> /// <param name="target">The target.</param> /// <param name="reader">The reader.</param> /// <param name="selectKeys">The keys</param> public void Set(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader, object selectKeys) { string paramString = mapping.ColumnName; object keys = null; bool wasNull = false; #region Finds the select keys. if (paramString.IndexOf(',') > 0 || paramString.IndexOf('=') > 0) // composite parameters key { IDictionary keyMap = new Hashtable(); keys = keyMap; // define which character is seperating fields char[] splitter = { '=', ',' }; string[] paramTab = paramString.Split(splitter); if (paramTab.Length % 2 != 0) { throw new DataMapperException("Invalid composite key string format in '" + mapping.PropertyName + ". It must be: property1=column1,property2=column2,..."); } IEnumerator enumerator = paramTab.GetEnumerator(); while (!wasNull && enumerator.MoveNext()) { string hashKey = ((string)enumerator.Current).Trim(); if (paramString.Contains("="))// old 1.x style multiple params { enumerator.MoveNext(); } object hashValue = reader.GetValue(reader.GetOrdinal(((string)enumerator.Current).Trim())); keyMap.Add(hashKey, hashValue); wasNull = (hashValue == DBNull.Value); } } else // single parameter key { keys = reader.GetValue(reader.GetOrdinal(paramString)); wasNull = reader.IsDBNull(reader.GetOrdinal(paramString)); } #endregion if (wasNull) { // set the value of an object property to null mapping.Set(target, null); } else // Collection object or .Net object { selectStrategy.Set(request, resultMap, mapping, ref target, reader, keys); } }
/// <summary> /// Sets the value to the result property. /// </summary> /// <param name="mapping"></param> /// <param name="target"></param> /// <param name="dataBaseValue"></param> public override void SetData(ref object target, ResultProperty mapping, object dataBaseValue) { Type targetType = target.GetType(); if ((targetType != parameterClass) && !targetType.IsSubclassOf(parameterClass) && !parameterClass.IsAssignableFrom(targetType)) { throw new ArgumentException("Could not set value in class '" + target.GetType() + "' for property '" + mapping.PropertyName + "' of type '" + mapping.MemberType + "'"); } if (mapping.IsComplexMemberName) { ObjectProbe.SetMemberValue(target, mapping.PropertyName, dataBaseValue, DataExchangeFactory.ObjectFactory, DataExchangeFactory.AccessorFactory); } else { mapping.Set(target, dataBaseValue); } }
/// <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); }