Chapter 13. Controller factories

 

This chapter covers

  • Building custom controller factories
  • Dependency injection with controllers
  • Working with StructureMap
  • Working with Ninject
  • Working with Castle Windsor

One common technique when building applications is to pass application dependencies into the constructor of the controllers. By leveraging various tools, we can automatically wire up these dependencies and provide the arguments without having to write mundane, repetitive code.

To enable this for our controllers, we need to take responsibility for creating them. In this chapter, you’ll learn about controller factories and how you can use them to help enable such scenarios.

13.1. What are controller factories?

Controller factories are an important extension point in the ASP.NET MVC Framework. They allow you to take on the responsibility of creating controllers, which enables you to apply logic for every single controller in your application. You can use controller factories to apply a custom IActionInvoker instance to all your controllers, or perhaps to add custom logging. The most common case of a controller is to enable support for dependency injection tools.

13.2. Creating a custom controller factory

13.3. Enabling dependency injection in your controllers

13.4. Creating a StructureMap controller factory

13.5. Creating a Ninject controller factory

13.6. Creating a Castle Windsor controller factory

13.7. Summary