Dynamic Configuration Per Request in Mapster Mapper

70 Views Asked by At

I'm going to create multi language web api so I wanna to map some fields to dto depend on lang key value that stored in request header . for Example this is my domain model

public class Brand{
    public string Name_fa {get;set;}
    public string Name_en{get;set;}
}

if the language was en in header request, then Name_en must be map to Name property. and so on.

here is my code that works fine but it ran just for first mapping not for each request

config.Default
    .GetMemberName((model, side) =>
    {
        if (side == MemberSide.Destination)
        {
               if (model.GetCustomAttributes(true)
                   .OfType<MultilingualAttribute>()
                   .Any()
                  )
               {
                var httpContext = MapContext.Current.GetService<IHttpContextAccessor>();
                
                if (httpContext is null)
                    throw new Exception("HttpContext Not Injected");

                if(httpContext.HttpContext != null && httpContext.HttpContext.Request.Headers.
                       TryGetValue(Languages.LanguageHeaderKey, out var lang))
                    return model.Name + Languages.LanguageSeperation + lang;
               }
        }

        return model.Name;
    });

what should I do to apply this code for every request and change the source property map

I tried my solution but just for first mapping worked

0

There are 0 best solutions below