MonoRail SmartDispatcherController Rocks - ASP.NET Rapid Application Development Goodness
by David Hayden ( Florida .NET C# SQL Server Developer ), Filed: Castle Project, MonoRail
Daily I like to learn new features in MonoRail and each day brings more and more ASP.NET Rapid Application Development Goodness that I would love to see in the Web Client Software Factory and ASP.NET in general. But, I have a feeling that may be the wrong attitude. Maybe I should just be using MonoRail. As I learn more and more about it, I just may do that :)
SmartDispatcherController
MonoRail has this wonderful SmartDispatcherController that is just that - SMART! It removes the tedious burden of grabbing querystring and form field values and automatically plugs them into your actions as paramter values. Wow!
Let's take a simple form created using NVelocity that asks for the name and email of the person wishing to subscribe:
<form action="Create.rails">
Name: <input type="text" name="name" /> <br>
Email: <input type="text" name="email" /> <br>
<br>
<input type="submit" value="Create" />
</form>
When the submit button is pressed it calls the Create Action in the SubscriberController:
public void Create(string name, string email)
{
// Do Work
Flash["name"] = name;
}
The parameters to the Create Method, name and email, are automatically populated with the values entered into the name and email text fields in the form. SMART! Saves me the trouble of manually reading and converting the values and populating the variables myself.
But, wait.... it gets better with the Databind Attribute.
Databind Attribute in MonoRail
Let's say the name and email of the subscriber will be entered into a Subscriber Class:
public class Subscriber
{
private string name, email;
public string Name
{
get { return name; }
set { name = value; }
}
public string Email
{
get { return email; }
set { email = value; }
}
}
Let's ramp the productivity up a notch and have MonoRail enter the values directly into an instance of subscriber for us. We change the form a bit and prefix the control names with the “subscriber“ prefix:
<form action="Create.rails">
Name: <input type="text"
name="subscriber.name" /> <br>
Email: <input type="text"
name="subscriber.email" /> <br>
<br>
<input type="submit" value="Create" />
</form>
and change the Create Method on the SubscriberController to work with an instance of Subscriber instead of individual variables:
public void Create([DataBind("subscriber")]
Subscriber subscriber)
{
// Do Work
Flash["name"] = subscriber.Name;
}
Yep, you guessed it. Now an instance of subscriber is created with the Name and Email Properties automatically populated with the values entered in the name and email text fields in the form. Freaking Cool!
Conclusion
The MonoRail SmartDispatcherController Class and DataBind Attribute Rock at helping you automatically populate action parameters in your web applications. You cannot beat that kind of ASP.NET Rapid Application Development Goodness. You won't find this in the Web Client Software Factory.
Source: David Hayden ( Florida .NET C# SQL Server Developer )
Filed: Castle Project, MonoRail