Tour of MonoRail Series

Due to a spell of blogger’s block, I thought I would do a series postings to help me get past it. This post begins a series on highlighting various features of the Castle Project MonoRail. All series will be examples from the MonoRail trunk. Some of the features I will be discussing are only available on the trunk (specifically the new Routing Module). To start off the series. The first part will cover MonoRail helpers. The series will have the following topics:


Part 1. Helpers
Part 2. UI Basics (Layouts, Views and Shared Views, View Components)
Part 3. Using the new Routing Module
Part 4. Authentication/Security

Part 5. Dynamic Actions
Part 6. Using JsonReturnBinder for serializing to JSON
Part 7. Creating Wizards using IWizardController

 

Helpers are classes that are created specifically for providing your view templates with a minimal amount of work to perform or some form of html generation. They should not be used for complex operations or domain logic except for some rare circumstance. They are amazingly simple. A prime example of a helper at work is the FormHelper. This Helper generates various html tags for the purpose of easily using the DataBinder on your controllers. Let’s look at the code required to have the FormHelper generate a TextField for us:

 

   1:  $FormHelper.TextField(“blogpost.title”)

 

This will generate the following code when rendered:

 

   1:  <input type=“text” id=“blogpost_title” name=“blogpost.title” />

What happens here is the FormHelper creates the requested form field and sets the id/name attributes properly so the data can be data bound on the controller action which would look something like this:


   1:  public void CreateBlogPost([DataBind("blogpost")] BlogPost blogpost) {}


As long as you specify the correct id prefix as it appears in your template, MonoRail will do all the binding on the controller. Your controller must also derive from SmartDispatcherController otherwise you will get an exception at runtime. There are many more features that are provided with FormHelper that I will leave you to explore here. There are a number of other helpers available such as AjaxHelper, UrlHelper and DateFormatHelper.


If you want to create your own Helper for whatever task you have at hand all you need to do is create a class and then add an attribute to any controllers that wish to use the helper. The code would look like so:


   1:  public class MyCoolHelper
   2:  {
   3:      public string DoSomethingCool()
   4:      {
   5:          return “cool stuff”;
   6:      }
   7:  }

 

The controller that wishes to use the Helper would look like so:

 

   1:  [Helper(typeof(MyCoolHelper))]
   2:  public class SomeController
   3:  {
   4:  }

It’s as easy as that. This first post is pretty basic. I wanted to start off simple and work up to some of the other topics such as Dynamic Actions and doing Ajax/JSON related data retrieval.


Next time we will go over layouts, views and view components. Should be a fun time!

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Sean Chambers

I am a Senior software developer from Palm Coast, Florida. An advocate of Domain Driven Design, Behavior Driven Development, creator of FluentMigrator and community activist. I am married to my beautiful wife Erin and am the proud father of two wonderful children. I currently reside at ACI, a local insurance industry/mortgage software company that excels in creating solutions using Agile methodologies.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Dan

    Seems like a nice serie, thanks !