asp.net core webapi------3.AutoMapper的使用

1.nuget 安装程序集

安装AutoMapper和AutoMapper.Extension.Microsoft.DependencyInjection

2.配置映射关系

新增类文件,在类里面配置映射关系

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Models;
using SmartFactoryCommon;

namespace SmartFactoryBusinessInterface
{
    public class AutoMapperConfigs : Profile
    {
        public AutoMapperConfigs()
        {
            CreateMap<SystemLog,SystemLogDto>().ReverseMap();
            CreateMap<PagingData<SystemLog>,PagingData<SystemLogDto>>().ReverseMap();
        }
    }
}

3.ioc配置映射关系生效

在Program.cs中配置映射关系

cs 复制代码
builder.Services.AddAutoMapper(typeof(AutoMapperConfigs));

4.注入Mapper,map映射

本案例写在controller层,映射对象分别为SystemLog、SystemLogDto

cs 复制代码
using System.Data.SqlClient;
using AutoMapper;
using log4net;
using Microsoft.AspNetCore.Mvc;
using Models;
using SmartFactoryBusinessInterface;
using SmartFactoryCommon;
using SqlSugar;

namespace SmartFactoryApi.Controllers
{
    /// <summary>
    /// 框架默认的控制器
    /// </summary>

    [ApiController]
    [Route("[controller]")]
    public class LogController : ControllerBase
    {
        private readonly ILogger<LogController> _logger;
        private readonly ISqlSugarClient _IsqlSugarClient;
        private readonly IBaseServices _baseServices;
        //依赖注入AutoMapper
        private readonly IMapper _Imapper;
        //构造函数注入
        public LogController(ILogger<LogController> logger,ISqlSugarClient IsqlSugarClient,IBaseServices baseServices, IMapper imapper)
        {
            _logger = logger;
            _IsqlSugarClient = IsqlSugarClient;
            _baseServices = baseServices;
            _Imapper = imapper;
        }

        /// <summary>
        /// 日志分页查询
        /// </summary>
        /// <param name="pageindex">当前第几页</param>
        /// <param name="pagesize">每页几行数据</param>
        /// <returns></returns>
        [HttpGet("{pageindex:int}/{pagesize:int}")]
        public IActionResult logPage(int pageindex, int pagesize)
        {
            _logger.LogInformation($"logPage开始执行。。。。 时间:{DateTime.Now}");
            int totalCount = 0;
            // 业务逻辑
            //List<SystemLog> page = _sqlSugarClient.Queryable<SystemLog>().ToPageList(pageindex, pagesize,ref totalCount);

            PagingData<SystemLog> page = _baseServices.QueryPage<SystemLog>
                (c=>true,pagesize,pageindex,s=>s.Id,true);
            //_Imapper.Map方法做映射,将PagingData<SystemLog>转换为PagingData<SystemLogDto>>对象
            PagingData<SystemLogDto> pageDto = _Imapper.Map<PagingData<SystemLog>, PagingData<SystemLogDto>>(page);
            
            return new JsonResult(pageDto);
        }
    }
}
相关推荐
njsgcs3 小时前
solidworks自动标注折弯4 无向图 c#
开发语言·c#·solidworks
我是唐青枫4 小时前
C#.NET ThreadLocal 深入解析:线程独享数据、性能收益与实战边界
c#·.net
JQLvopkk5 小时前
C# 工业级上位机:交互实战
开发语言·c#·交互
kingwebo'sZone6 小时前
PdfiumViewer使用权限控制期操作按钮(PdfViewer其实也可以完整兼容)
c#
张小俊_7 小时前
WPF 跨线程 UI 更新与硬编码赋值引发的 Bug 排查
c#·bug·wpf
無斜7 小时前
【CAPL实用开发】--- CAPL调用 .NET DLL
开发语言·c#·capl·canoe
puamac8 小时前
UcTabWindow 布局多tab,加载编辑器和资源管理器等自定义控件
c#·编辑器·datagridview
唐青枫8 小时前
别再把增删改查写成一锅粥!C#.NET CQRS 从原理到实战
c#·.net
czhc114007566317 小时前
C# 428 线程、异步
开发语言·c#
唐青枫18 小时前
C#.NET ThreadLocal 深入解析:线程独享数据、性能收益与实战边界
c#·.net