示例#1
0
        public static void AddParams(this DbCommand command, object param)
        {
            _ = command ?? throw new ArgumentNullException($"{nameof(command)} must not be null!");
            _ = param ?? throw new ArgumentNullException($"{nameof(param)} must not be null!");

            var properties = AttributeParser.GetAllProperties(param.GetType()).ToList();

            foreach (var propertyInfo in properties)
            {
                object value = null;

                if (AttributeParser.HasForeignKey(propertyInfo))
                {
                    value = AttributeParser.GetKey(propertyInfo.PropertyType)
                            .GetValue(propertyInfo.GetValue(param));
                }
                // Enum or Nullable<Enum>
                else if (propertyInfo.PropertyType.IsEnum ||
                         propertyInfo.PropertyType.IsGenericType &&
                         propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>) &&
                         propertyInfo.PropertyType.GetGenericArguments()[0].IsEnum &&
                         propertyInfo.GetValue(param) != null)
                {
                    value = propertyInfo.GetValue(param).ToString();
                }

                command.AddParam(propertyInfo.Name, value ?? propertyInfo.GetValue(param));
            }
        }
        public static async Task <IEnumerable <TEntity> > Query <TEntity>(this DbConnection connection, string sql,
                                                                          object param = null)
        {
            _ = connection ?? throw new ArgumentNullException($"{nameof(connection)} must not be null!");

            using var command   = connection.CreateCommand();
            command.CommandText = sql;
            if (param != null)
            {
                command.AddParams(param);
            }

            var items = new List <TEntity>();

            using var reader = await command.ExecuteReaderAsync().ConfigureAwait(false);

            while (reader.Read())
            {
                var entity     = (TEntity)Activator.CreateInstance(typeof(TEntity));
                var properties = AttributeParser.GetAllProperties(entity.GetType()).ToList();
                if (typeof(TEntity) == typeof(int))
                {
                    entity = (TEntity)Convert.ChangeType(reader[0], typeof(Int32), CultureInfo.InvariantCulture);
                }
                foreach (var propertyInfo in properties)
                {
                    if (AttributeParser.HasForeignKey(propertyInfo))
                    {
                        MethodInfo method      = typeof(MapperExtensions).GetMethod("Get");
                        MethodInfo generic     = method.MakeGenericMethod(propertyInfo.PropertyType);
                        var        columnValue = Convert.ChangeType(reader[AttributeParser.GetColumnName(propertyInfo)], typeof(int), CultureInfo.InvariantCulture);
                        var        task        = (Task)generic.Invoke(null, new object[] { connection, columnValue });
                        await task.ConfigureAwait(false);

                        var resultProperty = task.GetType().GetProperty("Result");
                        propertyInfo.SetValue(entity, resultProperty.GetValue(task));
                    }
                    else if (propertyInfo.PropertyType.IsEnum)
                    {
                        var parsedEnum = Enum.Parse(propertyInfo.PropertyType, (string)reader[AttributeParser.GetColumnName(propertyInfo)]);
                        propertyInfo.SetValue(entity, parsedEnum);
                    }
                    else if (!reader.IsDBNull(reader.GetOrdinal(AttributeParser.GetColumnName(propertyInfo))))
                    {
                        var value = Convert.ChangeType(reader[AttributeParser.GetColumnName(propertyInfo)],
                                                       propertyInfo.PropertyType,
                                                       CultureInfo.InvariantCulture);
                        propertyInfo.SetValue(entity, value);
                    }
                }

                items.Add(entity);
            }

            return(items);
        }