Automapper 映射 属性 意外地通过部分 属性 名称匹配
Automapper mapping property unexpectedly by partial property name match
我有两个 class 被映射。源 class 有一个 DateTime
属性 映射到目标 属性 类型 long
(DateTime.Ticks
),它们是 UpdateDt
和 UpdateDtTicks
分别。
当我使用 Automapper 映射这两个 class 时,我的 UpdateDtTicks
属性 自动从 UpdateDt
属性 获取值,即使属性 名称不一样,我还没有为此 属性 明确设置映射。
Automapper 是否会自动设置 属性,因为 属性 名称仅在末尾不同?如果不是,为什么会发生这种情况,因为这是意外行为。
请看下面的代码:
static void Main(string[] args)
{
Configuration.Configure();
var person = new OrderDto
{
OrderId = 999,
MyDate = new DateTime(2015, 1, 1, 4, 5, 6)
};
var orderModel = Mapper.Map<OrderModel>(person);
Console.WriteLine(new DateTime(orderModel.MyDateTicks.Value));
Console.ReadLine();
}
public class OrderDto
{
public int OrderId { get; set; }
public DateTime MyDate { get; set; }
}
public class OrderModel
{
public int OrderId { get; set; }
public long? MyDateTicks { get; set; }
}
public class Configuration
{
public static void Configure()
{
Mapper.CreateMap<OrderDto, OrderModel>();
}
}
控制台中的结果:
还有手表:
您正在触发 AutoMapper 的展平功能。来自 documentation:
When you configure a source/destination type pair in AutoMapper, the configurator attempts to match properties and methods on the source type to properties on the destination type. If for any property on the destination type a property, method, or a method prefixed with "Get" does not exist on the source type, AutoMapper splits the destination member name into individual words (by PascalCase conventions).
因此,给出以下示例(也来自他们的文档,在我的回答中缩短):
public class Order
{
public Customer Customer { get; set; }
}
public class Customer
{
public string Name { get; set; }
}
public class OrderDto
{
public string CustomerName { get; set; }
public decimal Total { get; set; }
}
Mapper.CreateMap<Order, OrderDto>();
var order = new Order
{
Customer = new Customer
{
Name = "John Doe"
}
};
OrderDto dto = Mapper.Map<Order, OrderDto>(order);
The CustomerName property matched to the Customer.Name property on Order.
这与 MyDateTicks
匹配 MyDate.Ticks
完全相同,其中 returns 需要 long
...
我有两个 class 被映射。源 class 有一个 DateTime
属性 映射到目标 属性 类型 long
(DateTime.Ticks
),它们是 UpdateDt
和 UpdateDtTicks
分别。
当我使用 Automapper 映射这两个 class 时,我的 UpdateDtTicks
属性 自动从 UpdateDt
属性 获取值,即使属性 名称不一样,我还没有为此 属性 明确设置映射。
Automapper 是否会自动设置 属性,因为 属性 名称仅在末尾不同?如果不是,为什么会发生这种情况,因为这是意外行为。
请看下面的代码:
static void Main(string[] args)
{
Configuration.Configure();
var person = new OrderDto
{
OrderId = 999,
MyDate = new DateTime(2015, 1, 1, 4, 5, 6)
};
var orderModel = Mapper.Map<OrderModel>(person);
Console.WriteLine(new DateTime(orderModel.MyDateTicks.Value));
Console.ReadLine();
}
public class OrderDto
{
public int OrderId { get; set; }
public DateTime MyDate { get; set; }
}
public class OrderModel
{
public int OrderId { get; set; }
public long? MyDateTicks { get; set; }
}
public class Configuration
{
public static void Configure()
{
Mapper.CreateMap<OrderDto, OrderModel>();
}
}
控制台中的结果:
还有手表:
您正在触发 AutoMapper 的展平功能。来自 documentation:
When you configure a source/destination type pair in AutoMapper, the configurator attempts to match properties and methods on the source type to properties on the destination type. If for any property on the destination type a property, method, or a method prefixed with "Get" does not exist on the source type, AutoMapper splits the destination member name into individual words (by PascalCase conventions).
因此,给出以下示例(也来自他们的文档,在我的回答中缩短):
public class Order
{
public Customer Customer { get; set; }
}
public class Customer
{
public string Name { get; set; }
}
public class OrderDto
{
public string CustomerName { get; set; }
public decimal Total { get; set; }
}
Mapper.CreateMap<Order, OrderDto>();
var order = new Order
{
Customer = new Customer
{
Name = "John Doe"
}
};
OrderDto dto = Mapper.Map<Order, OrderDto>(order);
The CustomerName property matched to the Customer.Name property on Order.
这与 MyDateTicks
匹配 MyDate.Ticks
完全相同,其中 returns 需要 long
...