Home

Castle Stronghold

More about SmartDispatcherController

The SmartDispatcherController goes beyond what has been introduced on the previous document. This document goes a little further introducing more features required for every real web application development.

Source

By default the binder will use the Params collection as source of information to bind data. You can define that it should use the QueryString or Form post data instead. To do that use the From property exposed by the DataBindAttribute.

This is a recommend practice for performance and to prevent people from easily override form parameters. For example:


using Castle.MonoRail.Framework;

public class ProductController : SmartDispatcherController
{
    public void Create([DataBind("product", From=ParamStore.Form)] Product product)
    {
        ...
    }
}

Defining accessible properties

As the DataBindAttribute usually act on domain model classes, you might not want that all properties be "bindable". Suppose you are binding an User class. Sensitive properties might allow overriding the password, roles, access levels or audit information. For these cases you can use Allow and Exclude properties of DataBindAttribute.

The values for these properties are a comma separated list of property names. For example:


public class AccountController : SmartDispatcherController 
{
    public void CreateAccount([DataBind("account", Allow="Name,Email,Password")] Account account)
    { 
        ...
    }
}

This indicates that you only want to allow the Name, Email and Password properties to bound with the values from the request. All other properties will be ignored.

The Exclude property is the inverse. It prevents the properties indicated from being used, and allow all others.

Binding Errors

Binding errors might ocur, like invalid dates or problems in data conversion. When using simple binding, an exception will be thrown. When using the DataBindAttribute, however, no exception will be thrown.

To access the error information, use the GetDataBindErrors method:


public class AccountController : SmartDispatcherController 
{
    public void CreateAccount([DataBind("account")] Account account)
    { 
        ErrorList errors = GetDataBindErrors(account);

        ...
    }
}

The ErrorList implements the ICollection so you can enumerate the problems. You can also check if some specific property could not be converted. For example:


public class AccountController : SmartDispatcherController 
{
    public void CreateAccount([DataBind("account")] Account account)
    { 
        ErrorList errors = GetDataBindErrors(account);

        if (errors.Contains("DateOfBirth"))
        {
            Flash["error"] = errors["DateOfBirth"].ToString(); // Or Exception

            RedirectToAction("New", Params);
        }

        ...
    }
}

BindObject and BindObjectInstance

You do not need to always use parameters to have an object bound. The methods BindObject and BindObjectInstance, exposed by the SmartDispatcherController, allow you to have the same functionality. The benefit is that not under every case you want to perform the bindings. For example:


public class AccountController : SmartDispatcherController 
{
    public void CreateAccount(bool acceptedConditions)
    { 
        if (acceptedConditions)
        {
            Account account = (Account) BindObject(ParamStore.Form, typeof(Account), "account");
            
            ...
        }
        
        ...
    }
}
Google
Search WWW Search castleproject.org