Fork me on GitHub
Blog
The journal that this archive was targeting has been deleted. Please update your configuration.
« Catch and Report Behavior Exceptions | Main
Thursday
Oct112012

Define Behavior Context Objects

Most times you want to define application context data that all views and all behaviors want to know about, for example current login name, user role, etc. You don’t want your views to fire a behavior event each time they need an application context data, or pass every application context data you need to every behavior execute method call. You would rather define it in the controller on InitBehaviorContext and all behaviors and views would be able to access it using their own property called “BehaviorContext” which is basically a key-value pair of application context objects.

public partial class Controller
{
    // ...
    // override InitBehaviorContext 
    // ..to define application context objects
    protected override void InitBehaviorContext()
    {
        base.InitBehaviorContext();

        var iden = WindowsIdentity.GetCurrent();
        (this as IBehaviorContext)["UserViewModel"] =
            new UserViewModel()
                {
                    LoginName = iden != null ? iden.Name : null,
                    Role = "regular-user"
                };
    }
}
// a behavior implementation
public partial class OpenStudentProfileBehavior
{
    public override void Execute(BehaviorEvent<StudentViewModel> args)
    {
        base.Execute(args);
        // getting a behavior context object
        var userViewModel = 
            this.BehaviorContext["UserViewModel"] as UserViewModel;

        if (userViewModel != null)
        {
            var loginName = userViewModel.LoginName;
            var role = userViewModel.Role;
        }
    }
}
//some random View called "MyView"
public partial class MyView 
{
    public override void OnAfterTransition<T>(
                                BehaviorEvent<T> behaviorEvent)
    {
        // Get a behavior context object after a behavior event 
        // is executed and the transition event 
        // is targeted to this view.

        base.OnAfterTransition<T>(behaviorEvent);
        var userViewModel = 
            this.BehaviorContext["UserViewModel"] as UserViewModel;

        if (userViewModel != null)
        {
            var loginName = userViewModel.LoginName;
            var role = userViewModel.Role;
        }
    }
    public void Bind(MyViewModel item)
    {
        // you can also get a behavior context object on bind
        var userViewModel = 
            this.BehaviorContext["UserViewModel"] as UserViewModel;
        if (userViewModel != null)
        {
            var loginName = userViewModel.LoginName;
            var role = userViewModel.Role;
        }
    }
}

 

Redefining Behavior Contexts

On occasions, you might want to redefine behavior context objects depending on what behavior is being executed. First you have to catch behavior you are looking for before it gets executed and then you would override the behavior context objects:

public partial class Controller
{
    protected override void OnBeforeBehaviorEvent<T>(
        IBehaviorEventSender<T> sender, 
        BehaviorEvent<T> behaviorEvent)
    {
        base.OnBeforeBehaviorEvent<T>(sender, behaviorEvent);
        
        // only override behavior context for a specific behavior 
        if(behaviorEvent.BehaviorReference 
                         == BehaviorReference.MyBehavior)
        {
            (this as IBehaviorContext)["UserViewModel"] =
            new UserViewModel()
            {
                LoginName = @"domain\somelogin",
                Role = "admin-role"
            };
        }
    }
}

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>