您的位置:首页 > 编程语言 > ASP

Asp.NetCore 3.1 使用AutoMapper自动映射转换实体 DTO,Data2ViewModel

spring_poll 2020-08-27 20:11 477 查看 https://www.cnblogs.com/Fengge

1:什么是AutoMapper?

下面为AutoMapper官方的解释:

AutoMapper是一个对象-对象映射器。对象-对象映射通过将一种类型的输入对象转换为另一种类型的输出对象来工作。

使AutoMapper变得有趣的是,它提供了一些有趣的约定,以免去搞清楚如何将类型A映射为类型B。只要类型B遵循AutoMapper既定的约定,就需要几乎零配置来映射两个类型。

附上官网地址:https://docs.automapper.org/en/stable/Getting-started.html

2:使用AutoMapper有啥好处?

其实,这个比较好回答,通常在我们使用面向对象编程中,经常会遇到,ORM从数据库表中获取到比较多的字段,

这个时候我们只需要在页面展示比较少的字段,也可以起到节流等的作用,如果一两个实体的转换还好,成白上千N多地方都需要这样的操作,

还是手动一个一个的赋值实就大大降低了开发效率,这个时候AutoMapper就派上了用场!

3:怎么使用AutoMapper?

1:引入NuGet包 AutoMapper.Extensions.Microsoft.DependencyInjection 8.0.1最新版本即可,其他相关的依赖包会自动导入新增进来

using System;

namespace WebApp.AutoMapperTestModel
{
// Entity类
public class SendMsg
{
public int mid { get; set; }
public string mTitle { get; set; }
public string mContent { get; set; }
public DateTime editTime { get; set; }
}
}
View Code
using System;

namespace WebApp.AutoMapperTestModel
{
public class SendMsgViewModel
{
public int mid { get; set; }
public string mTitlw { get; set; }
public string mContent { get; set; }
public DateTime UpdateTime { get; set; }
}
}
View Code 3:注入AutoMapper的服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApp.Controllers
{
using WebApp.AutoMapperTestModel;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[Controller]")]
public class DoAutoMapperController
{
private IMapper _mapper;
private List<SendMsg> sendMsgList { get; set; }
private SendMsg sendMsgOne { get; set; }

public DoAutoMapperController(IMapper mapper)
{
_mapper = mapper;
sendMsgList = GetList();
sendMsgOne = GetSendMsg();

}
private List<SendMsg> GetList()
{
return new List<SendMsg> {
new SendMsg{ mid=110,editTime=DateTime.Parse("1998-01-01"),mContent="你好我好大家好1",mTitle="T1"},
new SendMsg{ mid=111,editTime=DateTime.Parse("1998-02-01"),mContent="你好我好大家好2",mTitle="T2"},

ad0
new SendMsg{ mid=112,editTime=DateTime.Parse("1998-03-01"),mContent="你好我好大家好3",mTitle="T3"},
new SendMsg{ mid=113,editTime=DateTime.Parse("1998-04-01"),mContent="你好我好大家好4",mTitle="T4"},
new SendMsg{ mid=114,editTime=DateTime.Parse("1998-05-01"),mContent="你好我好大家好5",mTitle="T5"}
};
}
private SendMsg GetSendMsg()
{
return new SendMsg { mid = 110, editTime = DateTime.Parse("1998-01-01"), mContent = "你好我好大家好1", mTitle = "T1" };
}

[HttpGet("Automapper")]
public ApiResultObject AutomapperOne()
{
ApiResultObject resultObject = new ApiResultObject();
try
{
var sendmsg = _mapper.Map<SendMsg, SendMsgViewModel>(sendMsgOne);
if (sendmsg != null)
{
resultObject.Data = sendmsg;
resultObject.Code = ResultCode.Success;
resultObject.Msg = "AutoMapper Success";
}
else
{
resultObject.Data = Enumerable.Empty<SendMsgViewModel>();
}
}
catch (Exception ex)
{
resultObject.Msg = $"发生异常:{ ex.Message}";
}
return resultObject;

}
[HttpGet("AutomapperList")]
public ApiResultObject AutomapperList()
{
ApiResultObject resultObject = new ApiResultObject();
try
{
//                                                                      List IEnumerable
IEnumerable<SendMsgViewModel> sendMsgViewModels = _mapper.Map<List<SendMsg>, IEnumerable<SendMsgViewModel>>(sendMsgList);
if (sendMsgViewModels != null && sendMsgViewModels.Any())
{
resultObject.Data = sendMsgViewModels;
resultObject.Code = ResultCode.Success;
resultObject.Msg = "AutoMapper Success";
}
else
{
resultObject.Data = Enumerable.Empty<SendMsgViewModel>();
}
}
catch (Exception ex)
{
resultObject.Msg = $"发生异常:{ ex.Message}";
}
return resultObject;
}
}
}
View Code 6:看测试结果:

两个表的定义的字存在不一致,映射的时候也没有指明关系,会导致相关字段没有值

映射并指明两者之间的属性关系之后:

 最后来一个集合的映射测试,测试结果是ok的

7:最后还不过瘾,再来一波小菜:下面只是起到抛砖引玉的作用, 学友们可以根据自己的实际情况来使用扩展方法,直接点出,更是美美哒!!!

 

标签: 
相关文章推荐