0

I have an ASP.NET MVC Application I am working on. The core functionality of the Application all happens in the service layer, with the MVC Website just handling basic request routing and view/model preparation.

Because this application will need to be customizable for 1...N number of clients that all may have different code needs, I want to develop the core functionality with a heavy reliance on Interfaces. In my reading, I feel like Unity or another Dependency Injection / IOC framework would be a idea for this code. All the examples I have seen so far though involve using Unity to resolve everything including the Controllers. This seems like overkill to me since I do not see needing this functionality. Is there a way to use an IoC container without abstracting away the entire MVC framework.

1 Answer 1

3

Dependency Injection is a means to an end. That end is loose coupling. That is, we make the entire application loosely-coupled, and then in one place in the application, the composition root we couple the interfaces to their concrete types.

For MVC, there are 2 primary extension points that we can use for dependency injection - IControllerFactory and IDependencyResolver. Either way, the framework creates controllers at runtime and it is here where we need to resolve our dependent services.

If we don't inject services into our controllers, then the controllers, not the composition root will be responsible for composing the application. This means for every client, you would need a different set of controllers. It also means that it would be extremely difficult to test the logic within the controllers. It is far better to push the coupling to the composition root so there is only a single place in the application that will need to be different per client, and a single set of controller logic to test.

Sign up to request clarification or add additional context in comments.

3 Comments

Amen to that. If you need flexibility and extensibility, your best bet is to use Dependency Injection. Whether you need to use a DI container to achieve this however, is a totally different discussion.
Thanks for the answer. If I can clarify though, in my viewpoint, everything in the Controller / Model / ViewModel / View is just a skin and my application is entirely in the services. I am trying to design so that the services could be used that same from a Website, a mobile app, a desktop app, or any other type of client. So the only meaningful variation that happens within the MVC framework is when the model instatiates the service layer. Its at that point that I would like to have loose coupling.
When we use DI, we push dependencies down into the application from a single point (the composition root). The composition root is at the entry point of the application, which is typically in the UI layer. That means the DI container must know about some part of the UI layer in order to push the services down into it. However, there is nothing limiting you from using a different UI framework once you have achieved loose coupling of the services. As for reusing the composition root across applications, see blog.ploeh.dk/2015/01/06/composition-root-reuse

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.