在 Automapper 配置中使用 appsettings.json 值
Using appsettings.json values in Automapper configuration
在 Automapper 中映射两个对象时,有没有办法从 appsettings.json 获取值?
public class MapperProfile : Profile
{
public MapperProfile()
{
CreateMap<Employee, EmployeeDto>()
.ForMember(employeeDto => employeeDto.Picture, employee => employee.MapFrom(employee => $"{someValueFromAppSettings}{employee.Picture}"
}
}
我需要在映射的图片名称前附加路径,但我似乎无法弄清楚如何以及是否可以使用 Automapper。
来自 AutoMapper 文档:
You can’t inject dependencies into Profile classes.
但是(至少)有两种方法可以实现您想要的:
public class EmployeeDtoPathResolver: IValueResolver<Employee, EmployeeDto, string>
{
private readonly IConfiguration _configuration
public CarBrandResolver(IConfiguration configuration)
{
_configuration= configuration;
}
public string Resolve(Employee source, EmployeeDto destination, int destMember, ResolutionContext context)
{
var path = // get the required path from _configuration;
return $"{path}{source.Picture}"
}
}
public class MapperProfile : Profile
{
public MapperProfile()
{
CreateMap<Employee, EmployeeDto>()
.ForMember(employeeDto => employeeDto.Picture, opt => opt.MapFrom<EmployeeDtoPathResolver>()); // Edited this
}
}
它基本上是将 Before
和 After
Map Actions 封装成可重复使用的小型 类.
public class EmployeeDtoAction : IMappingAction<Employee, EmployeeDto>
{
private readonly IConfiguration _configuration;
public EmployeeAction (IConfiguration configuration)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}
public void Process(Employee source, EmployeeDto destination, ResolutionContext context)
{
var path = // get the required path from _configuration;
destination.Picture = $"{path}{destination.Picture}"
}
}
public class MapperProfile : Profile
{
public MapperProfile()
{
CreateMap<Employee, EmployeeDto>()
.AfterMap<EmployeeDtoAction>(); // Added this
}
}
在 Automapper 中映射两个对象时,有没有办法从 appsettings.json 获取值?
public class MapperProfile : Profile
{
public MapperProfile()
{
CreateMap<Employee, EmployeeDto>()
.ForMember(employeeDto => employeeDto.Picture, employee => employee.MapFrom(employee => $"{someValueFromAppSettings}{employee.Picture}"
}
}
我需要在映射的图片名称前附加路径,但我似乎无法弄清楚如何以及是否可以使用 Automapper。
来自 AutoMapper 文档:
You can’t inject dependencies into Profile classes.
但是(至少)有两种方法可以实现您想要的:
public class EmployeeDtoPathResolver: IValueResolver<Employee, EmployeeDto, string>
{
private readonly IConfiguration _configuration
public CarBrandResolver(IConfiguration configuration)
{
_configuration= configuration;
}
public string Resolve(Employee source, EmployeeDto destination, int destMember, ResolutionContext context)
{
var path = // get the required path from _configuration;
return $"{path}{source.Picture}"
}
}
public class MapperProfile : Profile
{
public MapperProfile()
{
CreateMap<Employee, EmployeeDto>()
.ForMember(employeeDto => employeeDto.Picture, opt => opt.MapFrom<EmployeeDtoPathResolver>()); // Edited this
}
}
它基本上是将 Before
和 After
Map Actions 封装成可重复使用的小型 类.
public class EmployeeDtoAction : IMappingAction<Employee, EmployeeDto>
{
private readonly IConfiguration _configuration;
public EmployeeAction (IConfiguration configuration)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}
public void Process(Employee source, EmployeeDto destination, ResolutionContext context)
{
var path = // get the required path from _configuration;
destination.Picture = $"{path}{destination.Picture}"
}
}
public class MapperProfile : Profile
{
public MapperProfile()
{
CreateMap<Employee, EmployeeDto>()
.AfterMap<EmployeeDtoAction>(); // Added this
}
}