簡體   English   中英

如何使用 Automapper 將原型文件重復類型的 map 屬性列出 C# class 的屬性?

[英]How to map property of type repeated of proto file to list property of C# class using Automapper?

我有一個包含以下詳細信息的原型文件:

TestGRPC.proto:

syntax = "proto3";

option csharp_namespace = "MyGRPC";

package MyApi;

service Author {
  rpc GetArticles (ArticleRequest) returns (ArticleResponse);
}

message ArticleRequest {
  int32 articleId = 1;
  int32 countryId = 2;
  string userCookieId = 3;
}

message ArticleResponse {
   int32 ArticleId = 1; 
   string Title = 2;    
   repeated TaxTagsResponse RelatedTaxTags = 3; 
}

message TaxTagsResponse{
    int32 TaxTagId = 1;
    string DisplayName = 2;
}

這用於 asp.net 核心 3.1 gRPC 客戶端項目,該項目具有具有以下結構的 DTO 類:

public class ArticleDTO
{
    public int ArticleId
    {
        get;
        set;
    }

    public List<TaxTagsDTO> RelatedTaxTags
    {
        get;
        set;
    }
}

public class TaxTagsDTO
{
    public int? TaxTagId
    {
        get;
        set;
    }

    public int? ParentTagId
    {
        get;
        set;
    }

    public int? CountryId
    {
        get;
        set;
    }

    [JsonProperty("displayname")]
    [RegularExpression(Constants.GeneralStringRegularExpression)]
    public string DisplayName
    {
        get;
        set;
    }

    public int? LanguageId
    {
        get;
        set;
    }

    public List<CountryDTO> RelatedCountries
    {
        get;
        set;
    }
}

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<ArticleResponse, ArticleDTO>().ForMember(dest => dest.RelatedTaxTags, opt => opt.MapFrom(src => src.RelatedTaxTags));
    }
}

public class ArticleService : IArticleService
{
    private readonly IOptions<UrlsConfig> _appSettings;
    private readonly HttpClient _httpClient;
    private readonly ILogger<ArticleService> _logger;
    private readonly IMapper _mapper;
    public ArticleService(HttpClient httpClient, IOptions<UrlsConfig> appSettings, ILogger<ArticleService> logger, IMapper mapper)
    {
        _appSettings = appSettings;
        _httpClient = httpClient;
        _logger = logger;
        _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
    }

    public async Task<ArticleDTO> GetArticleAsync(int articleId, int countryId, string userCookieId)
    {
        return await GrpcCallerService.CallService(_appSettings.Value.GrpcAuthor, async channel =>
        {
            var client = new AuthorGRPC.Author.AuthorClient(channel);
            var response = await client.GetArticlesAsync(new ArticleRequest{ArticleId = articleId, CountryId = countryId, UserCookieId = userCookieId});
            _logger.LogDebug("grpc response {@response}", response);
            var articleResponse = _mapper.Map<ArticleDTO>(response);
            return articleResponse;
        });
    }
}

任何人都可以通過提供解決此問題的指導來幫助我。

我用以下代碼更新了 MappingProfile,它現在對我有用:)

public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            CreateMap<TaxTagsResponse, TaxTagsDTO>();
            CreateMap<ArticleResponse, ArticleDTO>();                
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM