Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Share a single MongoClient and fix LiteDB TableCollection (#757)
Krzysztof Pajak3199d91cb4f9
diff --git a/src/Core/Grand.Data/LiteDb/LiteDBRepository.cs b/src/Core/Grand.Data/LiteDb/LiteDBRepository.csindex a28bd3098..2811e81db 100644--- a/src/Core/Grand.Data/LiteDb/LiteDBRepository.cs+++ b/src/Core/Grand.Data/LiteDb/LiteDBRepository.cs@@ -413,7 +413,8 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity/// </summary>public virtual IQueryable<C> TableCollection<C>() where C : class{- return Database.GetCollection<C>(nameof(T)).Query().ToEnumerable().AsQueryable();+ //typeof(T).Name, not nameof(T) - the latter is the literal "T"+ return Database.GetCollection<C>(typeof(T).Name).Query().ToEnumerable().AsQueryable();}#endregiondiff --git a/src/Core/Grand.Infrastructure/Startup/StartupApplication.cs b/src/Core/Grand.Infrastructure/Startup/StartupApplication.csindex 7e179686a..8fc295fa6 100644--- a/src/Core/Grand.Infrastructure/Startup/StartupApplication.cs+++ b/src/Core/Grand.Infrastructure/Startup/StartupApplication.cs@@ -51,7 +51,10 @@ public class StartupApplication : IStartupApplicationvar mongoUrl = new MongoUrl(connectionString);var databaseName = mongoUrl.DatabaseName;var clientSettings = MongoClientSettings.FromConnectionString(connectionString);- serviceCollection.AddScoped(_ => new MongoClient(clientSettings).GetDatabase(databaseName));+ //the driver expects a single client per connection string - it owns the connection+ //pool and the cluster monitoring, and is thread-safe+ serviceCollection.AddSingleton<IMongoClient>(_ => new MongoClient(clientSettings));+ serviceCollection.AddScoped(sp => sp.GetRequiredService<IMongoClient>().GetDatabase(databaseName));}else{diff --git a/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs b/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.csindex cd823d34d..1277532bc 100644--- a/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs+++ b/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs@@ -552,4 +552,18 @@ public class LiteDbRepositoryTestsAssert.IsTrue(p.UpdatedOnUtc.HasValue);Assert.AreEqual("user", p.UpdatedBy);}++ [TestMethod]+ public void TableCollection_ReadsTheEntityCollection()+ {+ var myRepository = new LiteDBRepositoryMock<SampleCollection>();+ myRepository.Insert(new SampleCollection { Id = "1", Name = "Test" });+ myRepository.Insert(new SampleCollection { Id = "2", Name = "Test2" });++ //reads the collection named after the entity, not one named "T"+ var result = myRepository.TableCollection<SampleCollection>().ToList();++ Assert.AreEqual(2, result.Count);+ Assert.IsNotNull(result.FirstOrDefault(x => x.Id == "1"));+ }}\ No newline at end of file