Log in / create account

FubuMVC wiki
[FubuMVC home]

Behaviors

From FubuMVC Wiki

Behaviors

A Behavior is a class that contributes to the fulfillment of a request. Sounds very generic, because it is. There are no strict rules about what a Behavior can or should do. For example, to fulfill a request, you might need to “log the request”, “execute a one input/one output controller action”, and “render a WebForms view”. In FubuMVC, those are all implemented as distinct Behaviors. The only requirement is that the class implement the IActionBehavior interface:

 public interface IActionBehavior {
   void Invoke();
   void InvokePartial();
 }

Chaining Behaviors

Technically, only a single Behavior is registered for each Route in your application. However, that single Behavior can have a reference to a second Behavior. At any point during the Behavior’s execution, it can yield control to the second Behavior. Since that second Behavior can have a reference to a third Behavior, and the third can reference a fourth, you can see how any number of Behaviors can potentially contribute to the fulfillment of a given request. The abstract base class BasicBehavior exists to facilitate this common pattern. The ability to chain Behaviors is very important, as it allows each Behavior to be designed for a Single Responsibility, making them easy to mix-and-match with other Behaviors.

BasicBehavior

FubuMVC provides an abstract base class to help you implement your custom behaviors.

using System;

namespace FubuMVC.Core.Behaviors {

   public enum PartialBehavior
   {
       Ignored,
       Executes
   }
   public abstract class BasicBehavior : IActionBehavior
   {
       private readonly Action _partialInvoke;
       protected BasicBehavior(PartialBehavior partialBehavior)
       {
           _partialInvoke = partialBehavior == PartialBehavior.Executes
                                ? (Action) (Invoke)
                                : () => { if (InsideBehavior != null) InsideBehavior.InvokePartial(); };
       }
       public IActionBehavior InsideBehavior { get; set; }
       public void Invoke()
       {
           if (performInvoke() == DoNext.Continue && InsideBehavior != null)
           {
               InsideBehavior.Invoke();
           }
           afterInsideBehavior();
       }
       public void InvokePartial()
       {
           _partialInvoke();
       }
       protected virtual DoNext performInvoke()
       {
           return DoNext.Continue;
       }
       protected virtual void afterInsideBehavior()
       {
       }
   }

}

When you inherit from this base class it's important to remember that the IoC container that you are using will be composing your behavior for you. This allows you to use constructor injection to gain access to any services that you have registered in your container. The class will however require you to specify if your behavior executes a PartialBehavior. You will need to determine this yourself, your container will not be injecting this for you.

   public class MyBasicBehavior : BasicBehavior
   {
       public MyBasicBehavior() : base(PartialBehavior.Ignored)
       {
       }
       protected override void afterInsideBehavior()
       {
           // do some stuff after the inner behavior
       }
       protected override FubuMVC.Core.DoNext performInvoke()
       {
           // write some code
           return base.performInvoke();
       }
   }

PartialBehavior.Ignored will call the Invoke method and PartialBehavior.Execute will call InvokePartial

Page Discussion View source History