From b4c68c09426a7fb683d131dba93ea20ceb212124 Mon Sep 17 00:00:00 2001 From: tajoutihamza <46810740+tajoutihamza@users.noreply.github.com> Date: Fri, 9 Dec 2022 15:16:31 +0100 Subject: [PATCH 1/2] seeding data at the start of the service not at the context object creation --- .../Catalog/Catalog.API/Data/CatalogContext.cs | 2 -- .../Catalog/Catalog.API/Data/CatalogContextSeed.cs | 14 +++++++++++--- src/Services/Catalog/Catalog.API/Startup.cs | 1 + 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Services/Catalog/Catalog.API/Data/CatalogContext.cs b/src/Services/Catalog/Catalog.API/Data/CatalogContext.cs index 453ff733..9022c4e0 100644 --- a/src/Services/Catalog/Catalog.API/Data/CatalogContext.cs +++ b/src/Services/Catalog/Catalog.API/Data/CatalogContext.cs @@ -11,9 +11,7 @@ public CatalogContext(IConfiguration configuration) { var client = new MongoClient(configuration.GetValue("DatabaseSettings:ConnectionString")); var database = client.GetDatabase(configuration.GetValue("DatabaseSettings:DatabaseName")); - Products = database.GetCollection(configuration.GetValue("DatabaseSettings:CollectionName")); - CatalogContextSeed.SeedData(Products); } public IMongoCollection Products { get; } diff --git a/src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs b/src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs index d61b31e9..72aae3bd 100644 --- a/src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs +++ b/src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs @@ -7,12 +7,20 @@ namespace Catalog.API.Data { public class CatalogContextSeed { - public static void SeedData(IMongoCollection productCollection) + public static void PrepPopulation(IApplicationBuilder app) { - bool existProduct = productCollection.Find(p => true).Any(); + using (var serviceScope = app.ApplicationServices.CreateScope()) + { + SeedData(serviceScope.ServiceProvider.GetService()); + } + } + + public static void SeedData(ICatalogContext catalogContext) + { + bool existProduct = catalogContext.Products.Find(p => true).Any(); if (!existProduct) { - productCollection.InsertManyAsync(GetPreconfiguredProducts()); + catalogContext.Products.InsertManyAsync(GetPreconfiguredProducts()); } } diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index 236d75dd..e2a443a4 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -62,6 +62,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); }); + CatalogContextSeed.PrepPopulation(app); } } } From e72255bdf67fea3d01584a43dfecf48f2dafd036 Mon Sep 17 00:00:00 2001 From: tajoutihamza <46810740+tajoutihamza@users.noreply.github.com> Date: Fri, 9 Dec 2022 16:00:00 +0100 Subject: [PATCH 2/2] need to inject the interface to assure the dependancy injection --- src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs b/src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs index 72aae3bd..a986602d 100644 --- a/src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs +++ b/src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs @@ -11,7 +11,7 @@ public static void PrepPopulation(IApplicationBuilder app) { using (var serviceScope = app.ApplicationServices.CreateScope()) { - SeedData(serviceScope.ServiceProvider.GetService()); + SeedData(serviceScope.ServiceProvider.GetService()); } }