The SmartDispatcherController in MonoRail will make your life easier
It has been awhile since I last wrote about MonoRail due to a lot of work at my job, but I will try to make up for that with this article and hopefully a few more.
The SmartDispatcherController in MonoRail is one of the things that I really like in the framework, it really increases the speed at witch you can write some of your code – lets take an example.
1 2 3 4 5 | <form action="/user/create.rails"> Email: <input type="text" name="email" /> <br /> Password: <input type="text" name="password" /> <br /><br /> <input type="submit" value="Create" /> </form> |
This looks pretty straight forward, no magic here, so lets take a look at the code that will be executed when you hit the submit button.
1 2 3 4 | public void Create(string email, string password) { // Create your user } |
This is where the SmartDispatcherController kicks in, as you can see the parameters in the functions matches the elements from the form and better yet, it will (if capable) convert it to the datatype you have specified – now that’s cool, right!
If you think this is cool, then think about how cool it would be to do this with objects. Let us make a simple class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class User { private string email; private string password; public string Email { get { return email; } set { email = value; } } public string Password { get { return password; } set { password = value; } } } |
Nothing fancy here, so moving on to the html.
1 2 3 4 5 | <form action="/user/create.rails"> Email: <input type="text" name="user.Email" /> <br /> Password: <input type="text" name="user.Password" /> <br /><br /> <input type="submit" value="Create" /> </form> |
It looks quite similar to the first html example but if you look a little closer you will see that the name of the input elements is slightly different (adding user). That part will work like a prefix when we parse it to our function.
Time to add some SmartDispatcherController magic.
1 2 3 4 5 | public void Create([DataBind("user")]User user) { // Create your user user.Create(); } |
Now the object and the properties have been populated with the data we entered into the input fields - This is so awesome.
There is no doubt about that this will improve your productivity and I seriously hope that Microsoft will learn from this and add it to their MVC framework.