AutoMapper에서 맵 기능에 대한 모의 외설을 설정하는 가장 좋은 방법은 무엇입니까?모의 AutoMapper 매퍼. Moq을 사용하여 맵 호출
IMapper 인터페이스를 추출하므로 해당 인터페이스에 대한 설정을 기대할 수 있습니다. 내 매퍼는 의존성이 있으므로 매퍼에 전달해야합니다.
두 개의 서로 다른 종속성 구현을 사용하여 내퍼 클래스의 인스턴스를 2 개 만들면 어떻게됩니까? AutoMapper 맵은 정적이므로 두 매퍼 모두 동일한 종속성 인스턴스를 사용합니다. 또는 AutoMapper는 동일한 객체로 2 개의 다른 맵을 설정하려고하기 때문에 예외가 발생할 수도 있습니다.
이 문제를 해결하는 가장 좋은 방법은 무엇입니까?
public interface IMapper {
TTarget Map<TSource, TTarget>(TSource source);
void ValidateMappingConfiguration();
}
public class MyMapper : IMapper {
private readonly IMyService service;
public MyMapper(IMyService service) {
this.service = service
Mapper.CreateMap<MyModelClass, MyDTO>()
.ForMember(d => d.RelatedData, o => o.MapFrom(s =>
service.getData(s.id).RelatedData))
}
public void ValidateMappingConfiguration() {
Mapper.AssertConfigurationIsValid();
}
public TTarget Map<TSource, TTarget>(TSource source) {
return Mapper.Map<TSource, TTarget>(source);
}
}