如何使用此特定映射器映射模型和dto?

问题描述 投票:0回答:1

我正在尝试映射api调用的响应。我所做的是将API调用的响应(响应是一个数组)反序列化为模型,然后使用根对象的数据返回新的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
        };
    }

我已经编写了一个mapperclass和一个接口来执行此操作,但是我无法使其正常工作:

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个回答
0
投票

如果我了解您要尝试做的事情,那么您也需要一个列表映射器,然后为该类中的对象调用该映射器:

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;  
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.