views:

520

answers:

3

Hi, I know basic idea of thunderdome principle (one object enters, one object leaves) but I didn't see any real world example of it in asp.net mvc. Is it good example of thunderdome principle

  public ActionResult Index(Employee employee)
        {
             //some actions here
             return View(employeeViewModel);
        }

But what about statement

The Controller classes will NEVER be directly exposed to anything related to HttpContext

How the action invoker should looks like ? Could You provide some examples and unit tests for it ?

A: 

This is the most clean approach "thunderdome principle (one object enters, one object leaves)" for MVC applications. You should always try to do trhings in this style, and avoid using ViewData or ViewTemp in order to get your necessary data in the view.

For a simple example you can look in the jscportal project here link text

for example in the jscportal\JSC.Portal.Web\Controllers\TemplatesController.cs you'll have their examples like you want:

public ActionResult List()
{
    IList<Template> templates = Service.GetAll();
    return View(templates);
}

public ActionResult Edit(int id)
{
    Template t = Service.GetById(id, false);
    return View(t);
}

good luck!

diadiora
Yes but what about hiding httpcontext ?? and about testing such controller ?
Tadeusz Wójcik
if you let it like this, than you'll have to resume your testing to what is going on within the service, assume you'll test in this case only the posibilities that are coming from repositories. If you want to go into HttpContext than you can use ideas from haacked: see http://haacked.com/archive/2007/12/09/writing-unit-tests-for-controller-actions.aspx
diadiora
A: 

from http://codebetter.com/blogs/jeremy.miller/archive/2008/10/23/our-opinions-on-the-asp-net-mvc-introducing-the-thunderdome-principle.aspx

The “Thunderdome Principle” – All Controller methods take in one ViewModel object (or zero objects in some cases) and return a single ViewModel object (one object enters, one object leaves). The Controller classes will NEVER be directly exposed to anything related to HttpContext. Nothing makes me cry like seeing people trying to write tests that mock or stub that new IHttpContextWrapper interface. Likewise, Controller methods do not return ViewResult objects and are generally decoupled from all MVC infrastructure. We adopted this strategy very early on as a way to make Controller testing simpler mechanically.

But i want to know how to do this ? how to write such controller action invoker ? becouse normally we have to mock httpcontext

Tadeusz Wójcik
+2  A: 

There is an example of how to achieve a OMIOMO (Thunderdome) Action invoker in ASP.NET MVC in the Oxite rev2 source.

Specifically the OxiteActionInvoker: http://oxite.codeplex.com/SourceControl/changeset/view/31497#442766

And here you can see a controller that's OMIOMO: http://oxite.codeplex.com/SourceControl/changeset/view/31497#442745

Also of interest, the Oxite guys were able to make it so that you could you have IoC-able action filters (vs. having to specify all your filters on the actions -- a possible OCP violation since the action would then have to know all the possible ways in which it would be used). You can see this in action in the OxiteActionInvoker method "GetFilters" where it hits the FilterRegistry to load all the registered filters for that action.

chadmyers