In the NBA basketball game I want to store play-by-play data. One Game has its StartTime
, two Team
s and the list of plays. Each Play has its occurrence in time and (depends on the play) different properties. There are 8 different play types, for example: "Rebound", "FieldGoalAttempt":
Rebound:
- team
- player
- reboundType
Field Goal Attempt:
team
shootingPlayer
assistingPlayer
blockingPlayer
shotType
distanceFeet
points
location
result
As you can see, these two are totally different plays - and there are 6 more types. Which brings me to my first problem.
Question 1: How do I implement it in C#?
My idea is to create a type Play
, and then 8 classes representing play types, like Rebound
, FieldGoalAttempt
, JumpBall
like this:
public class Play
{
public int PlayId { get; set; }
public int GameId { get; set; }
public int PlayerId { get; set; }
public Rebound Rebound { get; set; }
public FieldGoalAttempt FieldGoalAttempt { get; set; }
public Turnover Turnover { get; set; }
public Foul Foul { get; set; }
...
public JumpBall JumpBall { get; set; }
public Game Game { get; set; }
public Player Player { get; set; }
}
But I wasn't sure if this is a "code smell". I had a few more ideas: A list of object
s? or a custom built C# class that acts like "Union"? So first question is How should I handle this situation in C#? Is my first idea good?
Question 2: After all, I want to store these plays in the database. In my architecture I use Models that represents tables in the database. For my logic I create Service
, so models are "anemic" which some people call "anti-pattern", but I don't want to talk about it. I have Services for my logic and it works for me.
If I decide to create Union
like class - How do I store these plays in the database?