Hello XAFers! So January’s passed and all those great New Year’s resolutions for getting fit have fallen by the wayside. They have haven’t they, come on you can tell me? Yeah I thought so. Well don’t worry ‘cos I’m here to help. By, here to help, what I mean is that I too have a New Year’s resolution for losing weight and getting fit. Of course, when I start to lose motivation I turn to XAF to help me out, I mean what could be better than having a hot piece of software to help you? Okay, I admit, a hot personal trainer would be better, but it is what it is right?
Anyway, today we’ll begin a short series of posts on an application that I threw together (yes I do mean threw together and not developed) to help me measure my weight lose and fitness program. So, the first order of business is going to be to record my weight as it heads down towards my target. Let’s add a domain object to our application and add properties for the date the record was made and my weight that day, in pounds:
using System;
using DevExpress.Xpo;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
namespace Fitness.Module
{
[DefaultClassOptions]
public class WeightRecord : BaseObject
{
public WeightRecord(Session session) : base(session) { }
private DateTime date;
public DateTime Date
{
get
{
return date;
}
set
{
SetPropertyValue("Date", ref date, value);
}
}
private int lBS;
public int LBS
{
get
{
return lBS;
}
set
{
SetPropertyValue("LBS", ref lBS, value);
}
}
}
}
Okay, well working in pounds is fine for my American cousins, but here in the UK we work in stones and in Europe they work in Kilos, so we should really show those values too. We don’t want to input that information three times though and we don’t have to as we can calculate both of these fields from the lBS field. To do that, modify the class definition like so:
using System;
using DevExpress.Xpo;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
namespace Fitness.Module
{
[DefaultClassOptions]
public class WeightRecord : BaseObject
{
public WeightRecord(Session session) : base(session) { }
private DateTime date;
public DateTime Date
{
get
{
return date;
}
set
{
SetPropertyValue("Date", ref date, value);
}
}
private int lBS;
public int LBS
{
get
{
return lBS;
}
set
{
SetPropertyValue("LBS", ref lBS, value);
}
}
[Persistent]
public double Stones
{
get
{
const double STONES_PER_POUND = 0.0714285714;
return Math.Round((lBS == 0) ? 0 : lBS * STONES_PER_POUND,2);
}
}
[Persistent]
public double Kilos
{
get
{
const double KILOS_PER_POUND = 0.45359237;
return Math.Round((lBS == 0) ? 0 : lBS * KILOS_PER_POUND,2);
}
}
}
}
We mark the property with the persistent attribute as we want the value to be saved in the database. Notice there is no setter though, we just do the calculation in the getter. There is one more thing we need to do for this to work and that is to set the ImmediatePostData attribute to true on those calculated fields in the WeightRecord node on the Model Editor. First find these fields in the Model Editor:
Then set the ImmediatePostDataAttribute to true:
This attribute specifies whether the property value is updated immediately when changes occur in the current Property Editor's bound control.
Now, let’s fire up the application and we can see that when we enter an amount for the pounds, we get a value calculated for stones and kilos too:
Looking at the list view, we can see that these values are persisted to the database:
Well that wraps it up for this post, next time we’ll add some training programs to the application – until then, happy XAFing! :-)