Blogs

Gary's Blog

Keeping Fit With XAF #1

     

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:

image

Then set the ImmediatePostDataAttribute to true:

image

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:

image

Looking at the list view, we can see that these values are persisted to the database:

image

Well that wraps it up for this post, next time we’ll add some training programs to the application – until then, happy XAFing! :-)

Published Feb 09 2010, 02:50 PM by Gary Short (DevExpress)
Filed under:
Technorati tags: XAF
Bookmark and Share

Comments

 

Chloe Anfield said:

Now, I know this is a quick and dirty application, but one should be able to enter data in any of the three units of measure. Perhaps an enum for the UOM, a float property for Weight, etc.

February 9, 2010 11:04 AM
 

Gary Short (DevExpress) said:

Well my scales only weigh in pounds. :-)

February 9, 2010 11:11 AM
 

Chloe Anfield said:

Does your scale measure body fat % as well ? A worthwhile addition to your WeightRecord class.

February 9, 2010 11:41 AM
 

Steve Sharkey said:

If your doing stones then it should be a text field listing in stones & lbs - who uses 13.231456 stone?

February 10, 2010 3:40 AM
 

Alain Bismark said:

I Gary,

I dont know how complex will be this xaf application, but i think that this post is very similar to others....

I think that the community need a "more complex" post from the xaf evalengist, for example how to solve things like :

- How to load a partial object, for example if we have a "big object" with many integer and string properties (Aggregateds is not the answer), and we want to populate a lookup control in xaf, and we dont want to load all the properties from the database, how i can do that??,

- How to personalize the lookup window in xaf (in windows and xaf), in order to allow search for many fields at the same time like the ERPs.

- How to optimize the accessing to database from the architecture perspective of object definitions.

- How to optimize the xaf/web applications??

Some questions might be belongs to support, but i think that new user and experimented xaffer needs this kind of things in this blog with your code and your explanation.

Regards,

February 10, 2010 4:05 PM
 

Phil Allen said:

I must be missing something but why do you want to persist stones and kilos to the database? Isnt this redundant info - not to say a potential source of error?

February 10, 2010 11:02 PM
 

Gary Short (DevExpress) said:

Hi Phil,

Whether or not the information is redundant depends on your point of view I guess. If I was only going to be using this application to access the data then I guess the data could be redundant to a certain extent, but there again you could make the arguement that it is better to calculate once and store than to calculate every time.

However, in this case, I want to access the data from Excel later so I want all the data to be present.

February 11, 2010 9:04 AM
 

Gary Short (DevExpress) said:

Alain,

I'll look into these topics for you.

February 11, 2010 9:17 AM
 

Gary's Blog said:

In the first post in this series we looked at creating a WeightRecord. Today, we are going to look at

February 22, 2010 8:48 AM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.