implementing the balance code to a webform

I've been exploring context specification driven development in earlier posts (all avilable on this summary page).
In this post I'm adopting my progressive widening strategy so that I can deliver my user story all the way through to the UI and have a working product at the end of my work on that feature.

I therefore need to create a new web project - I name it Plutus.Web and I add a page into a new "Views" folder - Balance.aspx. I add some html code as follows:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewBalance.aspx.cs" 
Inherits="Plutus.Web.Views.ViewBalance" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Current Balance</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        The current balance is <asp:Label runat="server" id="currentBalanceLabel"/>
, taken on <asp:Label runat="server" ID="currentBalanceTakenLabel"/>. 
    </div> 
    </form> 
</body> 
</html>

With this html I've left room for the current balance to be displayed - both the amount and the date have labels associated with them.
Back in the code behind I now implement the IBalanceView (while first referencing the Plutus.Core project):

public partial class ViewBalance : System.Web.UI.Page, IBalanceView 
{ 
    private BalancePresenter balancePresenter; 
    private Action<Balance> newBalanceAvailable; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
        balancePresenter = new BalancePresenter(this, new MemoryRepository()); 
        balancePresenter.Initialise(); 
    } 

    public void SetBalance(Balance balance) 
    { 
        string balanceText; 
        string balanceDateText; 
        if (balance == null) 
        { 
            balanceText = "0 (no balance has been stored)"; 
            balanceDateText = "(not yet taken)"; 
        } 
        else 
        { 
            balanceText = balance.Amount.ToString(); 
            balanceDateText = balance.Date.ToString("dd-MMM-yyyy"); 
        } 
        currentBalanceLabel.Text = balanceText; 
        currentBalanceTakenLabel.Text = balanceDateText; 
    } 
}

In the Page_Load method I am creating a new BalancePresenter and sending this instance of the Page in whilst creating a new MemoryRepository instance.
I am then calling Initialise() which from the specifications I wrote in the previous posts should call through to the interface method SetBalance().
Within that method I am checking for the presence of a balance and displaying text accordingly.

I make this ViewBalance page my default start page and run the web project to see the results - I get the following text on screen:

"The current balance is 0 (no balance has been stored), taken on (not yet taken)."

I can also amend the implementation of SetBalance() in the MemoryRespository and verify that it brings back the right balance:

public Balance GetCurrentBalance() 
{ 
    return new Balance(10.52, DateTime.Now.AddDays(-2)); 
}

If I rebuild and access the web page again I get the following text:

"The current balance is 10.52, taken on 15-Apr-2009.".

I've now successfully delivered my first story using context specification to deliver as much code as possible outside of the UI.
I've explored how to set up a context for the observations I am making via a "context" delegate, how to have an Act part of the AAA testing syntax via a "because" delegate and how to use the library's extension methods to make the observation code much more descriptive.

In a future post I will move onto a second user story and begin some more complex specifications.



0 comments: