Posts Tagged ‘Inversion of Control’

Maintainable MVC Series: Inversion of Control Container – StructureMap

Tuesday, February 9th, 2010

This article is part of the Maintainable MVC Series.

To make your code testable the pattern to use is that of Dependency Injection. DI is nothing more than injecting all dependencies of a class instance through the constructor (or setters if you wish, but less intuitive when reading the code, and doesn’t guarantee all dependencies are set). For example if you have a controller that uses a factory and a repository you can both inject them into the constructor as follows:

public class Controller
{
	private readonly IFactory factory;
	private readonly IRepository repository;

	public Controller(IFactory factory, IRepository repository)
	{
		this.factory = factory;
		this.repository = repository;
	}
}

(more…)