C # anonymous type for identical database tables with different names

I was looking for the answer to this question, but since I'm pretty inexperienced, I don’t even know the right keywords to find the relevant information. Sorry if this is redundant ...

I have a database with several tables that have the same structure - just differently depending on the type of sport: i.e. MLB_Games, NFL_Games, and NCAAF_Games. In my C # class, I have logic that applies the same for everyone, only depending on which sport I will be working on. Is there a way to declare one list, so I don’t need to have 3 different lists, initialize the one for which I work, and each time specify the switch statement in the logic with which I need to work the data?

Here's the initialization code to help sort out the issue. It would be much easier to just declare a single dbGames list, populate it, and use the rest of the logic. Is it possible? If I try to just use var, I get an error that should be initialized by implicitly typed local variables.

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; } } 
+4
source share
1 answer

Abstract your entites through an 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 Property

 List<IGames> games; 

And then load the entities based on their type (you can use switch instead of the dictionary if you want. Question about 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 must add specific behavior for each specific object and expose it through the IGames interface.

edit If you use the first approach of the model and generate entities, you can use partial classes to determine the implementation of the interface in separate files. EF already inserts the partial keyword into the generated classes. For example, you can define:

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

Now the C # compiler will combine this definition and the generated definition of EF. This file will not be affected if you update your objects from the edmx model.

+2
source

Source: https://habr.com/ru/post/1502573/


All Articles