首页 > 其他分享 >.NetCore【工作应用】AutoMapper

.NetCore【工作应用】AutoMapper

时间:2022-11-26 21:33:39浏览次数:60  
标签:NetCore Demo get public 应用 AutoMapper using AutoMapperWeb

AutoMapper

OOM(Object-Object-Mapping)组件

为了实现实体间的相互转换,从而避免我们每次采用手工的方式进行转换。

使用

  1. 安装nuget包
install-package AutoMapper
install-package AutoMapper.Extensions.Microsoft.DependencyInjection

AutoMapper.Extensions.Microsoft.DependencyInjection这个包方便通过依赖注入的方式去使用AutoMapper

  1. 定义model和viewmodel
namespace Demo.AutoMapperWeb.Model
{
    public class Student
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public string Gender { get; set; }
    }
}


namespace Demo.AutoMapperWeb.ViewModel
{
    public class StudentVM
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public string Gender { get; set; }
    }
}

  1. 定义class继承Profile

创建model和viewModel之间的map关系

namespace Demo.AutoMapperWeb.Mapper
{
    public class StudentProfile : Profile
    {
        /// <summary>
        /// 构造函数中实现映射
        /// </summary>
        public StudentProfile()
        {
            // Mapping
            // 第一次参数是源类型(这里是Model类型),第二个参数是目标类型(这里是DTO类型)
            CreateMap<Student, StudentVM>();
        }
    }
}

  1. ConfigureServices中添加AddAutoMapper配置
//自动找到所有继承了Profile的类然后进行配置
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
  1. 添加controller的AutoMapper注入,并使用AutoMapper实现自动映射
using AutoMapper;
using Demo.AutoMapperWeb.Model;
using Demo.AutoMapperWeb.ViewModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Demo.AutoMapperWeb.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class StudentsController : ControllerBase
    {
        private IMapper _mapper;
        public StudentsController(IMapper mapper)
        {
            this._mapper = mapper;
        }

        [HttpGet]
        public List<StudentVM> Get()
        {
            List<Student> list = new List<Student>();

            for (int i = 0; i < 3; i++)
            {
                Student student = new Student()
                {
                    ID = i,
                    Name = $"测试_{i}",
                    Age = 20,
                    Gender = "男"
                };
                list.Add(student);
            }

            var result = _mapper.Map<List<StudentVM>>(list);
            return result;
        }
    }
}

  1. 列名不相同时在profile中重新map

双向绑定设置

using AutoMapper;
using Demo.AutoMapperWeb.Model;
using Demo.AutoMapperWeb.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Demo.AutoMapperWeb.Mapper
{
    public class StudentProfile : Profile
    {
        /// <summary>
        /// 构造函数中实现映射
        /// </summary>
        public StudentProfile()
        {
            // Mapping
            // 第一次参数是源类型(这里是Model类型),第二个参数是目标类型(这里是DTO类型)
            CreateMap<Student, StudentVM>()
                .ForMember(des => des.StudentId, memberOption =>
                {
                    // 列名不相同时 匹配 studentID<=>id
                    memberOption.MapFrom(map => map.ID);
                })
                 .ForMember(des => des.StudentName, memberOption =>
                 {
                     // 列名不相同时 匹配 studentName<=>name
                     memberOption.MapFrom(map => map.Name);
                 })
                 //.ReverseMap()  //ReverseMap表示双向映射
                 ;
        }
    }
}


源码

标签:NetCore,Demo,get,public,应用,AutoMapper,using,AutoMapperWeb
From: https://www.cnblogs.com/thomerson/p/16928346.html

相关文章

  • jdwp+rinetd 进行java 容器应用的远程调试
    实际上就是一个玩法,实际上因为jdwp是基于tcp协议的,我们可以使用任何合适的tcplb工具解决(iptables也是可以的)只是rinetd使用起来还是比较简单的,同时包含了一些简单的......
  • Windows之应用安装程序 —— winget
    大家都用过Linux中的应用程序安装工具,如yum、apt、rpm等工具进行安装自己想要的一些工具或则软件之类的,当然Linux操作系统还是很强大的有很多类似的命令来安装我们所需要的......
  • Camera应用开发进阶
    前言之前曾经发布过一篇关于安卓Camera踩坑的文章,​​《分享几个关于Camera的坑》​​后面有读者私信说到希望系统学习下安卓Camera的的应用开发,能不能针对Camera出一些列......
  • Git应用开发详解之Git概述与环境准备
    Linux环境配置zsh快捷操作输入命令之后,按下tab​按键,可以在命令之间选择zshenv与zshrc的区别:zshenv​始终是source的,也就是说一直生效,但是zshrc​只是活跃......
  • 第1章-Spring的模块与应用场景
    目录一、Spring模块1.核心模块2.AOP模块3.消息模块4.数据访问模块5.Web模块6.测试模块二、集成功能1.目标原则2.支持组件三、应用场景1.典型完整的SpringWeb应......
  • 教育行业数据可视化应用方案与实践
    教育行业背景介绍随着信息技术发展,教育领域中的学习方式、教学模式、教学内容均已发生重大变革,以云计算、人工智能、物联网、大数据等技术的结合,“智慧教育”的需求也变的紧......
  • 教育行业数据可视化应用方案与实践
    教育行业背景介绍随着信息技术发展,教育领域中的学习方式、教学模式、教学内容均已发生重大变革,以云计算、人工智能、物联网、大数据等技术的结合,“智慧教育”的需求也变的紧......
  • IDEA官方 Docker 插件一键部署应用到远程服务器
    环境:jdk1.8及以上。Maven3.2+ideadockerdocker开启远程连接访问首先我们要开启docker的远程连接访问。保证不是docker所在的服务器,也能够远程访问docker。Linux版的docker:1......
  • Pytest接口测试框架实战项目搭建(五)—— Streamlit首次应用
    一、源码,c_page.py'''@Date:2022/11/1414:36@Author:一加一'''importstreamlitasstimportosimportsysBASE_DIR=os.path.dirname(os.path.dirname(os.pa......
  • .NET 7 看图桌面应用程序源代码下载
    .net7.0刚发布不久,就拿了练手了,制作了一个看图的桌面应用程序,可以看图片信息。请访问以下页面:https://hovertree.com/h/bjag/3osrx05l.htm这里有源代码可以下载.效果......