Esempio n. 1
0
        /// <inheritdoc />
        /// <summary>
        /// Set data from data reader to entity property
        /// </summary>
        /// <param name="entity"> instance of the entity </param>
        /// <param name="reader"> data reader </param>
        /// <returns> the same instance as was passed to the entity param. (To make builder pattern)</returns>
        public override TEntity SetProperty(TEntity entity, DbDataReader reader, ReadSession session, int recursionDepth)
        {
            if (entity != null)
            {
                // Get collection, if not exists create it
                var collection = Getter(entity);
                if (collection == null)
                {
                    if (Setter != null)
                    {
                        Setter(entity, (collection = new List <TProperty>()));
                    }
                    else
                    {
                        throw new InvalidOperationException($"Please make sure that navigation property {typeof(TProperty)} collection is initialized or has ICollection<TProperty> signature");
                    }
                }

                // Get previous entity
                // Check collection is List for optimized access of previous entity
                var previousProperty = default(TProperty);
                if (typeof(List <TProperty>) == collection.GetType())
                {
                    var pkIdx = ForeignTable.PrimaryKey.GetColumnIndexByAlias(reader);
                    if (pkIdx.HasValue)
                    {
                        // TODO: Think about more effective way of reading previous entity
                        if (!ForeignTable.PrimaryKey.IsNull(reader, pkIdx.Value))
                        {
                            var pkValue = ForeignTable.PrimaryKey.GetValue(reader, /*shift*/ pkIdx.Value - ForeignTable.PrimaryKey.Index.Value);
                            previousProperty = collection.FirstOrDefault(e => ForeignTable.PrimaryKey.GetField(e).Equals(pkValue));
                        }
                    }

                    //if (collection.Count > 0)
                    //    previousProperty = ((List<TProperty>)collection)[collection.Count - 1];
                }
                else
                {
                    // Try to find entity with the same key in collection
                    var pkIdx = ForeignTable.PrimaryKey.GetColumnIndexByAlias(reader);
                    if (pkIdx.HasValue)
                    {
                        // TODO: Think about more effective way of reading previous entity
                        if (!ForeignTable.PrimaryKey.IsNull(reader, pkIdx.Value))
                        {
                            var pkValue = ForeignTable.PrimaryKey.GetValue(reader, /*shift*/ pkIdx.Value - ForeignTable.PrimaryKey.Index.Value);
                            previousProperty = collection.FirstOrDefault(e => ForeignTable.PrimaryKey.GetField(e).Equals(pkValue));
                        }
                    }
                }

                var propertyInstance = ForeignTable.ToEntity(reader, previousProperty, session, ++recursionDepth);
                if (propertyInstance != null && propertyInstance != previousProperty)
                {
                    collection?.Add(propertyInstance);
                }
            }
            return(entity);
        }
Esempio n. 2
0
 /// <inheritdoc />
 /// <summary>
 /// Set data from data reader to entity property
 /// </summary>
 /// <param name="entity"> instance of the entity </param>
 /// <param name="reader"> data reader </param>
 /// <returns> the same instance as was passed to the entity param. (To make builder pattern)</returns>
 public override TEntity SetProperty(TEntity entity, DbDataReader reader, ReadSession session, int recursionDepth)
 {
     if (entity != null)
     {
         var previousProperty = Getter(entity);
         var currentProperty  = ForeignTable.ToEntity(reader, previousProperty, session, ++recursionDepth);
         Setter(entity, currentProperty);
     }
     return(entity);
 }