简体   繁体   English

嵌套列表和 Automapper.Map

[英]Nested Lists and Automapper.Map

So I have 2 objects,AlbumDto and AlbumMediaDto.所以我有 2 个对象,AlbumDto 和 AlbumMediaDto。

public class AlbumDto
{
    public int AlbumId { get; set; }

    public string Title { get; set; }

    public DateTimeOffset? AlbumDate { get; set; }

    public AlbumMediasDto Media { get; set; }// list of media
}

And I have a method from the repo that would return a list of albums (where each has the properties mentioned above which includes a list of media)我有一个来自 repo 的方法,它会返回一个专辑列表(其中每个都有上面提到的属性,其中包括一个媒体列表)

var albumsForChild = await GetTenantRepository<IAlbumRepository>().GetAlbumsForChild(childId);

So I'm not sure how to map them to these Dtos using the AutoMapper ( I know that I can always use a nested foreach and fill everything accordingly but thought of learning how to do this the right way).所以我不确定如何使用 AutoMapper 将它们映射到这些 Dtos(我知道我总是可以使用嵌套的 foreach 并相应地填充所有内容,但想学习如何以正确的方式做到这一点)。

Any help appreciated!任何帮助表示赞赏!

Can you work with JSon deserialization?您可以使用 JSon 反序列化吗? If yes, below can be your solution.如果是,下面可以是您的解决方案。

Business logic:商业逻辑:

public IEnumerable<AlbumDto> GetAllAlbums()
{
    var allAlbums = _theApiWrapper.ExecuteGet<IEnumerable<AlbumDto>>("theApiRoute");
    return allAlbums;
}

Repository:存储库:

public T ExecuteGet<T>(string endpoint)
{
    var uri = $"https://apiurl.company.com/api/v1/{endpoint}";

    using (HttpClient client = new HttpClient())
    {
        var x = client.GetAsync(uri);
        var result = x.Result.Content.ReadAsStringAsync().Result;

        return JsonConvert.DeserializeObject<T>(
            result, 
            new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }
            );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM