Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection in MVC 3 using unity framework

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

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 ,

    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.

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

like image 372
user3119881 Avatar asked Dec 29 '13 06:12

user3119881


People also ask

Can you use dependency injection in Unity?

Scene-based dependency injection means you can setup up the Unity hierarchy and your MonoBehaviors however you like. It scans your scene and automatically wires together your MonoBehaviors. It allows you to connect objects in the Unity hierarchy without having to manually use functions such as FindObjectOfType(...) .

What is Unity framework in MVC?

The Dependency Injection Design Pattern allows us to inject the dependency objects into a class that depends on them. Unity is a dependency injection container that can be used for creating and injecting the dependency object using either constructor, method, or property injections.

Which framework is used for dependency injection?

NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in . NET is a built-in part of the framework, along with configuration, logging, and the options pattern.


1 Answers

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

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:

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.

like image 95
Darin Dimitrov Avatar answered Oct 18 '22 04:10

Darin Dimitrov