Home

Castle Stronghold

Dynamic Actions

Dynamic actions and action providers are a way to create some custom and dynamic functionality.

Dynamic Actions

What is an action? MonoRail considers every public instance method an action. Sometimes, this is not enough. You want to add some code to a name on the url that will only be available in runtime. Or sometimes a code for an action is the same for a sequence of controllers but they inherit from distinct super classes.

Dynamic Actions are a way to associate a code with a name in runtime.

Using it

A dynamic action is nothing more than an implementation of the interface IDynamicAction:


public interface IDynamicAction
{
    /// <summary>
    /// Implementors should perform the action 
    /// upon this invocation
    /// </summary>
    void Execute(Controller controller);
}

You can associate a dynamic action with a controller using the DynamicActions property:


public class MyController : Controller
{
    public MyController
    {
        DynamicActions["index"] = new IndexDynamicAction();
    }
}

Dynamic Action Providers

Dynamic Action Provider are in charge of adding dynamic actions to controllers. They can be associated with controllers using the DynamicActionAttribute:


[DynamicAction( typeof(MyDynActionProvider) )]
public class MyController : Controller
{
    public MyController
    {
    }
}

The implementation of the provider can be something static - ie always add the same set of actions - or can "read" something on the controller or from the logged user and add the actions accordingly:


public class MyDynActionProvider : IDynamicActionProvider
{
    public void IncludeActions(Controller controller)
    {
        controller.DynamicActions["index"] = new IndexDynamicAction();
    }
}
Google
Search WWW Search castleproject.org