Fork me on GitHub
Blog
The journal that this archive was targeting has been deleted. Please update your configuration.
Tuesday
Oct092012

Behavior Event

A Behavior Event (BE) is an application level event fired by Views. Views basically catch their own internal events and accordingly translate it to the appropriate application level event by wrapping a View Model (VM) and a Behavior Reference into a single Behavior Event (BE) object and then sending it to the Controller. The Controller would translate the Behavior Reference to an actual concrete Behavior to be executed against the View Model wrapped inside the Behavior Event. A Behavior Event enables Views to communicate to the Controller by isolating the concrete behaviors from the views and isolating internal view events from the rest of the application.

//from JUIPP core
public class BehaviorEvent<T> where T : IViewModel, new()
{
    public T ViewModel { get; set; }
    public string BehaviorReference { get; set; }
}
//from StudentProfileView view
protected void ButtonOpenStudentProfileClick(object sender, EventArgs e)
{
    //..translate a [button click event] to a [behavior event]..
    //..and fire (or send) the behavior event that would eventually
    //..open the StudentProfileView view
    this.SendBehaviorEvent(
      new BehaviorEvent<StudentViewModel>()
      {
          BehaviorReference = BehaviorReference.OpenStudentProfileBehavior,
          ViewModel = new StudentViewModel()
                          {
                              Id = "0001"
                          }
      });
}
Friday
Oct052012

Behavior

A Behavior (B) is a command-like class that can be executed by the Controller (C) against a specific View Model (VM) to retrieve and persist data or just transition a View (V) to another View (V). There are 3 types of Behaviors: Transition Behaviors (TB), Data Behaviors (DB), and Hybrid Behaviors (HB). Transition Behaviors are dynamically bound to a specific View (V), when executed would cause the current View (V) to hide and the bounded View to be rendered as the next visible View. Data Behaviors when executed against a specific View Model would interact with the actual data and keep the View Model in sync with the data. Hybrid Behaviors like the name suggests they are both Transition Behaviors and Data Behaviors; they keep the data and the View Model in sync and at the same time transition the current View to the bounded View.
public partial class OpenStudentProfileBehavior
{
    public override void Execute(
        BehaviorEvent<StudentViewModel> behaviorEvent)
    {
        StudentEntity entity;
        var id = behaviorEvent.ViewModel.Id;
        
        // find from database
        using (var data = new EntityModelContainer())
            entity = data.StudentEntities.First(s => s.Id == id);

        // translate data to ViewModel
        behaviorEvent.ViewModel =
            new StudentViewModel()
                {
                    Id = entity.Id,
                    FirstName = entity.FirstName,
                    LastName = entity.LastName,
                    Email = entity.Email
                };

    }
}
Friday
Oct052012

View

A View (V) is simply a user interface component in charge of presenting the data from a View Model (VM) and collecting user input, translating user interaction and gestures into an application level event and sends it to the Controller (C) in the form of Behavior Events.

<%@ Control 
Language="C#" 
CodeBehind="StudentProfileView.ascx.cs" 
Inherits="StudentProfileView" %>

ID:         <asp:Label runat="server" ID="LabelID"/> 
First Name: <asp:TextBox runat="server" ID="TextBoxFirstName"/>
Last Name:  <asp:TextBox runat="server" ID="TextBoxLastName"/> 
Email:      <asp:TextBox runat="server" ID="TextBoxEmail"/>


// CODE-BEHIND
public partial class StudentProfileView 
{
    public void Bind(StudentViewModel viewModel)
    {
        // data binding
        this.LabelID.Text = viewModel.Id;
        this.TextBoxFirstName.Text = viewModel.FirstName;
        this.TextBoxLastName.Text = viewModel.LastName;
        this.TextBoxEmail.Text = viewModel.Email;

        // render logic
        this.TextBoxFirstName.Enabled = viewModel.IsInEditMode;
        this.TextBoxLastName.Enabled = viewModel.IsInEditMode;
        this.TextBoxEmail.Enabled = viewModel.IsInEditMode;
    }
}
Friday
Oct052012

ViewModel

A ViewModel (VM) is a specialized representation of data that a View can use for data binding. You can think of a View Model as a lightweight domain entity that contains closely related properties that Views can consume to display a consolidation or subset of the data. ViewModels are used across the application as Data Transfer Objects (DTOs) and most times contain almost no business logic. Similar to Martin Fowler’s Presentation Model (PM).

 


    public partial class StudentViewModel
    {
        public string Id { get; set; } 
        public string FirstName { get; set; } // data 
        public string LastName { get; set; } 
        public string Email { get; set; } 
        public bool IsEditable { get; set; } // logic
    }
Page 1 2