一、概述
AutoMapper是用于DTO对象与实体之间的映射,有两种方式:
第一种:使用IObjectMapper接口
第二种:特性注入
二、使用
2.1 使用IObjectMapper接口
public class UserAppService : ApplicationService
{
private readonly IObjectMapper _objectMapper;
public UserAppService(IObjectMapper objectMapper)
{
_objectMapper = objectMapper;
}
public void CreateUser(CreateUserInput input)
{
var user = _objectMapper.Map(input);
}
}
2.2特性注入
[AutoMapTo(typeof(User))]
public class CreateUserInput
{
public string Name { get; set; }
public string Surname { get; set; }
public string EmailAddress { get; set; }
public string Password { get; set; }
}