0

I have a web API to read below dat

I am using automapper to map the classes

This is my entity class

class Country
{
  public int id {get;set}
  public string CountryEnglishName {get;set;}
  public string CountryArabicName {get;set;}

}

and my DTO looks like below

class CountryDTO
{
  public int id {get;set}
  public string Country {get;set;}

}

If user pass API parameter as "English" then CountryDTO class field Country should contain with CountryEnglishName

If user pass API parameter as "Arabic" then Country should contain CountryArabicName

can we solve this with automapper ?

1

1 Answer 1

1

You can do this

CreateMap<Country, CountryDTO>().ForMember(x => x.Country, opt => opt.MapFrom(src => string.IsNullOrEmpty(src.CountryEnglishName) ? src.CountryArabicName : src.CountryEnglishName));

Here is an example where you can use a customer resolver that can hold the api parameter

using System;
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;

namespace ConsoleApp7
{
    class Country
    {
        public int id { get; set; }
        public string CountryEnglishName { get; set; }
        public string CountryArabicName { get; set; }

    }

    class CountryDTO
    {
        public int id { get; set; }
        public string Country { get; set; }

    }

    public interface IX
    {
        string ApiParameter { get; }
    }

    public class X : IX
    {
        // Here is where you get the parameter from the request if you need it from let's say the HttpContext, change this and you will see a differnt output
        public string ApiParameter => "English";
    }

    public class CustomResolver : IMemberValueResolver<object, object, string, string>
    {
        private readonly string _parameter;

        public CustomResolver(IX x)
        {
            _parameter = x.ApiParameter;
        }

        public string Resolve(object source, object destination, string sourceMember, string destinationMember, ResolutionContext context)
        {
            var c = (Country) source;
            return _parameter == "English" ? c.CountryEnglishName : c.CountryArabicName;
        }
    }

    public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            CreateMap<Country, CountryDTO>()
                .ForMember(x => x.Country, opt => opt.MapFrom<CustomResolver,string>(src=>src.CountryEnglishName));

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var services = new ServiceCollection();
            services.AddAutoMapper(typeof(MappingProfile));
            services.AddScoped<IX, X>();
            var mapper = services.BuildServiceProvider().GetRequiredService<IMapper>();
            var c = new Country()
            {
                CountryEnglishName = "A",
                CountryArabicName = "B"
            };
            var dto = mapper.Map<Country, CountryDTO>(c);
            Console.WriteLine(dto.Country);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.