Breaking News

c# – Union type in DDD model and in the database-Exceptionshub

Questions:

In the NBA basketball game I want to store play-by-play data. One Game has its StartTime, two Teams 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:

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 objects? 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?

How to&Answers:

You can use inheritance for this. Like:

public class BasePlay
{
    public DateTimeOffset StartTime { get; set; }
    public Team TeamA { get; set; }
    public Team TeamB { get; set; }
}

public class JumpBallPlay : BasePlay
{
    public string ExtraProperty1 { get; set; }
}

public class FieldGoalAttemptPlay : BasePlay
{
    public string ExtraProperty2 { get; set; }
}

Then, you can map this classes with EntityFramework using Table Inheritance:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity()
        .HasDiscriminator("PlayType")
        .HasValue("jumpBall")
        .HasValue("fieldGoalAttemptPlay");
}

All your objects will be stored in the same table, but mapped to a different columns (with some common for all columns from BasePlay, see Shared columns section). Extra column PlayType will contain the type of a play.

About rankhawks

Check Also

c# – Acumatica upgrade from 18.210 to 18.217 (Unable to cast object of type ‘PX.Data.PXResult`2[PX.Objects.FS.FSServiceOrder,PX.Objects.AR.Customer]’-Exceptionshub

Questions: The source code of the button action is public PXAction CreateSalesOrder; [PXUIField(DisplayName = "Create …

jQuery offline slideshow on Android-Exceptionshub

(function($){ $.fn.chocolate = function(args) { // Default Options var options = $.extend({ interval : 1000, …

c# – How to handle mocking (Rhino Mock) and unity container in different unit test classes?-Exceptionshub

Questions: I am working on a large desktop application. The application communicates with number of …

javascript – Actualize Database Data with Functions Cloud-Exceptionshub

Questions: I am developing an Android application and I have a problem that I cannot …

valgrind for Android devices (Crystax). configure: error: C compiler cannot create executables-Exceptionshub

Questions: Using and adapting the script(https://gist.github.com/smits/27060b310546ca6f76568f24838118c3) I get and error ” C compiler cannot create …

Leave a Reply

Your email address will not be published. Required fields are marked *