用于映射数据的自动映射器配置

编程入门 行业动态 更新时间:2024-10-27 05:32:50
本文介绍了用于映射数据的自动映射器配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下型号

来源:

public class Opportunity { public Guid Id { get; set; } public string Name { get; set; } public Guid QuotationId { get; set; } public int? QuotationNumber { get; set; } public int? QuotationVersionNumber { get; set; } }

目标:

public class OpportunityDto { public Guid Id { get; set; } public string Name { get; set; } public List<QuotationDto> Quotations { get; set; } } public class QuotationDto { public Guid Id { get; set; } public int Number { get; set; } public int VersionNumber { get; set; } }

我从数据库中获取的数据将与Opportunity模型保持一致,并且我的api公开了OpportunityDto模型.因此,在我的自动映射器配置中,我有以下代码:

The data I would fetch from my database would be flat as the Opportunity model and my api is exposing the OpportunityDto model. so, in my auto-mapper configuration, I have the following code:

services .AddSingleton(new MapperConfiguration(cfg => { cfg.CreateMap<OpportunityDto, Opportunity>().ReverseMap(); cfg.CreateMap<QuotationDto, Quotation>().ReverseMap(); }).CreateMapper())

我要实现的是唯一机会的列表,每个机会将有一个嵌套的成员,该成员将具有报价单.如何使自动映射器执行此分组?现在,从api返回的契机Dto的Quotations成员始终为空.

what I want to achive is a list of unique opportunities and each opportunity would have a nested member which would have the list of the quotations. how can I make the automapper perform this grouping? right now the Quotations member of the opportunityDto returned from the api is always empty.

推荐答案

AutoMapper配置

您可以执行以下操作:

AutoMapper Configuration

You can do something like the following:

public static void InitialiseMapper() { Mapper.Initialize(cfg => { cfg.CreateMap<IEnumerable<Opportunity>, OpportunityDto>() .ForMember(x => x.Id, x => x.MapFrom(y => y.FirstOrDefault().Id)) .ForMember(x => x.Name, x => x.MapFrom(y => y.FirstOrDefault().Name)) .ForMember(x => x.Quotations, x => x.MapFrom(y => Mapper.Map<IEnumerable<Opportunity>, IEnumerable<QuotationDto>>(y).ToArray())) ; cfg.CreateMap<Opportunity, QuotationDto>() .ForMember(x => x.Id, x => x.MapFrom(y => y.QuotationId)) .ForMember(x => x.Number, x => x.MapFrom(y => y.QuotationNumber)) .ForMember(x => x.VersionNumber, x => x.MapFrom(y => y.QuotationVersionNumber)) ; }); }

测试

然后成功地知道如何进行映射,如以下测试所示:

Test

Which then successfully knows how to map as demonstrated by the following test:

[TestMethod] public void TestMethod1() { var oppo1Guid = Guid.NewGuid(); var opportunities = new List<Opportunity> { new Opportunity { Id = oppo1Guid, Name = "Mikeys Oppurtunity", QuotationId = Guid.NewGuid(), QuotationNumber = 169, QuotationVersionNumber = 80, }, new Opportunity { Id = oppo1Guid, Name = "Mikeys Oppurtunity", QuotationId = Guid.NewGuid(), QuotationNumber = 170, QuotationVersionNumber = 20, } }; var dtos = Mapper.Map<IEnumerable<Opportunity>, OpportunityDto>(opportunities); var json = JsonConvert.SerializeObject(dtos, Formatting.Indented); Console.WriteLine(json); }

输出为

{ "Id": "623c17df-f748-47a2-bc7e-35eb124dbfa3", "Name": "Mikeys Oppurtunity", "Quotations": [ { "Id": "ad8b31c2-6157-4b7f-a1f2-9f8cfc1474b7", "Number": 169, "VersionNumber": 80 }, { "Id": "515aa560-6a5b-47da-a214-255d1815e153", "Number": 170, "VersionNumber": 20 } ] }

更多推荐

用于映射数据的自动映射器配置

本文发布于:2023-11-04 12:07:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557894.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   映射器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!