我是Dapper micro ORM的新手。到目前为止,我能够将它用于简单的ORM相关内容,但我无法使用类属性映射数据库列名。
例如,我有以下数据库表:
Table Name: Person
person_id int
first_name varchar(50)
last_name varchar(50)
我有一个名为Person的类:
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
请注意,表中的列名与我尝试映射查询结果的数据的类的属性名不同。
var sql = @"select top 1 PersonId,FirstName,LastName from Person";
using (var conn = ConnectionFactory.GetConnection())
{
var person = conn.Query<Person>(sql).ToList();
return person;
}
上面的代码不起作用,因为列名与对象的(Person)属性不匹配。在这种情况下,我可以在Dapper中手动映射(例如person_id => PersonId
)列名与对象属性吗?
这很好用:
var sql = @"select top 1 person_id PersonId, first_name FirstName, last_name LastName from Person";
using (var conn = ConnectionFactory.GetConnection())
{
var person = conn.Query<Person>(sql).ToList();
return person;
}
Dapper没有允许你指定Column Attribute的工具,我不反对添加对它的支持,只要我们不引入依赖。
如果您使用.NET 4.5.1或更高版本的checkout using (var sqlConnection = new SqlConnection("your_connection_string"))
{
var sqlStatement = "SELECT " +
"db_column_name1, " +
"db_column_name2 " +
"FROM your_table";
return sqlConnection.Query<Section>(sqlStatement).AsList();
}
来映射LINQ样式。它允许您将db映射与模型完全分离(无需注释)
这是其他答案的后盾。这只是我管理查询字符串的一个想法。
Person.cs
Dapper.FluentColumnMapping
API方法
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public static string Select()
{
return $"select top 1 person_id {nameof(PersonId)}, first_name {nameof(FirstName)}, last_name {nameof(LastName)}from Person";
}
}
对于使用Dapper 1.12的所有人,以下是完成此操作所需的操作:
using (var conn = ConnectionFactory.GetConnection())
{
var person = conn.Query<Person>(Person.Select()).ToList();
return person;
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property]
public class ColumnAttribute : Attribute
{
public string Name { get; set; }
public ColumnAttribute(string name)
{
this.Name = name;
}
}
并评论出来。map = new DefaultTypeMap(type);
Kaleb Pederson的解决方案对我有用。我更新了ColumnAttributeTypeMapper以允许自定义属性(在同一域对象上需要两个不同的映射)和更新的属性,以便在需要派生字段和类型不同的情况下允许私有setter。
map = new CustomPropertyTypeMap(type, (t, columnName) =>
{
PropertyInfo pi = t.GetProperties().FirstOrDefault(prop =>
prop.GetCustomAttributes(false)
.OfType<ColumnAttribute>()
.Any(attr => attr.Name == columnName));
return pi != null ? pi : t.GetProperties().FirstOrDefault(prop => prop.Name == columnName);
});
我知道这是一个相对陈旧的线程,但我想我会抛弃我在那里做的事情。
我希望属性映射能够全局工作。您可以匹配属性名称(也就是默认值),也可以匹配类属性上的列属性。我也不想为我映射到的每个类设置它。因此,我创建了一个我在app start上调用的DapperStart类:
public class ColumnAttributeTypeMapper<T,A> : FallbackTypeMapper where A : ColumnAttribute
{
public ColumnAttributeTypeMapper()
: base(new SqlMapper.ITypeMap[]
{
new CustomPropertyTypeMap(
typeof(T),
(type, columnName) =>
type.GetProperties( BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(prop =>
prop.GetCustomAttributes(true)
.OfType<A>()
.Any(attr => attr.Name == columnName)
)
),
new DefaultTypeMap(typeof(T))
})
{
//
}
}
很简单。不知道在我写这篇文章时我会遇到什么问题,但它确实有效。
Kaleb试图解决的问题的简单解决方案就是在列属性不存在时接受属性名称:
public static class DapperStart
{
public static void Bootstrap()
{
Dapper.SqlMapper.TypeMapProvider = type =>
{
return new CustomPropertyTypeMap(typeof(CreateChatRequestResponse),
(t, columnName) => t.GetProperties().FirstOrDefault(prop =>
{
return prop.Name == columnName || prop.GetCustomAttributes(false).OfType<ColumnAttribute>()
.Any(attr => attr.Name == columnName);
}
));
};
}
}
Dapper现在支持自定义列到属性映射器。它通过ITypeMap界面实现。 Qapxswpoi课程由Dapper提供,可以完成大部分工作。例如:
CustomPropertyTypeMap
而型号:
Dapper.SqlMapper.SetTypeMap(
typeof(TModel),
new CustomPropertyTypeMap(
typeof(TModel),
(type, columnName) =>
type.GetProperties().FirstOrDefault(prop =>
prop.GetCustomAttributes(false)
.OfType<ColumnAttribute>()
.Any(attr => attr.Name == columnName))));
值得注意的是,CustomPropertyTypeMap的实现要求该属性存在并匹配其中一个列名,否则将不会映射该属性。 public class TModel {
[Column(Name="my_property")]
public int MyProperty { get; set; }
}
类提供标准功能,可以利用它来改变这种行为:
DefaultTypeMap
有了它,就可以很容易地创建一个自定义类型映射器,如果它们存在,它们将自动使用属性,否则将回退到标准行为:
public class FallbackTypeMapper : SqlMapper.ITypeMap
{
private readonly IEnumerable<SqlMapper.ITypeMap> _mappers;
public FallbackTypeMapper(IEnumerable<SqlMapper.ITypeMap> mappers)
{
_mappers = mappers;
}
public SqlMapper.IMemberMap GetMember(string columnName)
{
foreach (var mapper in _mappers)
{
try
{
var result = mapper.GetMember(columnName);
if (result != null)
{
return result;
}
}
catch (NotImplementedException nix)
{
// the CustomPropertyTypeMap only supports a no-args
// constructor and throws a not implemented exception.
// to work around that, catch and ignore.
}
}
return null;
}
// implement other interface methods similarly
// required sometime after version 1.13 of dapper
public ConstructorInfo FindExplicitConstructor()
{
return _mappers
.Select(mapper => mapper.FindExplicitConstructor())
.FirstOrDefault(result => result != null);
}
}
这意味着我们现在可以轻松支持需要使用属性的地图的类型:
public class ColumnAttributeTypeMapper<T> : FallbackTypeMapper
{
public ColumnAttributeTypeMapper()
: base(new SqlMapper.ITypeMap[]
{
new CustomPropertyTypeMap(
typeof(T),
(type, columnName) =>
type.GetProperties().FirstOrDefault(prop =>
prop.GetCustomAttributes(false)
.OfType<ColumnAttribute>()
.Any(attr => attr.Name == columnName)
)
),
new DefaultTypeMap(typeof(T))
})
{
}
}
这是一个Dapper.SqlMapper.SetTypeMap(
typeof(MyModel),
new ColumnAttributeTypeMapper<MyModel>());
。
有一段时间,以下应该有效:
Gist to the full source code
这是一个简单的解决方案,不需要允许您将基础设施代码保留在POCO之外的属性。
这是一个处理映射的类。如果映射了所有列,字典将起作用,但此类允许您仅指定差异。此外,它还包括反向映射,因此您可以从列中获取字段,从字段中获取列,这在执行生成sql语句等操作时非常有用。
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
设置ColumnMap对象并告诉Dapper使用映射。
public class ColumnMap
{
private readonly Dictionary<string, string> forward = new Dictionary<string, string>();
private readonly Dictionary<string, string> reverse = new Dictionary<string, string>();
public void Add(string t1, string t2)
{
forward.Add(t1, t2);
reverse.Add(t2, t1);
}
public string this[string index]
{
get
{
// Check for a custom column map.
if (forward.ContainsKey(index))
return forward[index];
if (reverse.ContainsKey(index))
return reverse[index];
// If no custom mapping exists, return the value passed in.
return index;
}
}
}
我使用动态和LINQ执行以下操作:
var columnMap = new ColumnMap();
columnMap.Add("Field1", "Column1");
columnMap.Add("Field2", "Column2");
columnMap.Add("Field3", "Column3");
SqlMapper.SetTypeMap(typeof (MyClass), new CustomPropertyTypeMap(typeof (MyClass), (type, columnName) => type.GetProperty(columnMap[columnName])));
取自目前在Dapper 1.42上的 var sql = @"select top 1 person_id, first_name, last_name from Person";
using (var conn = ConnectionFactory.GetConnection())
{
List<Person> person = conn.Query<dynamic>(sql)
.Select(item => new Person()
{
PersonId = item.person_id,
FirstName = item.first_name,
LastName = item.last_name
}
.ToList();
return person;
}
。
Dapper Tests
Helper类从Description属性中获取名称(我个人使用过类似@kalebs的例子)
// custom mapping
var map = new CustomPropertyTypeMap(typeof(TypeWithMapping),
(type, columnName) => type.GetProperties().FirstOrDefault(prop => GetDescriptionFromAttribute(prop) == columnName));
Dapper.SqlMapper.SetTypeMap(typeof(TypeWithMapping), map);
类
static string GetDescriptionFromAttribute(MemberInfo member)
{
if (member == null) return null;
var attrib = (DescriptionAttribute)Attribute.GetCustomAttribute(member, typeof(DescriptionAttribute), false);
return attrib == null ? null : attrib.Description;
}
实现此目的的一种简单方法是在查询中的列上使用别名。如果您的数据库列是public class TypeWithMapping
{
[Description("B")]
public string A { get; set; }
[Description("A")]
public string B { get; set; }
}
并且您的对象的属性是PERSON_ID
,您可以在查询中执行ID
,Dapper将按预期选择它。
与映射混淆是边缘进入真正的ORM土地。而不是与它斗争并使Dapper保持其真正的简单(快速)形式,只需稍微修改您的SQL:
select PERSON_ID as Id ...
在打开与数据库的连接之前,请为每个poco类执行以下代码:
var sql = @"select top 1 person_id as PersonId,FirstName,LastName from Person";
然后将数据注释添加到您的poco类,如下所示:
// Section
SqlMapper.SetTypeMap(typeof(Section), new CustomPropertyTypeMap(
typeof(Section), (type, columnName) => type.GetProperties().FirstOrDefault(prop =>
prop.GetCustomAttributes(false).OfType<ColumnAttribute>().Any(attr => attr.Name == columnName))));
在那之后,你们都准备好了。只需进行查询调用,例如:
public class Section
{
[Column("db_column_name1")] // Side note: if you create aliases, then they would match this.
public int Id { get; set; }
[Column("db_column_name2")]
public string Title { get; set; }
}