Story Details for articles

Golf Tracker - Ep. 10 - Player Refactor - Part 1

kahanu
Author:
Version:
Views:
3680
Date Posted:
8/24/2011 7:51:34 PM
Date Updated:
8/24/2011 7:51:34 PM
Rating:
3/1 votes
Framework:
ASP.NET MVC 2
Platform:
Windows
Programming Language:
C#
Technologies:
Tags:
golf, golf tracker
Demo site:
Home Page:
Share:

Golf Tracker - Player Refactor - Part 1

It's time to do a little refactoring.  I can see now that my initial database schema is not sufficient for my new needs and I'll have to make some changes.  Let me explain what the changes are and why I'm making them.

This is the primary issue involved here:
  • No way to know whether the players index is plus or minus
Here's a look at the original page with the form to enter a player and his handicap.

power point slide of player form before refactoring

This shows the three fields that are necessary to enter a new player into the system.  The only problem is that there's no way to tell whether this player is a plus or minus handicapper.

Why this is important is shown on the slide.  Handicaps for players who consistently shoot above par, are negative.  And vice versa, the handicaps for players who consistently shoot below par, are positive.

To explain why this is I'll go into a little more detail about what handicaps are and why they are used.

A handicap in golf are similar to handicaps in other sports like bowling and horse racing.  It's a method to allow multiple competitors to compete against each other even if they aren't of the same skill level.  Handicaps make competing fair between players.

This will illustrate how a typical handicap calculation works.

high handicapper calculation

In this example, the player shoots a gross score of 85 on a golf course with a par of 72.  When you subtract the par from his score you are left with 13, and for handicap purposes, this 13 is converted to -13.  For the next time he plays golf you can use this handicap to calculate his net score.

You would take his gross score, ADD it to his handicap index which is -13 and the result will be his net score.

Conversely, for a much better player, the calculation would look like this.

handicap calculation for low handicapper

Notice for both examples, we are following the same formula to calculate his net score, which is:

Gross Score + Index = net score

Since in both cases we add the index to the gross score, we need a way to know whether the handicap that is entered is a "plus" or "minus" value.  I'll first modify the web page to be closer to what I need.

player form after changes

Now that I have the check box for the IsPlus property value that I will be creating, I will have a way to check whether the player is a plus or minus handicapper.

In order to make this change I'll have to change the following items:
  • Player business object
  • Linq-To-Sql classes
  • Database schema on the Player table
  • DataMappers
In the database I'll be adding a new column to the Player table called IsPlus and give it a "bit" data type.  In the Player business object I'll be adding a new property called IsPlus and giving it a "bool" data type.  Then I'll update the Linq-To-Sql classes by deleting the Player entity from the designer and then dragging it back in from the Server Manager. 

Now I can update the DataMapper class in the Data Layer for the new property and I'm getting closer to being refactored.  There is still the issue of determining whether the posted value is plus or minus.  There are many ways that it can be handled.  These are three of them.
  1. Create a private function in the controller - putting code in the controller can definitely work but there is the issue if I want to reuse that function somewhere else
  2. Create a custom model binder - this would also work but seems like too much work
  3. Create a custom DataAnnotations attribute - this is what I'll use.  I can create a custom class that will perform the necessary actions to determine the result I need and it will be attached to the Player business objects class.
So how is this used?

custom attribute on property

I can create the attribute and then apply it to the HandicapIndex property, but this won't work since I also need the IsPlus value.

A better solution is to adorn the class with the new attribute.

custom attribute on clas

This way I can get access to any of the classes properties.  What this custom attribute will do is simply return a boolean of whether the players index is plus or minus.

Conclusion

In ASP.NET MVC is getting very easy to accomplish just about anything you need in order to achieve a goal.  Refactoring and then creating a custom attribute to perform an algorithm is quick and easy.

In the next episode, I'll go into more details of how this actually works.

Stay tuned.

Comments

  • benoitricaud@hotmail.com
    Posted By: benoitr
    On: 9/27/2012 3:53:57 PM

    Dear sir,

    Do you plan to make a new project series with EF ?

    it could be great to see you in action! I am learning a lot through your videos

    Kind regards

  • info@kingwilder.com
    Posted By: King Wilder
    On: 9/27/2012 6:20:31 PM
    @benoitr - yes I've built some applications with EF and the DbContext and I plan on adding some new videos and articles.  Hopefully I can find time in the next few weeks, but I've just moved and I need to settle in a bit first.  Thanks.

 

User Name:
(Required)
Email:
(Required)