Posts Tagged ‘Unit Testing’

Maintainable MVC Series: Poor man’s RenderAction

Monday, March 8th, 2010

This article is part of the Maintainable MVC Series.

Soon – once that MVC 2 is finalized and released – we will have access to the new Html.Action command with which we can easily render partials for shared functionality, like navigation structure or other parts of the master page that need dynamic data. As Phil Haack shows in this post it’s a very powerful and urgently needed feature.

In the master view you will have a call like this:


<%= Html.Action("Menu") %>

This will directly call a method in one of your controllers to supply the partial view with its data:

[ChildActionOnly]
public ActionResult Menu() {

	NavigationData navigationData = navigationService.GetNavigationData();
	MenuViewModel menuViewModel = mapper.GetMenuViewModel(navigationData);

	return PartialView(menuViewModel);
}

Unfortunately in MVC 1 we have to make use of a more elaborate trick to get the data to the partial view.

(more…)

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…)