C# AutoMapper 使用方法总结

2020-06-15 17:00:09王振洲

具体来说,支持的源集合类型包括:

IEnumerable IEnumerable ICollection ICollection IList IList List Arrays

映射到现有集合时,将首先清除目标集合。如果这不是你想要的,请查看AutoMapper.Collection。

5.1 处理空集合

映射集合属性时,如果源值为 null,则 AutoMapper 会将目标字段映射为空集合,而不是 null。这与 Entity Framework 和 Framework Design Guidelines 的行为一致,认为 C# 引用,数组,List,Collection,Dictionary 和 IEnumerables 永远不应该为 null

5.2 集合中的多态

这个官方的文档不是很好理解。我重新举个例子。实体类如下:

public class Employee
{
 public int ID { get; set; }

 public string Name { get; set; }
}

public class Employee2 : Employee
{
 public string DeptName { get; set; }
}

public class EmployeeDto
{
 public int ID { get; set; }

 public string Name { get; set; }
}

public class EmployeeDto2 : EmployeeDto
{
 public string DeptName { get; set; }
}

数组映射代码如下:

var config = new MapperConfiguration(cfg =>
{
 cfg.CreateMap<Employee, EmployeeDto>().Include<Employee2, EmployeeDto2>();
 cfg.CreateMap<Employee2, EmployeeDto2>();
});
IMapper mapper = config.CreateMapper();

var employees = new[]
{
 new Employee { ID = 1, Name = "Tom" },
 new Employee2 { ID = 2, Name = "Jerry", DeptName = "R & D" }
};

var dto = mapper.Map<Employee[], EmployeeDto[]>(employees);

可以看到,映射后,dto 中两个元素的类型,一个是 EmployeeDto,一个是 EmployeeDto2,即实现了父类映射到父类,子类映射到子类。

如果去掉 Include 方法,则映射后 dto 中两个元素的类型均为 EmployeeDto。

6 方法到属性映射

AutoMapper 不仅能实现属性到属性映射,还可以实现方法到属性的映射,并且不需要任何配置,方法名可以和属性名一致,也可以带有 Get 前缀。

例如下例的 Employee.GetFullName() 方法,可以映射到 EmployeeDto.FullName 属性。

public class Employee
{
 public int ID { get; set; }

 public string FirstName { get; set; }

 public string LastName { get; set; }

 public string GetFullName()
 {
  return $"{FirstName} {LastName}";
 }
}

public class EmployeeDto
{
 public int ID { get; set; }

 public string FirstName { get; set; }

 public string LastName { get; set; }

 public string FullName { get; set; }
}

7 自定义映射

当源类型与目标类型名称不一致时,或者需要对源数据做一些转换时,可以用自定义映射。

public class Employee
{
 public int ID { get; set; }

 public string Name { get; set; }

 public DateTime JoinTime { get; set; }
}

public class EmployeeDto
{
 public int EmployeeID { get; set; }

 public string EmployeeName { get; set; }

 public int JoinYear { get; set; }
}