Story Details for articles
Golf Tracker - Ep. 10 - Player Refactor - Part 1
8/24/2011 7:51:34 PM
8/24/2011 7:51:34 PM
Story Series Collection
Golf Tracker - Player Refactor - Part 1It'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
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.
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.
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.
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
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.
- 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
- Create a custom model binder - this would also work but seems like too much work
- 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.
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.
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.
ConclusionIn 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.
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