• Asp.Net 6.0 集成 AutoMapper 初入


    AutoMapper

    后端开发中,对外接口时通常使用的实体是DTO,用于数据传输使用。在DTO到数据访问层的时候,需要进行DTO实体到DAO实体类的转换。这时,我们使用AutoMapper可以简化这个转换对象的过程。

    依赖

    NuGet中搜搜AutoMapper进行安装即可

    配置

    创建Profile,配置实体映射关系
    MapperConfigurationExpression 继承自 Profile

    public class AutoMapperProFile:MapperConfigurationExpression
        {
    
            public AutoMapperProFile()
            {
                // 增加实体间的转换
                CreateMap<ToDo, ToDoDto>().ReverseMap();
                CreateMap<Memo, MemoDto>().ReverseMap();
                CreateMap<User, UserDto>().ReverseMap();
                
                /**
                 *  使用 ForMember 对映射规则做进一步的加工,可以使用自定义转换器
                CreateMap()
                .ForMember(destination => destination.CommentCounts, source => source.MapFrom(i => i.Comments.Count()));
                **/
    
    
            }
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    配置到Asp.Net IOC中

    
    #region 注册AutoMapper
    var autoMapperConfiguration = new MapperConfiguration(config =>
    {
        config.AddProfile(new AutoMapperProFile());
    });
    
    builder.Services.AddSingleton(autoMapperConfiguration.CreateMapper());
    #endregion
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    使用

    public class ToDoService : IToDoService
        {
    
            public IUnitOfWork Uow { get; }
            public ILogger<ToDoService> Logger { get; }
            public IMapper Mapper { get; }
    
            public ToDoService(IUnitOfWork uow, ILogger<ToDoService> logger,IMapper mapper)
            {
                Uow = uow;
                Logger = logger;
                Mapper = mapper;
            }
    		
    		public async Task<ApiResponse> AddAsync(ToDoDto model)
            {
                try
                {
                    // AutoMapper 转换
                    // var items = Mapper.Map>(toDos.Items);
                    var Todo = Mapper.Map<ToDo>(model);
                    IRepository<ToDo> repository = Uow.GetRepository<ToDo>();
                    await repository.InsertAsync(Todo);
                    if (await Uow.SaveChangesAsync() > 0)
                    {
                        return new ApiResponse(true, Todo);
                    }
                    return new ApiResponse("添加数据失败");
                }
                catch (Exception ex)
                {
                    return new ApiResponse(ex.Message);
                }
    
    
            }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    解决循环依赖import cycle not allowed的多个办法
    使用docker容器搭建MySQL主从复制
    2024年GPLT团体程序设计竞赛题解(无L3-3)
    vue3+three.js实现疫情可视化
    docker 容器中安装cron,却无法启动定时任务
    论文阅读【3】Efficient Estimation of Word Representations in Vector Space
    vue3 ref的使用
    #gStore-weekly | gAnswer源码解析 调用NE模块流程
    红黑树你需要了解一下
    【深度学习】 Python 和 NumPy 系列教程(八):Python类
  • 原文地址:https://blog.csdn.net/xiuyuandashen/article/details/132857095