Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm on a ASP NET MVC 5 web application inside a multi-tier solution.
The general scenario is the following: the controller Mvc uses a proxy to call methods from the services (Rest and Service2), the services call the methods from the business logic layer.

I have to use Unity as a dependency injection container to solve the dependencies in the controller, I'm not sure whether or not I understood it correctly.

What I assumed is the following: as the dependencies in the controller are the Service2Proxy and the RestServiceProxy, I registered these proxies as the components in the UnityConfig file.

As regards the implementation of the DI, I added property of type IProxy to the controller and I assigned the value of a specific proxy to it inside the constructor.
I wonder whether or not it is correct because I don't see the need of configuring Unity to do that, therefore I maybe have to look at it in a different way...?

What I have tried:

in the UnityConfig:

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

            container.RegisterType<IProxy, RESTProxy>();
            container.RegisterType<IProxy, Service2Proxy>();

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

In the controller:

private IProxy _proxy;

        public MovieMvcController(RESTProxy proxy)
            this._proxy= proxy;

And then I use _proxy to call the methods inside the actions
[no name] 30-Nov-20 12:43pm    
If you don't understand it, why use it? DI can be as simple as having a constructor that accepts a parameter; and you didn't even have to know you were doing it (DI).
xhon 30-Nov-20 13:14pm    
I know how to implement constructor and method injection (I used constructor injection in the code above, see the second block of code that I posted before), I apologize if my question wasn't clear. My Q was about the difference between using dependency injection and using dependency injection with Unity, I don't understand what would be the benefit of using Unity, configuring the classes in Unity to inject them, as it seems to me it can be even done without (As you said, DI can be as simple as having a constructor accepting parameters)
[no name] 1-Dec-20 11:45am    
Exactly. If you can't see the benefit, go with your gut after having thought about it (which you did). I'm not a fan of being a slave to any pattern "just because" if it only creates more work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900