Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Castle.MonoRail.Framework.IFilter – Part I

leave a comment »

Today I would like to share some technique in using Castle.MonoRail.Framework.IFilter . I am a bit sad because it takes too much time for me to realize how useful, until I read between the line over and over again

Filters are executed before and|or after your actions. It is useful for security, dynamic content and to keep away repetitive code.

Keep away repetitive code is what I am tracing, let the hear the story shall we?

This happen with one of the real world project that I am working on Data Bridge like any other web application it need authentication and passing some user preference along each action (inherit from SecureController) to the view such as List, Add, Edit & Delete action & view.

This can be done using the following technique:

Create Filters\AuthenticationFilter.cs

public class AuthenticationFilter: IFilter
{
 bool IFilter.Perform(ExecuteEnum exec,
     IRailsEngineContext context, IController controller)
 {
   if( !UserNotLogin() ) {
       Redirect("Login", "Index");
   } else {
       controller.PropertyBag["maxRow"] = GetUserMaxRowPreference();
       //... set more preferences as needed
   }
 }
}

Create SecureController

[Layout("default"), Rescue("generalerror")]
[Filter(ExecuteEnum.BeforeAction, typeof(AuthenticationFilter))]
public abstract class SecureController : ARSmartDispatcherController
{
    // ... more code
}

SecureController is marked as abstract, prevent from direct use.

Using SecureController

public abstract class ProductController: SecureController
{
    public void List() { //... list action code }
}

Now view part: View\Product\list.vm
When render table I want to display miximum $maxRow rows.

See there is no code need to set preference in each action method, nice.

Advertisements

Written by Chorn Sokun

December 31, 2007 at 3:14 pm

Posted in Castle, MonoRail

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: