The MVC framework – the Controller lifecycle II

A few days ago (ok, more than one month ago:),,) I’ve written a post about the Controller class where I’ve presented its main responsibilities. With the latest release of the platform, there has been some refactorings and now you’ve got another class on the hierarchy chain of your controllers.

IController –> ControllerBase –> Controller

I”d normally use a UML editor tool to build a simple class diagram, but in my current machin I don”t have any one installed, so I”ve limited myself to representing the hierarchy through text (btw, if you have any references for a free simple UML editor just let me know about it).

With the new model, the new ControllerBase class will  be responsible for storing the ViewData dictionary and the TempData dictionary. After initializing itself (ie, after creating the ControllerContext that will be used along its lifetime), it ends up calling the abstract ExecuteCore method. As you might expect, the Controller class ends up implementing that method and performing all the work described in the previous post.

Adding this new ControllerBase class means that now you can tests that need access to the ViewData and TempData dictionaries can be written agains this base class. This is specially useful where you receive instances of the ControllerContext class because now ControllerContext exposes a property of type ControllerBase (instead returning an instance of the IController interface).

The other option would be to pass those properties to the IController interface, but that would mean creating a new contract for IController and this means that if you’re building your own custom Controllers, you’d also have to support these two properties. So, at the end I believe that this was the correct approach and now if you’re reusing the default MVC controller functionalities and you need to access those two properties from your tests you no longer need to write something like:

((Controller)controller).ViewData …

Since we’re talking about the modifications on the controller, now is a good time to point out that it also has some new methods. You’ll probably be using the new PartialViewResult for generating partial views and you’ll use the UpdateModel object for updating your model from data submitted on your form. More on this methods on a future post. Keep tuned.


~ by Luis Abreu on September 8, 2008.

One Response to “The MVC framework – the Controller lifecycle II”

  1. >if you have any references for a free simple UML editor


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: