.net core 3.1 WebAPi 使用 AutoMapper 9.0

AutoMapper 可以很方便完成数据对象之间的转换。

Dto -> Entity

Entity -> ViewModel

 

Step 1:通过 NuGet 安装 AutoMapper 的包。

.net core 3.1 WebAPi 使用 AutoMapper 9.0

 

 

 

.net core 3.1 WebAPi 使用 AutoMapper 9.0.net core 3.1 WebAPi 使用 AutoMapper 9.0
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AutoMapper" Version="9.0.0" />
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
  </ItemGroup>


</Project>
Project.csproj

 

准备两个类 User 和 Department

.net core 3.1 WebAPi 使用 AutoMapper 9.0.net core 3.1 WebAPi 使用 AutoMapper 9.0
    public class User
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public string Remark { get; set; }

        public int DepartmentId { get; set; }

        public Department DepartmentInfo { get; set; }
    }
User
.net core 3.1 WebAPi 使用 AutoMapper 9.0.net core 3.1 WebAPi 使用 AutoMapper 9.0
    public class Department
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Remark { get; set; }
    }
Department

 

创建 User 的 Dto 和 ViewModel

.net core 3.1 WebAPi 使用 AutoMapper 9.0.net core 3.1 WebAPi 使用 AutoMapper 9.0
    public class UserDto
    {
        public int Id { get; set; }

        public string Name { get; set; }
    }
UserDto
.net core 3.1 WebAPi 使用 AutoMapper 9.0.net core 3.1 WebAPi 使用 AutoMapper 9.0
    public class UserViewModel
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public string Remark { get; set; }

        public int DepartmentId { get; set; }

        public string DepartmentName { get; set; }
    }
UserViewModel

 

Step 2:配置好对象之间的转换。

创建类 UserMapper 继承抽象类 Profile

.net core 3.1 WebAPi 使用 AutoMapper 9.0.net core 3.1 WebAPi 使用 AutoMapper 9.0
    public class UserMapper : Profile
    {
        public UserMapper()
        {
            CreateMap<UserDto, User>();
            CreateMap<User, UserViewModel>()
                .BeforeMap((u, v) => u.Remark = "Good")
                .ForMember(v => v.DepartmentId, u => u.MapFrom(user => user.DepartmentId))
                .ForMember(v => v.DepartmentName, u => u.MapFrom(user => user.DepartmentInfo.Name))
                .AfterMap((u, v) => u.Age++);
        }
    }
UserMapper

 

Profile 里面实现了两个接口

.net core 3.1 WebAPi 使用 AutoMapper 9.0

 

 

 

Step 3:注册服务

在 Startup.cs 的 ConfigureServices 方法中注册。

.net core 3.1 WebAPi 使用 AutoMapper 9.0.net core 3.1 WebAPi 使用 AutoMapper 9.0
        public void ConfigureServices(IServiceCollection services)
        {
            // ...
            services.AddAutoMapper(typeof(UserMapper));
            // ...
        }    
ConfigureServices

 

Step 4使用

创建 api 控制器

声明 private readonly IMapper _mapper;

.net core 3.1 WebAPi 使用 AutoMapper 9.0.net core 3.1 WebAPi 使用 AutoMapper 9.0
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IMapper _mapper;

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

        [ActionName("dto")]
        [HttpGet]
        public IActionResult DtoToEntity()
        {
            var userDto = new UserDto() { Id = 1, Name = "Bill" };
            var userEntity = _mapper.Map<UserDto, User>(userDto);

            JsonResult result = new JsonResult(userEntity);
            return result;
        }

        [ActionName("vo")]
        [HttpGet]
        public IActionResult EntityToViewModel()
        {
            var department = new Department() { Id = 101, Name = "Market" };
            var user = new User() { Id = 1, Name = "Bill", Age = 25, DepartmentId = 101, DepartmentInfo = department };
            var viewModel = _mapper.Map<User, UserViewModel>(user);

            JsonResult result = new JsonResult(viewModel);
            return result;
        }

    }
UserController

 

结果:

Dto -> Entity

.net core 3.1 WebAPi 使用 AutoMapper 9.0

 

 

Entity -> ViewModel

.net core 3.1 WebAPi 使用 AutoMapper 9.0

 

.net core 3.1 WebAPi 使用 AutoMapper 9.0

上一篇:Android SugarORM(3)


下一篇:Android音视频开发之-WebRTC技术实践