Question

I've searched for an answer to this, but since I'm fairly inexperienced I may not even know the right keywords to find relevant information. Apologies if this is redundant...

I have a database with several tables that have identical structure - just named differently depending on sport type: i.e. MLB_Games, NFL_Games, and NCAAF_Games. In my C# class, I have logic that is applied the same way to each, only depending on which sport I happen to be working on. Is there any way to declare a single list so that I don't have to have 3 different lists, initialize the one for the sport I'm working on, and have a switch statement every time in the logic that I need to work with the data?

Here's the initialization code to help make sense of the question. It would be so much easier to just declare one List dbGames, fill it, and use that in the rest of the logic. Is this possible? If I try to just use var, I get an error that Implicitly-typed local variables must be initialized.

List<MLB_Games> dbMlbGames;
List<NCAAF_Games> dbNcaafGames;
List<NFL_Games> dbNflGames;

using (var dbContext = new SportsProjectXEntities(Properties.Settings.Default.currentEntityConnString))
{
    switch (sport)
    {
        case "MLB":
            dbMlbGames = dbContext.MLB_Games.ToList();
            break;
        case "NCAAF":
            dbNcaafGames = dbContext.NCAAF_Games.ToList();
            break;
        case "NFL":
            dbNflGames = dbContext.NFL_Games.ToList();
            break;
        default:
            Logger.Instance.LogMessage("Cannot initialize the requested sport: " + sport);
            break;
    }
}
Was it helpful?

Solution

Abstract your entites via interface, for example:

class MLB_Games : IGames
{
    public string Name { get; set; }
}
class NCAAF_Games : IGames
{
    public string Name { get; set; }
}
class NFL_Games : IGames
{
    public string Name { get; set; }
}
interface IGames
{
    string Name { get; set; }
}

Create a property

List<IGames> games;

And then load entities based on their type (you can use switch instead of dictionary, if you like. Matter of preference):

var gamesLoaders = new Dictionary<string, Func<IQueryable<IGames>>>
{
    {"MLB", () => dbContext.MLB_Games},
    {"NCAAF", () => dbContext.NCAAF_Games},
    {"NFL", () => dbContext.NFL_Games}
};

var sport = "MLB";

if(gamesLoaders.ContainsKey(sport))
    games = gamesLoaders[sport]().ToList();
else
    Logger.Instance.LogMessage("Cannot initialize the requested sport: " + sport);

Now you can work with one list of games. Note, that you should add specific behaviour for each concrete entity and expose it via IGames interface.

edit: If you are using model first approach and generate entities, you can use partial classes to define interface implementation in separate files. EF already insert partial keyword to generated classes. For example you can define:

public partial class MLB_Games : IGames
{
    //interface implementation is defined in generated file
}

Now C# compiler will combine this definition and EF generated definition. This file won't be affected when you will update your entities from edmx model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top