闽公网安备 35020302035485号
3.简化代码结构,提高代码可读性和可维护性。

public class PersonInfo
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Nationality { get; set; }
}
PersonInfoDto的目标对象 public class PersonInfoDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Nationality { get; set; }
}
配置AutoMapper映射规则 public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<PersonInfo, PersonInfoDto>();
}
}
控制台执行对象映射var configuration = new MapperConfiguration(cfg => {
cfg.AddProfile<MappingProfile>();
//或者下面这种方式
//cfg.CreateMap<PersonInfo, PersonInfoDto>();
});
var mapper = configuration.CreateMapper();
var personInfo = new PersonInfo
{
//堆代码 duidaima.com
FirstName = "大东",
LastName = "陈",
Age = 18,
Nationality = "中国"
};
var personInfoDto = mapper.Map<PersonInfoDto>(personInfo);
