ASP.NET MVC 3 Dependency Injection

Version: 1.1.0

Description

In Object Oriented Programming paradigm, objects work together in a collaboration model where there are contributors and consumers. Naturally, this communication model generates dependencies between objects and components that could became difficult to manage when complexity increases . You have probably heard about Factory Pattern and the separation between the interface and the implementation using services. However, the client objects are often responsible for service location. Before introducing the Dependency Injection Pattern, we will explain what Inversion of Control (IoC) principle is. With Inversion of Control (Ioc), consumer objects do not create the other objects on which they rely. Those objects come from an external source.

Overview

Note:
This Hands-on Lab assumes you have basic knowledge of ASP.NET MVC and ASP.NET MVC 3 filters. If you have not used ASP.NET MVC 3 filters before, we recommend you to go over ASP.NET MVC Custom Action Filters and MVC Global and Dynamic Action filters Hand-on Lab.

In Object Oriented Programming paradigm, objects work together in a collaboration model where there are contributors and consumers. Naturally, this communication model generates dependencies between objects and components that could became difficult to manage when complexity increases .

Figure 1

Class dependencies and model complexity

You have probably heard about the Factory Pattern and the separation between the interface and the implementation using services. However, the client objects are often responsible for service location.

Before introducing the Dependency Injection Pattern, we will explain what Inversion of Control (IoC) principle is.

With Inversion of Control (Ioc), consumer objects do not create the other objects on which they rely. Those objects come from an external source.

The Dependency Injection (DI) Design Pattern

Dependency injection (DI) design pattern is based on separating component behavior from dependency resolution without object intervention.

This pattern is a particular implementation of Inversion of Control, where the consumer object receives his dependencies inside constructor properties or arguments.

DI requires a framework component behind to deal with class constructor.

Figure 2

Overview – Dependency Injection diagram

The advantages of using Dependency Injection pattern and Inversion of Control are the following:

  • Reduces class coupling
  • Increases code reusing
  • Improves code maintainability
  • Improves application testing
Note:
Depencency Injection is sometimes compared with Abstract Factory Design Pattern, but there is a slight difference between both approaches. DI has a Framework working behind to solve dependencies by calling the factories and the registered services.

Now that you understand the Dependency Injection Pattern, you will learn through this lab how to apply it on ASP.NET MVC 3. You will start using Dependency Injection on Controllers to include a service for database access. Next you will use Dependency Injection on Views to use a service inside a view and display information. Then, you will extend DI to MVC 3 Filters concept and inject a Custom Action Filter in the solution.

In this Hands-on Lab, you will learn how to:

  • Integrate MVC 3 with Unity Application Block for Dependency Injection
  • Use dependency injection inside an MVC Controller
  • Use dependency injection inside an MVC View
  • Use dependency injection inside an MVC Action Filter
Note:
This Lab proposes Unity Application Block as the dependency resolver framework, but it is posible to adapt any Dependency Injection Framework to work with MVC 3.

System Requirements

You must have the following items to complete this lab:

  • ASP.NET and ASP.NET MVC 3
  • Visual Studio 2010
  • SQL Server Database (Express edition or above)

    Note:
    You can install the previous system requirements by using the Web Platform Installer 3.0: https://go.microsoft.com/fwlink/?LinkID=194638.

Exercises

This Hands-On Lab is comprised by the following exercises:

  1. Exercise 1: Injecting a Controller
  2. Exercise 2: Injecting a View
  3. Exercise 3: Injecting Filters

Estimated time to complete this lab: 30 minutes.

Note:
Each exercise is accompanied by an End folder containing the resulting solution you should obtain after completing the exercises. You can use this solution as a guide if you need additional help working through the exercises.

Next Step

Exercise 1: Injecting a Controller