我正在使用 automapper 从 dto 映射到域,反之亦然; 我正在使用自定义类型转换器进行转换,但我想使用简单的注入(inject)器 ioc 将依赖项注入(inject)到我的转换器类中;我不能那样做。 请告诉我如何实现?
public class DtoToEntityConverter : ITypeConverter<Dto, Entity>
{
private readonly IEntityRepository _entityRepository;
public DtoToEntityConverter (IEntityRepository entityRepository )
{
_entityRepository = entityRepository ;
}
public Entity Convert(ResolutionContext context)
{
}
}
最佳答案
您需要通过 AutoMapper 配置服务:
var container = ConfigureSimpleInjectorContainer();
Mapper.Initialize(cfg => {
cfg.ConstructServicesUsing(type => container.GetInstance(type));
// The rest of your initialization
});
关于c# - Automapper 使用简单注入(inject)器 (Ioc) 将依赖项注入(inject)自定义类型转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31821253/