/// <summary> /// Checks, is property of entity represents it's primary key. /// </summary> protected abstract bool IsPrimaryKey([NotNull] string propertyName, [NotNull] IAdoEntityInfo mainEntityInfo, [CanBeNull] Includeon includeon);
/// <summary> /// Checks, is property represend foreign key to main entity from the included. /// </summary> protected virtual bool IsKeyOfMainEntityInForeign([NotNull] string propertyName, [NotNull] IAdoEntityInfo mainEntityInfo, [NotNull] Includeon includeon) { return(propertyName == includeon.ForeignKeyFromCurrentEntityToMain); }
/// <summary> /// Returns one single entity, by reading it's data from <paramref name="reader"/>. /// It uses <paramref name="includeon"/> if it was specified, otherwise - it uses <paramref name="mainEntityInfo"/>. /// It also return identifier of entity in <paramref name="keyId"/> outer parameter. /// </summary> /// <param name="reader">Reader contained entities data.</param> /// <param name="mainEntityInfo">Info about entity. Used if <paramref name="includeon"/> is not specified (equals to null).</param> /// <param name="includeon">Info about includeon. Can be unspecified. See <see cref="IncludeChildrenAttribute"/> for more details.</param> /// <param name="keyId"> /// Identifier of returned entity. /// If <paramref name="includeon"/> parameter unspecified, or represents single field includeon - <paramref name="keyId"/> will contain primary key of returned entity. /// If <paramref name="includeon"/> specified and represents sum-collection of main entity (<see cref="IIncludeon.IsCollection"/>) /// - <paramref name="keyId"/> will contain key primary key of main entity (not the key of returned one). /// </param> /// <returns>Main entity (when <paramref name="includeon"/> unspecified) or entity described by <paramref name="includeon"/>.</returns> protected virtual object ReadItem(DbDataReader reader, IAdoEntityInfo mainEntityInfo, [CanBeNull] Includeon includeon, out object keyId) { var entityType = includeon?.Info.EntityType ?? mainEntityInfo.EntityType; var item = Activator.CreateInstance(entityType); keyId = null; var isKey = includeon == null ? IsPrimaryKey : includeon.IsCollection ? (Func <string, IAdoEntityInfo, Includeon, bool>)IsKeyOfMainEntityInForeign : IsPrimaryKey; var columns = includeon?.Info.ReaderColumns ?? mainEntityInfo.ReaderColumns; for (var i = 0; i < columns.Length; i++) { var name = columns[i]; var value = reader.GetValue(i); if (value == null || value.GetType().FullName == "System.DBNull") { continue; } if (isKey(name, mainEntityInfo, includeon)) { keyId = value; } if (!item.TrySet(name, value)) { Log.TraceEvent(TraceEventType.Warning, $"Can't set property {name} from '{entityType.FullName}' context."); } } return(item); }