使用统一框架在MVC 3中进行依赖注入

时间:2021-09-06 15:47:11

I have implemented dependency injection in MVC 3 unity framework and following the instructions.

我已经在MVC 3统一框架中实现了依赖注入并遵循了指令。

It worked , but I have a few questions regarding this:

它有效,但我对此有几个问题:

Here is my implementation :

这是我的实施:

public interface ID
{

    string MReturn();
}

And the classes which implement this interface are:

实现此接口的类是:

public class D:ID
{
    public string MReturn()
    {
        return "Hi";
    }
}
public class E : ID
{
    public string MReturn()
    {
        return "HiE";
    }
}

public class F : ID
{
    public string MReturn()
    {
        return "Hif";
    }
}

In the bootstrapper class ,

在bootstrapper类中,

    private static IUnityContainer BuildUnityContainer()
    {

        var container = new UnityContainer();
        container.RegisterType<ID, D>();

        container.RegisterType<IController, HomeController>("feedbackRepo");
        container.RegisterType<ID, E>();
        container.RegisterType<ID, F>();
      // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();            

        return container;
    }

Now my question is

现在我的问题是

"I want to set the service class D in the Homecontroller constructor, but according to the above code, it is setting "class F" in the constructor.

“我想在Homecontroller构造函数中设置服务类D,但根据上面的代码,它在构造函数中设置”class F“。

Is there any way to do this?any modification to the above code?

有没有办法做到这一点?对上述代码的任何修改?

1 个解决方案

#1


5  

The reason why F is injected is because it is the last registered implementation of ID. It basically overwrites the previous registrations.

注入F的原因是因为它是ID的最后一次注册实现。它基本上覆盖了以前的注册。

If you have different implementations of some interface/base class and you want to inject specific implementations in different controllers you could register them as named instances:

如果你有一些接口/基类的不同实现,并且你想在不同的控制器中注入特定的实现,你可以将它们注册为命名实例:

container.RegisterType<ID, D>("d");
container.RegisterType<ID, E>("e");
container.RegisterType<ID, F>("f");

and then register the controller in the container and inject the desired named instance of ID:

然后在容器中注册控制器并注入所需的ID命名实例:

container.RegisterType<HomeController>(
    new PerRequestLifetimeManager(),
    new InjectionConstructor(new ResolvedParameter<ID>("d"))
);

Note that the controller is registered with PerRequestLifetimeManager to ensure that new instances are created for each HTTP request.

请注意,控制器已向PerRequestLifetimeManager注册,以确保为每个HTTP请求创建新实例。

#1


5  

The reason why F is injected is because it is the last registered implementation of ID. It basically overwrites the previous registrations.

注入F的原因是因为它是ID的最后一次注册实现。它基本上覆盖了以前的注册。

If you have different implementations of some interface/base class and you want to inject specific implementations in different controllers you could register them as named instances:

如果你有一些接口/基类的不同实现,并且你想在不同的控制器中注入特定的实现,你可以将它们注册为命名实例:

container.RegisterType<ID, D>("d");
container.RegisterType<ID, E>("e");
container.RegisterType<ID, F>("f");

and then register the controller in the container and inject the desired named instance of ID:

然后在容器中注册控制器并注入所需的ID命名实例:

container.RegisterType<HomeController>(
    new PerRequestLifetimeManager(),
    new InjectionConstructor(new ResolvedParameter<ID>("d"))
);

Note that the controller is registered with PerRequestLifetimeManager to ensure that new instances are created for each HTTP request.

请注意,控制器已向PerRequestLifetimeManager注册,以确保为每个HTTP请求创建新实例。