简体   繁体   English

使用Unity.Mvc5注入依赖项时的多个控制器构造函数

[英]Multiple controller constructors when injecting dependencies using Unity.Mvc5

I'm very new to dependency injection and I've just set up Unity.Mvc5 and has some success with it. 我对依赖注入非常陌生,我刚刚设置了Unity.Mvc5,并且取得了一些成功。 But now I face the problem of multiple contructors in my controller class. 但是现在我遇到了控制器类中多个构造函数的问题。 I already have constructors that handle my UserManager and following tutorials I understand I need another constructor to instantiate my interface. 我已经有了处理我的UserManager构造函数和以下教程我理解我需要另一个构造函数来实例化我的接口。 When I do this however, I get the following error: 但是当我这样做时,我收到以下错误:

The type OrganisationController has multiple constructors of length 1. Unable to disambiguate. OrganisationController类型有多个长度为1的构造函数。无法消除歧义。

A snippet from my controller: 来自我的控制器的片段:

    private IPush _pushMessage;

    // Here is the problem!
    public OrganisationController(IPush pushMessage)
    {
        _pushMessage = pushMessage;
    }

    public OrganisationController()
        : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
    {
    }

    public OrganisationController(UserManager<ApplicationUser> userManager)
    {
        UserManager = userManager;
        var provider = new DpapiDataProtectionProvider("MyApp");
        UserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(provider.Create("PasswordReset"));
    }

    public UserManager<ApplicationUser> UserManager { get; private set; }

And my UnityConfig.cs as follows: 我的UnityConfig.cs如下:

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();

        container.RegisterType<IPush, PushMessage>();
        container.RegisterType<IController, OrganisationController>();
        container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
        container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }
}

I am not sure how to tell Unity that I have another constructor being used to implement an interface. 我不知道如何告诉Unity我有另一个构造函数用于实现接口。

When using DI, constructors are specifically for DI. 使用DI时,构造函数专门用于 DI。 There is no reason to create alternate constructors (especially on controllers, since the only caller is the ControllerFactory which delegates the call to the DI container). 没有理由创建备用构造函数(特别是在控制器上,因为唯一的调用者是ControllerFactory ,它将调用委托给DI容器)。

In fact, using multiple constructors with dependency injection is anti-pattern and should be avoided. 事实上, 使用具有依赖注入的多个构造函数是反模式的 ,应该避免。

Related: Rebuttal: Constructor over-injection anti-pattern 相关: Rebuttal:构造函数过度注入反模式

While I agree with other answer by @NightOwl888. 虽然我同意@ NightOwl888的其他答案。 In some cases you may want to have multiple constructors. 在某些情况下,您可能希望拥有多个构造函数。

Have you tried InjectionConstructor attribute? 您是否尝试过InjectionConstructor属性?

[InjectionConstructor]
public OrganisationController(IPush pushMessage)
{
    _pushMessage = pushMessage;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM