private static bool MatchColumnAttribute(PropertyInfo property, ColumnDescription column)
        {
#if EFOLD
            return(false);
#else
            var columnAttribute = property.GetCustomAttributes(typeof(ColumnAttribute), true).FirstOrDefault();
            if (columnAttribute == null)
            {
                return(false);
            }
            return(((ColumnAttribute)columnAttribute).Name == column.Name);
#endif
        }
 private Expression ToExpression(ParameterExpression parameter, PropertyInfo property, ColumnDescription column)
 {
     if (property == null)
     {
         if (column.Name == table.DiscriminatorColumn)
         {
             return(Expression.Call(Expression.Constant(table), typeof(ObjectDataTable <T>).GetMethod(nameof(GetDiscriminator), BindingFlags.Instance | BindingFlags.NonPublic), parameter));
         }
         var binder = Binder.GetMember(CSharpBinderFlags.None, column.Name, typeof(ObjectData),
                                       new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });
         var expression = Expression.Dynamic(binder, typeof(object), parameter);
         return(Expression.TryCatch(expression, Expression.Catch(typeof(RuntimeBinderException), Expression.Constant(null))));
     }
     return(Expression.Property(parameter, property));
 }
Beispiel #3
0
        /// <summary>
        ///     Loads the table data from the specified table data loader and materializes it
        ///     bases on the specified metadata.
        /// </summary>
        /// <param name="loaderFactory"> The loader factory. </param>
        /// <param name="table"> The table metadata. </param>
        /// <returns> The materialized data. </returns>
        public static IEnumerable<object> Load(
            ITableDataLoaderFactory loaderFactory,
            DbTableInfo table)
        {
            List<ColumnDescription> columns = new List<ColumnDescription>();
            PropertyInfo[] properties = table.EntityType.GetProperties();
            Func<object, object>[] converters = new Func<object,object>[properties.Length];

            for (int i = 0; i < properties.Length; i++)
            {
                PropertyInfo property = properties[i];
                Type type = property.PropertyType;

                // TODO: external
                if (type == typeof(NMemory.Data.Timestamp))
                {
                    converters[i] = ConvertTimestamp;
                    type = typeof(byte[]);
                }
                else if (type == typeof(NMemory.Data.Binary))
                {
                    converters[i] = ConvertBinary;
                    type = typeof(byte[]);
                }

                ColumnDescription column = new ColumnDescription(property.Name, type);
                columns.Add(column);
            }

            TableDescription tableDescription =
                new TableDescription(table.TableName, columns);

            ITableDataLoader loader = loaderFactory.CreateTableDataLoader(tableDescription);

            // Prefetch require info/object to increase performance
            Func<object[], object> initializer = table.EntityInitializer;
            int columnCount = columns.Count;

            // Single array to spare GC
            object[] entityProperties = null;

            foreach (object[] data in loader.GetData())
            {
                if (entityProperties == null)
                {
                    // Initialize at the first element
                    entityProperties = new object[data.Length];
                }

                for (int i = 0; i < columnCount; i++)
                {
                    object propertyValue = data[i];

                    // Use converter if required
                    Func<object, object> converter = converters[i];
                    if (converter != null)
                    {
                        propertyValue = converter.Invoke(propertyValue);
                    }

                    entityProperties[i] = propertyValue;
                }

                yield return initializer.Invoke(entityProperties);
            }
        }
 private static PropertyInfo GetProperty(Type parentType, ColumnDescription column)
 {
     return(parentType.GetProperty(column.Name, PropertyFlags)
            ?? parentType.GetProperties(PropertyFlags)
            .SingleOrDefault(p => MatchColumnAttribute(p, column)));
 }
Beispiel #5
0
        /// <summary>
        ///     Loads the table data from the specified table data loader and materializes it
        ///     bases on the specified metadata.
        /// </summary>
        /// <param name="loaderFactory"> The loader factory. </param>
        /// <param name="table"> The table metadata. </param>
        /// <returns> The materialized data. </returns>
        public static IEnumerable <object> Load(
            ITableDataLoaderFactory loaderFactory,
            DbTableInfo table)
        {
            List <ColumnDescription> columns = new List <ColumnDescription>();

            PropertyInfo[]          properties = table.EntityType.GetProperties();
            Func <object, object>[] converters = new Func <object, object> [properties.Length];

            for (int i = 0; i < properties.Length; i++)
            {
                PropertyInfo property = properties[i];
                Type         type     = property.PropertyType;

                // TODO: external
                if (type == typeof(NMemory.Data.Timestamp))
                {
                    converters[i] = ConvertTimestamp;
                    type          = typeof(byte[]);
                }
                else if (type == typeof(NMemory.Data.Binary))
                {
                    converters[i] = ConvertBinary;
                    type          = typeof(byte[]);
                }

                ColumnDescription column = new ColumnDescription(property.Name, type);
                columns.Add(column);
            }

            TableDescription tableDescription =
                new TableDescription(table.TableName, columns);

            ITableDataLoader loader = loaderFactory.CreateTableDataLoader(tableDescription);

            // Prefetch require info/object to increase performance
            Func <object[], object> initializer = table.EntityInitializer;
            int columnCount = columns.Count;

            // Single array to spare GC
            object[] entityProperties = null;

            foreach (object[] data in loader.GetData())
            {
                if (entityProperties == null)
                {
                    // Initialize at the first element
                    entityProperties = new object[data.Length];
                }

                for (int i = 0; i < columnCount; i++)
                {
                    object propertyValue = data[i];

                    // Use converter if required
                    Func <object, object> converter = converters[i];
                    if (converter != null)
                    {
                        propertyValue = converter.Invoke(propertyValue);
                    }

                    entityProperties[i] = propertyValue;
                }

                yield return(initializer.Invoke(entityProperties));
            }
        }