简单的注入器注册自动化器

时间:2015-04-19 22:23:28

标签: c# asp.net-mvc dependency-injection automapper simple-injector

我想用Simple Injector注册Automapper,将它注入控制器:

public class MyController : BaseController
{
    private IUnitOfWork unitOfWork;
    private IMappingEngine mappingEngine;

    public PatientController(IUnitOfWork _unitOfWork, IMappingEngine _mappingEngine)
    {
        this.unitOfWork = _unitOfWork;
        this.mappingEngine = _mappingEngine;
    }
}

但是当我尝试注册时

public static void Initialize()
{
    //Code for registering our repository class and DI
    var container = new Container();
    container.Register<IUnitOfWork, UnitOfWork>();

    container.Register<IMappingEngine, MappingEngine>();

    container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
    DependencyResolver.SetResolver(
        new SimpleInjectorDependencyResolver(container));
}

我收到了一个错误:

  

要使容器能够创建MappingEngine,它应该只包含一个公共构造函数,但它有2个。

     

参数名称:TImplementation。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

Simple Injector不能自动为您自动连接MappingEngine类型,它不应该,因为这是一个框架类型,框架类型不应该自动连接,因为你可以阅读here

相反,请使用手动接线,如下所示:

var container = new Container();

container.RegisterSingle<IMappingEngine>(Mapper.Engine);