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 .
Class dependencies and model complexity
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.
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.
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
|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 MVC3 . 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
|This Lab proposes Unity Application Block as the dependency resolver framework, but it is posible to adapt any Dependency Injection Framework to work with MVC3.|
You must have the following items to complete this lab:
- ASP.NET and ASP.NET MVC 3 Beta 1
- Visual Studio 2010
- SQL Server Database (Express edition or above)
This Hands-On Lab is comprised by the following exercises:
- Exercise 1: Injecting a Controller
- Exercise 2: Injecting a View
- Exercise 3: Injecting Filters
Estimated time to complete this lab: 30 minutes.
|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.|