【问题标题】:How do i map my model and dto using this particular mapper?我如何使用这个特定的映射器映射我的模型和 dto?
【发布时间】:2019-12-14 18:47:41
【问题描述】:

我正在尝试映射 api 调用的响应。我所做的是将API调用的响应(响应是一个数组)反序列化为模型,然后使用rootobject的数据返回一个新的dto。

public class RoadStatusService : IRoadStatusService
    {
        string baseURL = "blah";

        private readonly IMapToNew<Road, RoadDto> _mapper;

        public RoadStatusService()
        {

        }

        public RoadStatusService(IMapToNew<Road, RoadDto> mapper)
        {
            _mapper = mapper;
        }

        public RoadDto GetRoadStatusDetail()
        {
            var road = CallApi();


            return new RoadDto
            {
                DisplayName = road.Result.DisplayName,
                StatusSeverityDescription = road.Result.DisplayName,
                StatusSeverity = road.Result.DisplayName
            };
        }


        private async Task<Road> CallApi()
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(baseURL);

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage Res = await client.GetAsync(baseURL);

            if (Res.IsSuccessStatusCode)
            {
                var roadResponse = Res.Content.ReadAsStringAsync().Result;

                List<Road> road = JsonConvert.DeserializeObject<List<Road>>(roadResponse);


                foreach (var item in road)
                {
                    return new Road
                    {
                        DisplayName = item.DisplayName,
                        StatusSeverity = item.StatusSeverity,
                        StatusSeverityDescription = item.StatusSeverityDescription
                    };
                }

            }

            return null;
        }


    }

我的问题是,如何使用映射器类将我的模型映射到我的 dto 对象而不必这样做:

    public RoadDto GetRoadStatusDetail()
    {
        var road = CallApi();


        return new RoadDto
        {
            DisplayName = road.Result.DisplayName,
            StatusSeverityDescription = road.Result.DisplayName,
            StatusSeverity = road.Result.DisplayName
        };
    }

我已经编写了一个映射器类和一个接口来执行此操作,但我无法让它工作:

public class RoadToRoadDtoMapper : IMapToNew<Road, RoadDto>
{
    public RoadDto Map(Road model)
    {
        return new RoadDto
        {
            DisplayName = model?.DisplayName,
            StatusSeverity = model?.StatusSeverity,
            StatusSeverityDescription = model?.StatusSeverityDescription
        };
    }
}

和:

public interface IMapToNew<in TIn, out TOut>
{
    TOut Map(TIn model);
}

我认为我遇到的问题是 api 调用以数组响应?我想知道我是否应该以某种方式将我的对象转换为一个列表,然后调用.Select 并使用我编写的 mapper.map 函数。但我无法让它工作。

【问题讨论】:

    标签: c# api dotnet-httpclient dto mapper


    【解决方案1】:

    如果我理解您要做什么,您还需要一个列表映射器,然后为该类中的对象调用映射器:

    public class RoadListToRoadListDtoMapper : IMapToNew<List<Road>, List<RoadDto>>
    {
        private RoadToRoadDtoMapper roadToRoadDtoMapper = new RoadToRoadDtoMapper();
        public List<RoadDto> Map(List<Road> models)
        {
            var roadDtos = new List<RoadDto>();
            foreach(var road in models){
                roadDtos.Add(roadToRoadDtoMapper.Map(road));
            }
            return roadDtos;  
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 2021-07-12
      相关资源
      最近更新 更多