private static IEntityPropertyMetadata FindBaseProperty(ref IEntityPropertyMetadataCollection properties, string name, ref ICollection <IEntityMetadata> ancestors) { if (properties == null) { return(null); } var baseEntity = properties.Entity.GetBaseEntity(); if (baseEntity != null) { ancestors = new List <IEntityMetadata>(); ancestors.Add(baseEntity); } while (baseEntity != null) { if (baseEntity.Properties.TryGet(name, out var property)) { return(property); } baseEntity = baseEntity.GetBaseEntity(); if (baseEntity != null) { ancestors.Add(baseEntity); } } return(null); }
/// <summary> /// <para>查找属性集合中指定成员路径对应的属性。</para> /// <para>注:查找范围包括父实体的属性集。</para> /// </summary> /// <param name="properties">指定要查找的属性集合。</param> /// <param name="path">指定要查找的成员路径,支持多级导航属性路径。</param> /// <param name="match">属性匹配成功后的回调函数。</param> /// <returns>返回找到的属性,如果没有指定指定成员路径的属性则返回空(null)。</returns> public static EntityPropertyFindResult <T> Find <T>(this IEntityPropertyMetadataCollection properties, string path, T token, Func <EntityPropertyFindContext <T>, T> match = null) { if (string.IsNullOrEmpty(path)) { return(EntityPropertyFindResult <T> .Failure(token)); } Queue <IEntityMetadata> ancestors = null; IEntityPropertyMetadata property = null; var parts = path.Split('.'); for (int i = 0; i < parts.Length; i++) { if (properties == null) { return(EntityPropertyFindResult <T> .Failure(token)); } //如果当前属性集合中不包含指定的属性,则尝试从父实体中查找 if (!properties.TryGet(parts[i], out property)) { //尝试从父实体中查找指定的属性 property = FindBaseProperty(ref properties, parts[i], ref ancestors); //如果父实体中也不含指定的属性则返回失败 if (property == null) { return(EntityPropertyFindResult <T> .Failure(token)); } } //如果回调函数不为空,则调用匹配回调函数 //注意:将回调函数返回的结果作为下一次的用户数据保存起来 if (match != null) { token = match(new EntityPropertyFindContext <T>(string.Join(".", parts, 0, i), token, property, ancestors)); } //清空继承实体链 if (ancestors != null) { ancestors.Clear(); } if (property.IsSimplex) { break; } else { properties = GetAssociatedProperties((IEntityComplexPropertyMetadata)property, ref ancestors); } } //返回查找到的结果 return(new EntityPropertyFindResult <T>(token, property)); }
public MetadataEntity(IMetadata provider, string name, string baseName = null) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } _name = name.Trim(); _baseName = baseName; _provider = provider ?? throw new ArgumentNullException(nameof(provider)); _properties = new MetadataEntityPropertyCollection(this); }