AutoMapper 基本使用

参考

【C#】AutoMapper 使用手册 - 丹枫无迹 - 博客园 (cnblogs.com)

环境搭建

AutoMapper.Extensions.Microsoft.DependencyInjection

服务注册

builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

创建实体类

转换前类

csharp 复制代码
namespace AutoMapper_01.Entities
{
    public class User
    {
        public int Id { get; set; }
        public string firstName {  get; set; }
        public string lastName { get; set; }
        public bool gender { get; set; }

        public DateTime DateOfBirth { get; set; }   
    }
}

转换后类

csharp 复制代码
namespace AutoMapper_01.Entities
{
    public class UserDto
    {
        public int Id { get; set; }
        public string name { set;get; }
        public bool sex { get; set; }

        public int Age { get; set; }
    }
}

创建映射关系

typescript 复制代码
using AutoMapper;
using AutoMapper_01.Entities;

namespace AutoMapper_01.profiles
{
    public class UserProfile:Profile
    {
        public UserProfile()
        {
            CreateMap<User,UserDto>()
                .ForMember(
                    dest=>dest.sex,
                    opt => opt.MapFrom(src=>src.gender)
                )
                .ForMember(
                    dest => dest.name,
                    opt => opt.MapFrom(src => $"{src.firstName} {src.lastName}")
                )
                .ForMember(
                    dest => dest.Age,
                    opt => opt.MapFrom(src => DateTime.Now.Year - src.DateOfBirth.Year)
                );
        }
    }
}

测试

ini 复制代码
using AutoMapper;
using AutoMapper_01.Entities;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace AutoMapper_01.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class Demo : ControllerBase
    {
        private readonly IMapper _mapper;

        public Demo(IMapper mapper)
        {
            _mapper = mapper;
        }

        [HttpGet(Name = "Test")]
        public Object Get()
        {
            User user = new User{
                Id = 1,
                firstName = "f1",
                lastName = "l1",
                gender = true,
                DateOfBirth = new DateTime(1970,10,6) ,
            };
            Console.WriteLine(user.Id);
            var userDto = _mapper.Map<UserDto>(user);

            User user2 = new User{
                Id = 2,
                firstName = "f2",
                lastName = "l2",
                gender = false,
                DateOfBirth = new DateTime(2019, 1, 26),
            };
            List<User> users = new List<User>();
            users.Add(user);
            users.Add(user2);
            var userDtos = _mapper.Map<List<UserDto>>(users);

            return new {
                userDto = userDto,
                userDtos = userDtos
            }; 
        }
    }
}
相关推荐
啦啦右一6 分钟前
Spring Boot | (一)Spring开发环境构建
spring boot·后端·spring
森屿Serien7 分钟前
Spring Boot常用注解
java·spring boot·后端
盛派网络小助手2 小时前
微信 SDK 更新 Sample,NCF 文档和模板更新,更多更新日志,欢迎解锁
开发语言·人工智能·后端·架构·c#
∝请叫*我简单先生2 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl
zquwei3 小时前
SpringCloudGateway+Nacos注册与转发Netty+WebSocket
java·网络·分布式·后端·websocket·网络协议·spring
dessler4 小时前
Docker-run命令详细讲解
linux·运维·后端·docker
Q_19284999064 小时前
基于Spring Boot的九州美食城商户一体化系统
java·spring boot·后端
ZSYP-S5 小时前
Day 15:Spring 框架基础
java·开发语言·数据结构·后端·spring
Yuan_o_5 小时前
Linux 基本使用和程序部署
java·linux·运维·服务器·数据库·后端