矮小利落的映射到列名中包含空格的SQL列

本文关键字:包含 SQL 空格 映射 | 更新日期: 2023-09-27 17:58:52

我今天已经设法将一些东西作为小型沙盒/POC项目启动并运行,但似乎在一个问题上遇到了麻烦。。。

问题:

有没有一种方法可以让dapper映射到包含空格的SQL列名。

我有这样的效果作为我的结果集。

例如:

SELECT 001 AS [Col 1], 
       901 AS [Col 2],
       00454345345345435349 AS [Col 3],
       03453453453454353458 AS [Col 4] 
FROM [Some Schema].[Some Table]

我的课看起来像这个

    public class ClassA
    {          
        public string Col1 { get; set; }    
        public string Col2 { get; set; }
        ///... etc
     }

目前我的实现是这样的

 public Tuple<IList<TClass>, IList<TClass2>> QueryMultiple<TClass, TClass2>(object parameters)
 {
      List<TClass> output1;
      List<TClass2> output2;
      using (var data = this.Connection.QueryMultiple(this.GlobalParameter.RpcProcedureName, parameters, CommandType.StoredProcedure))
      {
           output1 = data.Read<TClass>().ToList();
           output2 = data.Read<TClass2>().ToList();
      }
      var result = new Tuple<IList<TClass>, IList<TClass2>>(output1, output2);
      return result;
  }

注意:SQL不能以任何方式修改

目前,我正在研究dapper代码,我唯一可以预见的解决方案是添加一些代码来"说服"列比较,但到目前为止运气不佳。

我在StackOverflow上看到了类似dapper扩展的东西,但我希望我可以在不添加扩展的情况下完成这项工作。我会采取最快的措施。

矮小利落的映射到列名中包含空格的SQL列

有一个nuget包Dapper。FluentMap,它允许您添加列名映射(包括空格)。它类似于EntityFramework。

// Entity class.
public class Customer
{
    public string Name { get; set; }
}
// Mapper class.
public class CustomerMapper : EntityMap<Customer>
{
    public CustomerMapper()
    {
        Map(p => p.Name).ToColumn("Customer Name");
    }
}
// Initialise like so - 
FluentMapper.Initialize(a => a.AddMap(new CustomerMapper()));

参见https://github.com/henkmollema/Dapper-FluentMap了解更多信息。

这里的一个选项是通过动态/非通用API,然后通过IDictionary<string,object> API每行获取值,但这可能有点乏味。

作为替代方案,您可以创建一个自定义映射器,并将其告知dapper;例如:

SqlMapper.SetTypeMap(typeof(ClassA), new RemoveSpacesMap());

带有:

class RemoveSpacesMap : Dapper.SqlMapper.ITypeMap
{
    System.Reflection.ConstructorInfo SqlMapper.ITypeMap.FindConstructor(string[] names, Type[] types)
    {
        return null;
    }
    SqlMapper.IMemberMap SqlMapper.ITypeMap.GetConstructorParameter(System.Reflection.ConstructorInfo constructor, string columnName)
    {
        return null;
    }
    SqlMapper.IMemberMap SqlMapper.ITypeMap.GetMember(string columnName)
    {
        var prop = typeof(ClassA).GetProperty(columnName.Replace(" ", ""));
        return prop == null ? null : new PropertyMemberMap(columnName, prop);
    }
    class PropertyMemberMap : Dapper.SqlMapper.IMemberMap
    {
        private string columnName;
        private PropertyInfo property;
        public PropertyMemberMap(string columnName, PropertyInfo property)
        {
            this.columnName = columnName;
            this.property = property;
        }
        string SqlMapper.IMemberMap.ColumnName
        {
            get { throw new NotImplementedException(); }
        }
        System.Reflection.FieldInfo SqlMapper.IMemberMap.Field
        {
            get { return null; }
        }
        Type SqlMapper.IMemberMap.MemberType
        {
            get { return property.PropertyType; }
        }
        System.Reflection.ParameterInfo SqlMapper.IMemberMap.Parameter
        {
            get { return null; }
        }
        System.Reflection.PropertyInfo SqlMapper.IMemberMap.Property
        {
            get { return property; }
        }
    }
}

我在尝试从对系统sp_spaceused过程的调用中获取映射结果时遇到了类似的问题。Marc的代码不太适合我,因为它抱怨找不到默认的构造函数。我还使我的版本通用,以便理论上可以重复使用。这可能不是执行速度最快的代码,但对我来说很有效,在我们的情况下,这些调用很少进行。

