thinking out loud: asp.net mvc & nhaml

I’ve been playing around with the view engine nhaml and asp.net mvc. At the same time, I’m playing around with some architectural ideas in hope to benefit future projects.

I’m not doing any fancy yet, but I thought that it might be interesting to show you a couple of things to see what you think. Bear in mind, this is my sandbox code. I also suspect that I’m reinventing some wheels.

The context here is a page that allows a user to register for an account on the site.

First, the view looks like this:

%h2 Create Account

%form{method="post"}
  = Html.FormField<OpenAccount>(x=>x.AccountName)
  = Html.FormField<OpenAccount>(x=>x.EmailAddress)
  = Html.FormField<OpenAccount>(x=>x.Password)
  
  %input {type="submit" value="Save"}
  %button {onclick="javascript:history.back()" type="button"} Cancel

OpenAccount is my view model. (I hate how we’ve overloaded these  terms.)

I really like the succinctness of the nhaml, though I’m not ready to commit to it 100%. Spark is very attractive to me as well.

Html.FormField is an extension method I wrote. I’m toying with the idea of having it emit a <lablel /> paired with the <input />. Part of my motivation here is standardizing all of the label/inputs in my forms as well as a few experimental ideas about localization.

It’s rather naive at the moment, but here is the helper:

public static string FormField<T>(this HtmlHelper helper, Expression<Func<T, string>> property)
    where T : class, ICommand
{
    var model = helper.ViewData.Model as T;
    if (helper.ViewData.Model != null && model == null) throw new InvalidOperationException();

    var expression = (MemberExpression)property.Body;
    var name = expression.Member.Name;

    var value = (model == null)
                    ? null
                    : property.Compile().Invoke(model);

    return helper.TextBox(name, value);
}

Yeah, I hardcoded to a textbox for the moment. As I said, this is just a sandbox spike.

Finally, the associated controller action looks like this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(OpenAccount form)
{
    var result = _backend.Execute(form);

    return result.Success
               ? (ActionResult) RedirectToAction("WhateverIsNext")
               : View(result);
}

I’m treating the incoming form as a command. The _backend is responsible for processing the command. The command is just information necessary to execute the command, not the logic to execute it. The result I’m getting back is actually an instance of the same command with information attached by the backend (e.g., did the command execute correctly, what were the validation errors). It’s not exactly SRP, however I’m influenced by the simplicity of RoR here.

One more interesting bit. I really don’t like having actions in my controller that do nothing other than render the view. For example:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Create()
{
    return View();
}

So instead, I created a base controller that does this:

public class ControllerBase : Controller
{
    protected override void HandleUnknownAction(string actionName)
    {
        try
        {
            View(actionName).ExecuteResult(ControllerContext);
        }
        catch (InvalidOperationException e)
        {
            //show the error or something
        }
    }
}

Posted 10-02-2009 4:48 PM by Christopher Bennage
Filed under: , ,

[Advertisement]

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)