/// <summary> /// 创建数据项。 /// </summary> /// <param name="reader">数据读取器。</param> /// <param name="parent">如果是导航属性则为父级对象</param> /// <returns>创建结果。</returns> protected object CreateItem(DbDataReader reader, OutputContentItem parent = null) { if (_Children.Count == 0 && !HasCollectionProperty) { return(Metadata.CreateInstance(reader, ItemFields, EmptyComplexObjects)); } else { var complexs = new object[Metadata.ComplexMembers.Count]; foreach (var item in Children.OfType <ComplexOutputInfo>()) { if (item.Type == EOutputType.Object) { complexs[item.Index] = item.CreateItem(reader, parent); } else { var propertyMember = item.Parent.Metadata.ComplexMembers[item.Index]; var propertyType = ((PropertyInfo)propertyMember.Member).PropertyType; if (propertyType.IsInterface) { if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(IGrouping <,>)) { var collectionType = typeof(GroupCollectionImpl <,>).MakeGenericType(propertyType.GetGenericArguments()); complexs[item.Index] = Activator.CreateInstance(collectionType); } else { var collectionType = typeof(HashSet <>).MakeGenericType(item.Metadata.ClrType); complexs[item.Index] = Activator.CreateInstance(collectionType); } } else if (propertyType.IsClass) { complexs[item.Index] = Activator.CreateInstance(propertyType); } else { throw new OutputException(this, string.Format(Res.NotSupportedInstanceType, propertyType)); } parent.Collections.Add((CollectionOutputInfo)item, new OutputContentCollection((CollectionOutputInfo)item, complexs[item.Index])); } } return(Metadata.CreateInstance(reader, ItemFields, complexs)); } }
//注册使用内容项,用于生成集合属性用。 private void RegisterItem(DbDataReader reader, OutputContentCollection collection) { string key = string.Join("_", ItemKeyFields.Select(a => reader.GetValue(a).ToString()).ToArray()); if (!collection.TryGetValue(key, out OutputContentItem item)) { item = new OutputContentItem(); var content = CreateItem(reader, item); if (content != null) { item.Content = content; collection.Add(key, item); } } foreach (var kv in item.Collections) { kv.Key.RegisterItem(reader, kv.Value); } }
/// <summary> /// 添加指定键值及输出内容项。 /// </summary> /// <param name="key">键值。</param> /// <param name="value">关联输出内容项。</param> public void Add(string key, OutputContentItem value) { KeyCollection.Add(key, value); Output.Metadata.CollectionAdd(Target, value.Content); }
/// <summary> /// 使用指定的键值获取输出内容项。 /// </summary> /// <param name="key">当前键值。</param> /// <param name="value">获取的内容项。</param> /// <returns>是否执行成功。</returns> public bool TryGetValue(string key, out OutputContentItem value) { return(KeyCollection.TryGetValue(key, out value)); }