class TitleCaseMap<T> : SqlMapper.ITypeMap where T: new()
{
    ConstructorInfo SqlMapper.ITypeMap.FindConstructor(string[] names, Type[] types)
    {
        return typeof(T).GetConstructor(Type.EmptyTypes);
    }
    SqlMapper.IMemberMap SqlMapper.ITypeMap.GetConstructorParameter(ConstructorInfo constructor, string columnName)
    {
        return null;
    }
    SqlMapper.IMemberMap SqlMapper.ITypeMap.GetMember(string columnName)
    {
        string reformattedColumnName = string.Empty;
        foreach (string word in columnName.Replace("_", " ").Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
        {
            reformattedColumnName += char.ToUpper(word[0]) + word.Substring(1).ToLower();
        }
        var prop = typeof(T).GetProperty(reformattedColumnName);
        return prop == null ? null : new PropertyMemberMap(prop);
    }
    class PropertyMemberMap : SqlMapper.IMemberMap
    {
        private readonly PropertyInfo _property;
        public PropertyMemberMap(PropertyInfo property)
        {
            _property = property;
        }
        string SqlMapper.IMemberMap.ColumnName
        {
            get { throw new NotImplementedException(); }
        }
        FieldInfo SqlMapper.IMemberMap.Field
        {
            get { return null; }
        }
        Type SqlMapper.IMemberMap.MemberType
        {
            get { return _property.PropertyType; }
        }
        ParameterInfo SqlMapper.IMemberMap.Parameter
        {
            get { return null; }
        }
        PropertyInfo SqlMapper.IMemberMap.Property
        {
            get { return _property; }
        }
    }
}

我知道这是一个老问题,但我在上一个项目中遇到了同样的问题,所以我只是使用属性创建了一个自己的映射器。

我定义了一个名为ColumnNameAttribute.cs 的属性类

using System;
namespace DapperHelper.Attributes
{
    [System.AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
    sealed class ColumNameAttribute : Attribute
    {
        private string _columName;
        public string ColumnName
        {
            get { return _columName; }
            set { _columName = value; }
        }
        public ColumNameAttribute(string columnName)
        {
            _columName = columnName;
        }
    }
}

在定义了属性之后,我实现了一个动态映射器,它使用Dapper的Query方法,但用作Query<T>:

using Dapper;
using DapperHelper.Attributes;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web.Routing;
namespace DapperHelper.Tools
{
    public class DynamicMapper<T> :IDisposable where  T : class, new()
    {
        private readonly Dictionary<string, PropertyInfo> _propertiesMap;
        public DynamicMapper()
        {
            _propertiesMap = new Dictionary<string, PropertyInfo>();
            PropertyInfo[] propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                if (propertyInfo.GetCustomAttribute(typeof(ColumNameAttribute)) is ColumNameAttribute columNameAttribute)
                {
                    _propertiesMap.Add(columNameAttribute.ColumnName, propertyInfo);
                }
                else
                {
                    _propertiesMap.Add(propertyInfo.Name, propertyInfo);
                }
            }
        }
        public List<T> QueryDynamic(IDbConnection dbConnection, string sqlQuery)
        {
            List<dynamic> results = dbConnection.Query(sqlQuery).ToList();
            List<T> output = new List<T>();
            foreach (dynamic dynObj in results)
            {
                output.Add(AssignPropertyValues(dynObj));
            }
            return output;
        }
        private T AssignPropertyValues(dynamic dynamicObject)
        {
            T output = new T();
            RouteValueDictionary dynamicObjProps = new RouteValueDictionary(dynamicObject);
            foreach (var propName in dynamicObjProps.Keys)
            {
                if (_propertiesMap.TryGetValue(propName, out PropertyInfo propertyMapped)
                    && dynamicObjProps.TryGetValue(propName, out object value))
                {
                    propertyMapped.SetValue(output, value);
                }
            }
            return output;
        }

        public void Dispose()
        {
            _propertiesMap.Clear(); 
        }
    }
}

要使用它,您必须参考Model类并定义属性:

 using DapperHelper.Attributes;
namespace Testing
    {
        public class Sample
        {
            public int SomeColumnData { get; set; }
            [ColumnName("Your Column Name")]
            public string SpecialColumn{ get; set; }
        }
    }

然后你可以实现这样的东西:

DynamicMapper<Sample> mapper = new DynamicMapper<Sample>();
List<Sample> samples = mapper.QueryDynamic(connection, "SELECT * FROM Samples");

我希望它能帮助人们寻找另一种选择。

创建模型类(例如Employee)

public class Employee
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

现在创建一个Mapper类来映射从DB检索到的列(如SQL)。

例如,如果您正在中检索"员工列表"。Net core(Blazor),您的代码可能如下所示:

public async Task<IEnumerable<Employee>> GetEmployeeData()
{
    return await _db.QueryFromSPAsync<Employee, dynamic>("NameOfYourStoredProcedure",new { });
}
// Employee Mapper class
public class EmployeeMapper : EntityMap<Employee>
{
    public EmployeeMapper()
    {
        Map(p => p.ID).ToColumn("ID");
        Map(p => p.FirstName).ToColumn("First Name");
        Map(p => p.LastName).ToColumn("Last Name");
    }
}

您可以初始化映射器如下:

//In your Program.cs
FluentMapper.Initialize(Map =>
    {
        Map.AddMap(new SLAInsuredMapper());
    }
);

注意:不要忘记安装Dapper.FluentMapNuGet软件包