March 3, 2010

ASP.NET MVC : Injecting & Mocking an IRepository

If you are already familiar with ASP.NET MVC, you have probably seen the repository pattern in a few examples (such as NerdDinner), maybe you even use it in your own app.

One of the most frequent way to hot-plug a Repository class into a controller is to use the Strategy Pattern. But while an application evolves, you might want to centrally manage your wiring within a provider, that's what Dependency Injectors are for.
However lots of Dependency Injectors - or IoC containers at large - such as Unity or Castle Windsor contain a plethora of features and often rely heavily on XML configuration, making them overkill for smaller projects.
Ninject makes it relatively effortless to set up a DI. It just reached version 2.0.

In this post we will see how to:
  • Quickly inject a Repository in a controller with Ninject
  • Mock the IRepository interface in your tests with the Ninject.Moq plugin

Get the bits

Here's where to get these dependencies:
Ninject2 (with Ninject.Web.Mvc)
Ninject.Moq plugin (download and build)

Creating a Simple Repository

Let's start a quick MVC app with a Repository.
The Winter Olympics provide us with a simple Model composed of the following types:

Our Repository class looks like this:

Of course the first thing we'll do is to extract an IRepository interface from this class. We can at this point create a few Action Methods in an AthletesController to send some data to the view.

Injecting an IRepository into a Custom Controller

We'll now create a custom Controller by extending the MVC Controller class. Our extended type will contain our injected IRepository and all our controllers will inherit from it. Notice the [Inject] attribute:

We only need two things to set up Ninject and bind the Repository implementation as an IRepository in our controller instance:

-First, a Ninject Module class where the binding occurs:

-Second, we need to change our Global.asax so our MvcApplication inherits from NinjectHttpApplication instead. That's also where the override CreateKernel() references our binding module. Finally, we'll move the Application_Start content under the OnApplicationStarted override and call RegisterAllControllersIn() with the current assembly as parameter.

Ninject is now set up. At any time during development you can switch your IRepository implementation by changing the bindings of the NinjectModule.

Mocking an IRepository with Ninject

Because our Repository implementation uses some sort of database, we don't want our Unit Tests to depend on it. It is generally recommended to test persistence layers in separate integration tests.

For our Unit Tests, we set up a fake IRepository implementation with Moq.
The Ninject.Moq plugin integrates our mock in our controller by using a MockingKernel (as opposed to the StandardKernel previously seen in our Global.asax).

Let's create a test context in which we set up the mocked injection.
Notice how the kernel injects a mock in the controller and how we're then able to retrieve the mock to set it up.

[Thanks to Miguel Madero for helping me figure out this last part]

Our test class inherits from this context and our test methods mimic the behavior of the application, goal achieved.

[Requires Visual Studio 2010 RC / ASP.NET MVC2 RC2]

Wrapping Up

In this post we saw how to inject our Repository implementation as an IRepository member of the controller.

Finally we switched our implementation for a mocked Repository in our tests.

We clearly saw a case for injection as our application is now loosely coupled and our MVC is testable. We could switch from a database to an XML file as persistence layer without affecting the client or the tests.

kick it on
Shout it


  1. Excellent article. Thank you!

  2. Ninject.Moq has one huge disadvantage that you can see in your example.
    The repository in your controller has to be public, otherwise you would not be able to setup expectations on the repository.

  3. On the other hand moq-contrib project does not have this problem, but instead it keeps single mock for every mocked type which does not let you setup different expectations (probably rare case).