zoukankan      html  css  js  c++  java
  • .net core中使用Automapper

    安装所需的包
    Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection

    配置AutoMapper
    public void ConfigureServices(IServiceCollection services)
    {
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
    }
    1
    2
    3
    4
    5
    添加测试模型
    public class QueueInfo
    {
    public string Id { get; set; }

    public string QueueNumber { get; set; }

    public DateTime CreateTime { get; set; }

    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class QueueInfoCreateDto
    {
    public string Id { get; set; }

    public string QueueNumber { get; set; }

    public DateTime CreateTime { get; set; }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    创建用户自定义Profile进行映射配置
    public class QueueProfile:Profile
    {
    public QueueProfile()
    {
    CreateMap<QueueInfo, QueueInfoCreateDto>().ReverseMap();
    }
    }
    1
    2
    3
    4
    5
    6
    7
    ReverseMap表示双向映射。具体还有很多相关的api,详情可以进行官网查看。

    进行测试
    public class ValuesController : ControllerBase
    {
    //注册IMapper
    private readonly IMapper _mapper;
    public ValuesController(IMapper mapper)
    {
    _mapper = mapper;
    }
    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
    QueueInfo info = new QueueInfo
    {
    Id = Guid.NewGuid().ToString(),
    CreateTime = DateTime.Now,
    QueueNumber = "123456789"
    };
    var dto = _mapper.Map<QueueInfoCreateDto>(info);
    return Ok(dto);
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    集合之间也可以进行映射。其他拓展请查看官网进行学习。

    Automapper还可以在命名上进行自动转换。

    例如
    public class QueueInfo
    {
    public string Id { get; set; }

    public string QueueNumber { get; set; }

    public DateTime CreateTime { get; set; }

    public QueueItem QueueItem { get; set; }
    }

    public class QueueItem
    {
    public string Id { get; set; }

    public string Name { get; set; }
    }

    public class QueueInfoCreateDto
    {
    public string Id { get; set; }

    public string QueueNumber { get; set; }

    public DateTime CreateTime { get; set; }

    /// <summary>
    /// 这里使用的是QueueInfo中的QueueItem对象下的Name。进行映射的时候会自动映射
    /// </summary>
    public string QueueItemName { get; set; }
    }
    ————————————————
    版权声明:本文为CSDN博主「Jonny Lin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/xhl_james/article/details/90511969

  • 相关阅读:
    C#——数组
    javaScript数组移除指定对象或下标i,数组去重
    css实现左边div自适应宽度,右边宽度适应左边
    最短JS判断是否为IE6(!-[1,]&&!window.XMLHttpRequest)(转)
    java 获取微信 页面授权 获取用户openid
    鼠标滑轮滚动事件
    浏览器后退(返回)事件捕获
    一些常用的原生js方法(函数)
    简单瀑布流实现
    XMLHttpRequest对象的创建与用法
  • 原文地址:https://www.cnblogs.com/webenh/p/13144403.html
Copyright © 2011-2022 走看看