Fork me on GitHub

Conventions and Policies Edit on GitHub


TODO(More here!)

  • Using BehaviorGraph
  • Local v. Global
  • Reordering policies
  • Chain sources -- but not going to encourage folks to use this much anymore
  • link to routing policies

Custom policies

Jasper will retain the "BehaviorGraph" configuration model, but will eliminate the IActionBehavior interface objects in favor of inline, generated code to be much more efficient than FubuMVC's current runtime pipeline.

All the behavior chains (message handlers, HTTP endpoints, and job executions) are first modeled at application bootstrapping time through the behavioral model before they are "baked" into the underlying StructureMap container. This design opens up a lot of opportunities to extend FubuMVC applications by adding (or removing) extra behaviors to any chain.

Let's say that you want to log a simple message for each chain execution. You might first build a custom IActionBehavior like this:


public class ActionLogger : WrappingBehavior
{
    private readonly ICurrentChain _chain;
    private readonly ILogger _logger;

    public ActionLogger(ICurrentChain chain, ILogger logger)
    {
        _chain = chain;
        _logger = logger;
    }

    protected override void invoke(Action action)
    {
        _logger.Debug($"Running chain {_chain.Current.Title()}");
        action();
    }
}

To get that behavior applied before any HTTP endpoint action or message handler, we could write our own IConfigurationAction like this one:


public class MyLoggingPolicy : IConfigurationAction
{
    public void Configure(BehaviorGraph graph)
    {
        foreach (var chain in graph.Chains)
        {
            chain.WrapWith<ActionLogger>();
        }
    }
}

To add the custom policy to your application, use this code:


public class FubuAppWithLoggingPolicy : FubuRegistry
{
    public FubuAppWithLoggingPolicy()
    {
        // Apply the MyLoggingPolicy to every
        // chain in the system
        Policies.Global.Add<MyLoggingPolicy>();

        // or

        // Apply the MyLoggingPolicy to only
        // the chains discovered from the main application
        // or a local extension
        Policies.Local.Add<MyLoggingPolicy>();
    }
}