c# - Automapper 使用简单注入(inject)器 (Ioc) 将依赖项注入(inject)自定义类型转换器

标签 c# automapper ioc-container simple-injector typeconverter

我正在使用 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/

相关文章:

c# - 使用 AutoMapper 对集合进行多态映射

php - 如何通过 Laravel IoC 容器访问 Orchestra Xml Parser

c# - 覆盖 StructureMap 3 中的自定义注册约定

c# - 在 .NET RESTful 应用程序中使用 Ninject?

c# - 在类库中使用 Automapper 进行 Ninject

c# - File.ReadAllLines() 和 File.ReadAllText() 有什么区别?

c# - 想深入了解Http

c# - 从数组 C# 中删除字符串

javascript - ASP.NET mvc 隐藏 webgrid 列

c# - AutoMapper 最佳实践——我应该向 DAO 询问信息以完成从 DTO 到域对象的映射吗?