I have a list of Games users can join. To select a game, they have to pick a position (ie., role) within that game.
Model
[Game] 1 <-> * [PlayerGame] * <-> 0..1 [Player]
The PlayerGame entity defines the picked position by the player.
Other details
- All the games are listed within a MVC 3 View.
- Data is persisted within a SQL Server 2008 instance and accessed via EF4 (CodeFirst).
- The view updates itself via some jQuery Get() calls that pulls up-to-date data each N milliseconds.
- The higher the delay (N), the bigger the chances of racing conditions, where 2 users would try to pick the same position.
- I will obviously need to do some server-side validation to allow only one of the 2 users to get the selected position, if they both pick the same position at the same time.
What would be the best way to asses only 1 person gets the selected position while the other gets an error message?
I’m currently thinking about using TimeStamps to make sure only 1 of the 2 calls can successfully update the PlayerGame row. Is that a good idea?
Yes use timestamps. That is the way which is usually used and both SQL server (any database server) and EF supports them. You will have to roundtrip timestamp to the client and use it when constructing entity to be updated (in detached scenario).