How to calculate Australian Golf Handicap using XPO

ASP.NET Team Blog
16 July 2010

Check out this informative code central example that shows you how to calculate the Australian Golf Handicap using eXpress Persistent Objects (XPO):

Example Details: How to calculate Australian Golf Handicap using XPO

Features

The example demonstrates how to use the ASPxGridView with XPO to calculate the handicap used in Australian golf. The calculation algorithm and some sample data come from the The "Rolling Sample" Handicap Calculation Method article.

The sample features:

  • Linq-To-Xpo demonstration
  • Instead of unbound columns, non persistent fields of XPO objects are used
  • Editing similar to master-detail relationship

And it’s a complete sample. For example, here is the Player.cs file which defines the ‘Player’ class. To see the VB.NET version of this project, click the ‘Programming Language’ dropdown on the sample

using System;
using System.Linq;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using System.Collections.Generic;

public class Player : XPObject {
    const Double BonusForExcellense = 0.96;

    public Player()
        : base() { }

    public Player(Session session)
        : base(session) { }

    public override void AfterConstruction() {
        base.AfterConstruction();
    }

    protected String _Name;
    public String Name {
        get { return _Name; }
        set { SetPropertyValue<String>("Name", ref _Name, value); }
    }

    [NonPersistent]
    public Int32 Handicap {
        get {
            List<Result> results = LastBestTen();

            if (results.Count == 0)
                return 0;

            Double handicap = 0.0;

            handicap = results.Average<Result>(x => x.PlayedTo); // average
       
            handicap *= BonusForExcellense; // avarage * 0.96
            handicap = Math.Round(Math.Truncate(handicap * 10) / 10); // 14.496 -> 14.4 -> 14

            return Convert.ToInt32(handicap);
        }
    }

    [Association("Player-Results", typeof(Result))]
    public XPCollection<Result> Results {
        get { return GetCollection<Result>("Results"); }
    }

    public List<Result> LastBestTen() {
        if (Results.Count <= 10)
            return Results.ToList<Result>();

        XPQuery<Result> results = new XPQuery<Result>(this.Session);

        var list1 = (from r in results
                     where (r.Player == this)
                     orderby r.Date descending
                     select r).Take(20).ToList<Result>();

        var list2 = (from r in list1
                     orderby r.PlayedTo ascending
                     select r).Take(10);

        return list2.ToList<Result>();
    }
}

Click and Run

You can download and run the Code Central samples direct from your local machine. Just click the ‘Download Source Code’ button for the sample page.

Watch the ‘How To Use Code Central’ video to learn more:

image

Thanks Vest!

Vest, one of our awesome support engineers, created this sample for one of our awesome customers, Daryn.

Check out the ‘Australian Gold Handicap using XPO’ sample. Then drop me a line below with your thoughts.

Follow MehulHarry on Twitter

DXperience? What's That?

DXperience is the .NET developer's secret weapon. Get full access to a complete suite of professional components that let you instantly drop in new features, designer styles and fast performance for your applications. Try a fully-functional version of DXperience for free now: http://www.devexpress.com/Downloads/NET/

 

7 comment(s)
Glen Harvy
Glen Harvy

Harry, I am right in the midst of my beginning to learn XPO and jumped at the opportunity to see a real life example of XPO in action. Unfortunately, it is lacking the same simple item as many of your examples - not one single comment line anywhere.

   /// <summary>

   /// An XPO Session is used generally because blah blah blah

   /// Have a look at <a bla bla> for more detailed info</a>

   /// </summary>

   /// <returns>The session object to be used</returns>

   public static Session GetNewSession() {

       return new Session(DataLayer);

16 July, 2010
Brendon Muck [DevExpress MVP]
Brendon Muck [DevExpress MVP]

There is no computer on Earth powerful enough to computer my handicap. It requires the use of NASA super computers and a scientific notation so scary that it can't be mentioned by name.

17 July, 2010
Vest (DevExpress Support)
Vest (DevExpress Support)

Hello Glen,

The sample uses an approach from our KB article: www.devexpress.com/.../K18061.aspx

Please take a look at it. It should help you.

17 July, 2010
Louis Z
Louis Z

Great example, thanks for posting.  

Would I be able to coax anyone into doing an XAF version.  It would be of great learning value to me.

Thanks,

Louis

20 July, 2010
Louis Z
Louis Z

Great example, thanks for posing.

Would I be able to coax anyone into doing an XAF version.  It would be a great learning example for me.

Thanks,

Louis

20 July, 2010
James S K Makumbi
James S K Makumbi

Hi Harry,

Wonderful example. I have to agree with Louis above. Please get Gary and implement this in XAF. I have already spoken with Gary on twitter. Basically i think what we would like to to see is a app with several tables that involves calculations like the one above then updates to tables.

10 August, 2010
Dennis (DevExpress Support)
Dennis (DevExpress Support)

@Louis, Jake:

We appreciate your feedback. Actually, there is already a corresponding CC example for XAF. It's also described in the product's documentation: documentation.devexpress.com

So, instead of implementing the same code in an XAF application, I would recommend that you learn the concept of accomplishing such tasks. This will help you implement any calculation algorithm your business needs dictate.

Feel free to contact us via the Support Center, if you need any assistance with our products.

17 August, 2010

Please login or register to post comments.