一、AutoMapper说明

1.AutoMapper是一个对象-对象映射器。对象-对象映射通过将一种类型的输入对象转换为另一种类型的输出对象来工作。

二、.NET Core中配置AutoMapper

1.NuGet安装AutoMapper.Extensions.Microsoft.DependencyInjection

2.创建配置文件,并添加映射配置
需要继承AutoMapper中的Profile,

public class AutoMapperProfiles : Profile
{
    public AutoMapperProfiles()
    {
        //构造函数中创建映射关系
        CreateMap<UserCreateInput, User>();
    }
}

3.在Startup启动类中的ConfigureServices方法中将服务添加到容器

AutoMapperProfiles是上面步骤中定义的配置文件

services.AddAutoMapper(typeof(AutoMapperProfiles));

三、映射示例

1.简单用法
(1) 定义Model

namespace Model.AutoMapper
{
    public class UserEntity
    {
        public Guid UserId { get; set; }
        public string UserName { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }
        public int IsActive { get; set; }
        public DateTime CreateDate { get; set; }
    }

    public class UserCreateInput
    {
        public string UserName { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }
    }
   
    public class UserCreateOutput
    {
        public Guid UserId { get; set; }
        public string UserName { get; set; }
        public int IsActive { get; set; }
    }
}

(2) 配置Profiles

public class AutoMapperProfiles : Profile
{
    //构造函数中创建映射关系
    public AutoMapperProfiles()
    {
        CreateMap<UserCreateInput, UserEntity>();
        CreateMap<UserEntity, UserCreateOutput>();
    }
}

说明:
CreateMap创建时是单项映射, 例如CreateMap<UserCreateInput, UserEntity>(),映射只能是从UserCreateInput映射成UserEntity;
如果想要两个相互映射就必须CreateMap<UserCreateInput, UserEntity>().ReverseMap();

(3)模型映射

public class AutoMapperController : ControllerBase
{
    private IMapper mapper;
    public AutoMapperController(IMapper mapper)
    {
        //利用构造函数的依赖注入,注入对象IMapper
        this.mapper = mapper;
    }
    
    public UserCreateOutput Create()
    {
        //输入参数UserCreateInput模拟
        UserCreateInput input = new UserCreateInput
        {
            UserName = "小张",
            Age = 20,
            Sex = 1
        };

        //对输入参数UserCreateInput进行映射,映射成实体User
        UserEntity user = mapper.Map<UserEntity>(input);

        UserEntity restuleUser = UserEntityInsert(user);

        //对实体User进行映射,映射成输出参数UserCreateOutput
        var output = mapper.Map<UserCreateOutput>(restuleUser);

        return output;
    }

    private UserEntity UserEntityInsert(UserEntity entity)
    {
        //模拟插入后返回
        entity.UserId = Guid.NewGuid();
        entity.IsActive = 1;
        entity.CreateDate = DateTime.Now;
        return entity;
    }
    
}

2.相互映射就必须CreateMap<UserCreateInput, UserEntity>().ReverseMap()

public class AutoMapperProfiles : Profile
{
    //构造函数中创建映射关系
    public AutoMapperProfiles()
    {
        CreateMap<UserCreateInput, UserEntity>().ReverseMap();
    }
}

public void Create1()
{
    //输入参数UserCreateInput模拟
    UserCreateInput input = new UserCreateInput
    {
        UserName = "小张",
        Age = 20,
        Sex = 1
    };

    //对输入参数UserCreateInput进行映射,映射成实体UserEntity
    UserEntity user = mapper.Map<UserEntity>(input);

    user.UserId = Guid.NewGuid();
    user.IsActive = 1;
    user.CreateDate = DateTime.Now;
    user.UserName = "小李";

    //对实体UserEntity进行映射,映射成输出参数UserCreateInput
    var output = mapper.Map<UserCreateInput>(user);
}

四、异常示例:

1.Missing type map configuration or unsupported mapping. 因为没有创建映射关系CreateMap<T1, T2>();

五、AutoMapper官网

AutoMapper官网:

http://automapper.org/

AutoMapper官网文档:

https://automapper.readthedocs.io/en/latest/

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