Я пытаюсь создать пользовательский IValueResolver
, который имеет вложенные отображения, используя стиль IoC AutoMapper, но не может внедрить экземпляр IMapper
в распознаватель.
Ниже приведена исходная версияраспознаватель с конструктором, все еще включающим параметр DI IMapper.
Есть ли какой-нибудь способ, которым я могу использовать вложенные вызовы Mapper.Map()
внутри распознавателя, или есть другой способ достижения того же результата?
Не уверен, сколько информации здесь полезно, но у меня есть несколько профилей, которые добавляются в Startup.ConfigureServices()
с использованием services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
Спасибо!
public class PlannedTrainingExerciseDtosResolver : IValueResolver<TrainingPlan, TrainingPlanDto, ICollection<PlannedTrainingExerciseDto>>
{
private Mapper Mapper { get; }
public PlannedTrainingExerciseDtosResolver(IMapper mapper)
{
Mapper = mapper;
}
public ICollection<PlannedTrainingExerciseDto> Resolve(TrainingPlan source, TrainingPlanDto destination, ICollection<PlannedTrainingExerciseDto> destMember, ResolutionContext context)
{
var plannedTrainingExercises = new List<PlannedTrainingExerciseDto>();
foreach (var trainingPlanObjective in source.TrainingPlanObjectives)
{
if (trainingPlanObjective.PlannedTrainingExercises != null)
{
foreach (var exercise in trainingPlanObjective.PlannedTrainingExercises)
{
var exerciseDto = Mapper.Map<PlannedTrainingExerciseDto>(exercise);
exerciseDto.Objective = Mapper.Map<ObjectiveDto>(trainingPlanObjective.Objective);
plannedTrainingExercises.Add(exerciseDto);
}
}
}
return plannedTrainingExercises;
}
}