0

Dependency injection is often recommended in MVC projects because it's easy to implement with the pattern and adds a host of potential benefits. Indeed, it actually comes bundled with the default MVC project in Visual Studio.

If you read around MVC, however, you will also see a lot of people recommending the use of skinny controllers. In other words your controller does little more than act as a bridge between the display engine and the business logic. All the processing work is instead done by the models and helper classes.

But if you're using skinny controllers, is there any need for DI? Surely in this case, handling things like a repository or a file helper should be done outside the controller?

Or to put it another way, what I'm asking is that if you've got skinny controllers, shouldn't there be no need for those controllers to have external dependencies? No dependencies, no need to mock them for tests, so no need for DI. Indeed I've seen some arguments that you shouldn't unit test skinny controllers at all, because there's almost no logic in them.

Is there any value in adopting both approaches?

3 Answers 3

3

Dependency injection is often recommended in MVC projects because it's easy to implement with the pattern and adds a host of potential benefits

I would argue that dependency injection is recommended generally in projects (not just MVC) because of the benefits it brings in terms of decoupling and testability. There's nothing specific to an MVC pattern. As such I would adopt DI generally.

Indeed I've seen some arguments that you shouldn't unit test skinny controllers at all, because there's almost no logic in them.

I would disagree with this. I don't know what constitutes 'almost no logic' but you want to test your code to determine that it does what you want initially, and that it continues to do what you want in the face of subsequent modifications/additions (to ensure you're not introducing regressions)

1

Yes there is value because it allows you to unit test your controllers.

The business layer and model should know nothing about http so the job of the controller is translating the business layer to a web world.

Example:

  • Exception Y was thrown should result in http status 4xx
  • Precondition A was not met. Redirect to Abc

So even if the controller is skinny it has still work to do. Unit testing that work becomes possible by using DI.

2
  • Sorry, perhaps I didn't explain myself properly. I get this: what I'm asking is that if you've got skinny controllers, shouldn't there be no need for those controllers to have external dependencies? No dependencies, no need to mock them for tests, so no need for DI. Indeed I've seen some arguments that you shouldn't unit test skinny controllers at all, because there's almost no logic in them.
    – Bob Tway
    Apr 22, 2016 at 10:04
  • 1
    @MattThrower, If your controllers have no dependencies, then they have no functionality. In reality, even skinny controllers need to know how to call out to the business layer, ergo they have a dependency on that layer. DI allows you to abstract that dependency when testing the controllers. Just because they are skinny, doesn't mean they don't need tests.
    – David Arno
    Apr 22, 2016 at 11:37
1

Let's just assume for a minute that it is perfectly fine to skip DI with sknny controllers. Now what about the rest of your application?

Dependency Injection as a technique is proven to be immensely useful and valuable. As others have noted, it encourages a more decoupled design which improves testability. These are extremely valuable perks!

MVC or no MVC, Dependency Injection is a generally good idea.

So, even if doesn't apply to your skinny controllers it almost definitely applies to the rest of your application.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.