C# 基于动态条件的自动映射
我有一个web API来阅读下面的dat 我正在使用automapper映射类 这是我的实体类C# 基于动态条件的自动映射,c#,asp.net-core,entity-framework-core,automapper,C#,Asp.net Core,Entity Framework Core,Automapper,我有一个web API来阅读下面的dat 我正在使用automapper映射类 这是我的实体类 class Country { public int id {get;set} public string CountryEnglishName {get;set;} public string CountryArabicName {get;set;} } 我的DTO如下所示 class CountryDTO { public int id {get;set} public s
class Country
{
public int id {get;set}
public string CountryEnglishName {get;set;}
public string CountryArabicName {get;set;}
}
我的DTO如下所示
class CountryDTO
{
public int id {get;set}
public string Country {get;set;}
}
如果用户将API参数传递为“English”,则CountryDTO
类字段Country应包含带CountryEnglishName
如果用户将API参数作为“阿拉伯语”传递,则国家应包含国家阿拉伯名称
我们可以用automapper解决这个问题吗?你可以这样做
CreateMap<Country, CountryDTO>().ForMember(x => x.Country, opt => opt.MapFrom(src => string.IsNullOrEmpty(src.CountryEnglishName) ? src.CountryArabicName : src.CountryEnglishName));
CreateMap().formMember(x=>x.Country,opt=>opt.MapFrom(src=>string.IsNullOrEmpty(src.CountryEnglishName)?src.CountryArabicName:src.CountryEnglishName));
下面是一个示例,您可以使用可以保存api参数的客户解析程序
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);
}
}
}
使用系统;
使用自动制版机;
使用Microsoft.Extensions.DependencyInjection;
名称空间控制台App7
{
阶级国家
{
公共int id{get;set;}
公共字符串CountryEnglishName{get;set;}
公共字符串CountryArabicName{get;set;}
}
CountryDTO类
{
公共int id{get;set;}
公共字符串国家{get;set;}
}
公共接口九
{
字符串参数{get;}
}
公共类别X:IX
{
//这里是从请求中获取参数的地方,如果您需要它,比如说从HttpContext,更改它,您将看到不同的输出
公共字符串参数=>English;
}
公共类CustomResolver:IMemberValueResolver
{
私有只读字符串_参数;
公共自定义解析器(IX x)
{
_参数=x.api参数;
}
公共字符串解析(对象源、对象目标、字符串源成员、字符串目标成员、解析上下文上下文)
{
var c=(国家)来源;
返回_参数==“English”?c.countryenglish名称:c.CountryArabicName;
}
}
公共类映射配置文件:配置文件
{
公共映射配置文件()
{
CreateMap()
.ForMember(x=>x.Country,opt=>opt.MapFrom(src=>src.CountryEnglishName));
}
}
班级计划
{
静态void Main(字符串[]参数)
{
var services=newservicecolection();
AddAutoMapper(类型(映射配置文件));
services.addScope();
var mapper=services.BuildServiceProvider().GetRequiredService();
var c=新国家/地区()
{
CountryEnglishName=“A”,
CountryArabicName=“B”
};
var dto=mapper.Map(c);
控制台写入线(dto.国家/地区);
}
}
}