From 901a1128e9ef8ebc86ed51f5957f66a083c767ca Mon Sep 17 00:00:00 2001 From: "alexis.drai" Date: Thu, 6 Oct 2022 10:36:54 +0200 Subject: [PATCH] :adhesive_bandage: Try-catch db connectionsin prototype --- Sources/App/Program.cs | 77 +++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/Sources/App/Program.cs b/Sources/App/Program.cs index 785dac0..5f4cff8 100644 --- a/Sources/App/Program.cs +++ b/Sources/App/Program.cs @@ -31,23 +31,26 @@ namespace App gameRunner = new(new PlayerManager(), new DieManager(), null); } - // DB stuff when the app opens - using (DiceAppDbContext db = new()) - { - // Later, we'll use the DiceAppDbContext to get a GameDbRunner - - // get all the players from the DB - IEnumerable entities = db.Players; - - foreach (PlayerEntity entity in entities) - { - try // to persist them - { // as models ! - gameRunner.GlobalPlayerManager.Add(entity.ToModel()); + try + { + // DB stuff when the app opens + using (DiceAppDbContext db = new()) + { + // Later, we'll use the DiceAppDbContext to get a GameDbRunner + + // get all the players from the DB + IEnumerable entities = db.Players; + + Debug.WriteLine("Loading players"); + + foreach (PlayerEntity entity in entities) + { + // persist them as models ! + gameRunner.GlobalPlayerManager.Add(entity.ToModel()); } - catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Did you make sure that the DATABASE exists?"); } } - } + } + catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Couldn't use the database"); } string menuChoice = "nothing"; @@ -146,26 +149,30 @@ namespace App } } - // DB stuff when the app closes - using (DiceAppDbContext db = new()) - { - // get all the players from the app's memory - IEnumerable models = gameRunner.GlobalPlayerManager.GetAll(); - - // create a PlayerDbManager (and inject it with the DB) - PlayerDbManager playerDbManager = new(db); - - foreach (Player model in models) - { - try // to persist them - { // as entities ! - playerDbManager.Add(model.ToEntity()); - } - // what if there's already a player with that name? Exception (see PlayerEntity's annotations) - catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); } - catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Did you make sure that the DATABASE exists?"); } - } - } + try + { + // DB stuff when the app closes + using (DiceAppDbContext db = new()) + { + // get all the players from the app's memory + IEnumerable models = gameRunner.GlobalPlayerManager.GetAll(); + + // create a PlayerDbManager (and inject it with the DB) + PlayerDbManager playerDbManager = new(db); + + Debug.WriteLine("Saving players"); + + foreach (Player model in models) + { + try // to persist them + { // as entities ! + playerDbManager.Add(model.ToEntity()); + } + // what if there's already a player with that name? Exception (see PlayerEntity's annotations) + catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); } + } + } + } catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Couldn't use the database"); } } private static void Play(GameRunner gameRunner, string name)