diff --git a/src/Context.Tests/MongoDbContextTests.cs b/src/Context.Tests/MongoDbContextTests.cs index 17b4bf1..e90024e 100644 --- a/src/Context.Tests/MongoDbContextTests.cs +++ b/src/Context.Tests/MongoDbContextTests.cs @@ -1,4 +1,5 @@ -using MongoDB.Driver; +using MongoDB.Bson; +using MongoDB.Driver; using Squadron; using Xunit; @@ -22,36 +23,76 @@ public MongoDbContextTests(MongoResource mongoResource) #region Constructor Tests [Fact] - public void Constructor_AutoInitializeDefault_InitializationExecuted() + public void Constructor_AutoInitializeManual_InitializationExecuted() + { + // Arrange + + // Act + var testMongoDbContext = new TestMongoDbContext(_mongoOptions, true); + + // Assert + Assert.True(testMongoDbContext.IsInitialized); + } + + [Fact] + public void Constructor_NoInitializeManual_InitializationExecuted() { // Arrange - + // Act + var testMongoDbContext = new TestMongoDbContext(_mongoOptions, false); + + // Assert + Assert.False(testMongoDbContext.IsInitialized); + } + + [Fact] + public void Constructor_Database_InitializationExecuted() + { + // Arrange var testMongoDbContext = new TestMongoDbContext(_mongoOptions); + // Act + _ = testMongoDbContext.Database; + // Assert Assert.True(testMongoDbContext.IsInitialized); } [Fact] - public void Constructor_AutoInitializeManual_InitializationExecuted() + public void Constructor_CreateCollection_InitializationExecuted() { // Arrange - + var testMongoDbContext = new TestMongoDbContext(_mongoOptions); + // Act - var testMongoDbContext = new TestMongoDbContext(_mongoOptions, true); + testMongoDbContext.CreateCollection(); // Assert Assert.True(testMongoDbContext.IsInitialized); } [Fact] - public void Constructor_NoInitializeManual_InitializationExecuted() + public void Constructor_Client_InitializationExecuted() { // Arrange + var testMongoDbContext = new TestMongoDbContext(_mongoOptions); // Act - var testMongoDbContext = new TestMongoDbContext(_mongoOptions, false); + _ = testMongoDbContext.Client; + + // Assert + Assert.True(testMongoDbContext.IsInitialized); + } + + [Fact] + public void Constructor_MongoOptions_InitializationNotExecuted() + { + // Arrange + var testMongoDbContext = new TestMongoDbContext(_mongoOptions); + + // Act + _ = testMongoDbContext.MongoOptions; // Assert Assert.False(testMongoDbContext.IsInitialized); @@ -67,7 +108,7 @@ public TestMongoDbContext(MongoOptions mongoOptions) : base(mongoOptions) { } - public TestMongoDbContext(MongoOptions mongoOptions, bool enableAutoInit) + public TestMongoDbContext(MongoOptions mongoOptions, bool enableAutoInit) : base(mongoOptions, enableAutoInit) { } diff --git a/src/Context/MongoDbContext.cs b/src/Context/MongoDbContext.cs index 1b59945..1e48f63 100644 --- a/src/Context/MongoDbContext.cs +++ b/src/Context/MongoDbContext.cs @@ -1,7 +1,6 @@ using System; using MongoDB.Driver; - namespace MongoDB.Extensions.Context { public abstract class MongoDbContext : IMongoDbContext @@ -10,10 +9,11 @@ public abstract class MongoDbContext : IMongoDbContext private readonly object _lockObject = new object(); - public MongoDbContext(MongoOptions mongoOptions) : this(mongoOptions, true) + public MongoDbContext(MongoOptions mongoOptions) : this(mongoOptions, false) { } + [Obsolete] public MongoDbContext(MongoOptions mongoOptions, bool enableAutoInitialize) { if (mongoOptions == null) @@ -23,23 +23,48 @@ public MongoDbContext(MongoOptions mongoOptions, bool enableAutoInitialize) MongoOptions = mongoOptions; - if(enableAutoInitialize) + // This initialization should be removed and switched to Lazy initialization. + if (enableAutoInitialize) { Initialize(mongoOptions); } } - public IMongoClient Client => _mongoDbContextData.Client; - public IMongoDatabase Database => _mongoDbContextData.Database; + public IMongoClient Client + { + get + { + EnsureInitialized(); + return _mongoDbContextData.Client; + } + } + + public IMongoDatabase Database + { + get + { + EnsureInitialized(); + return _mongoDbContextData.Database; + } + } + public MongoOptions MongoOptions { get; } - public IMongoCollection CreateCollection() where TDocument : class + public IMongoCollection CreateCollection() + where TDocument : class { + EnsureInitialized(); return _mongoDbContextData.CreateCollection(); } protected abstract void OnConfiguring(IMongoDatabaseBuilder mongoDatabaseBuilder); + private void EnsureInitialized() + { + Initialize(MongoOptions); + } + + [Obsolete] protected void Initialize(MongoOptions mongoOptions) { if(_mongoDbContextData == null) @@ -48,7 +73,7 @@ protected void Initialize(MongoOptions mongoOptions) { if (_mongoDbContextData == null) { - var mongoDatabaseBuilder = new MongoDatabaseBuilder(mongoOptions); + var mongoDatabaseBuilder = new MongoDatabaseBuilder(MongoOptions); OnConfiguring(mongoDatabaseBuilder);