From 862e22cd5e820d82c4fb73d4da99c107a1f46a06 Mon Sep 17 00:00:00 2001 From: Gabriel Lucaci Date: Tue, 3 Nov 2020 09:36:34 +0100 Subject: [PATCH] Switch to default initialize the context (#37) --- src/Context.Tests/MongoDbContextTests.cs | 42 +++++++++++------------- src/Context/MongoDbContext.cs | 26 +++++++++------ 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/Context.Tests/MongoDbContextTests.cs b/src/Context.Tests/MongoDbContextTests.cs index e90024e..93f55b6 100644 --- a/src/Context.Tests/MongoDbContextTests.cs +++ b/src/Context.Tests/MongoDbContextTests.cs @@ -1,3 +1,4 @@ +using System; using MongoDB.Bson; using MongoDB.Driver; using Squadron; @@ -23,19 +24,19 @@ public MongoDbContextTests(MongoResource mongoResource) #region Constructor Tests [Fact] - public void Constructor_AutoInitializeManual_InitializationExecuted() + public void Constructor_AutoInitialize_InitializationExecuted() { // Arrange // Act - var testMongoDbContext = new TestMongoDbContext(_mongoOptions, true); + var testMongoDbContext = new TestMongoDbContext(_mongoOptions); // Assert Assert.True(testMongoDbContext.IsInitialized); } [Fact] - public void Constructor_NoInitializeManual_InitializationExecuted() + public void Constructor_NoInitialize_InitializationNotExecuted() { // Arrange @@ -47,55 +48,52 @@ public void Constructor_NoInitializeManual_InitializationExecuted() } [Fact] - public void Constructor_Database_InitializationExecuted() + public void Constructor_Database_ThrowsWhenNoInitialize() { // Arrange - var testMongoDbContext = new TestMongoDbContext(_mongoOptions); + var testMongoDbContext = new TestMongoDbContext(_mongoOptions, false); // Act - _ = testMongoDbContext.Database; - // Assert - Assert.True(testMongoDbContext.IsInitialized); + Assert.Throws( + () => _ = testMongoDbContext.Database); } [Fact] - public void Constructor_CreateCollection_InitializationExecuted() + public void Constructor_CreateCollection_ThrowsWhenNotInitialize() { // Arrange - var testMongoDbContext = new TestMongoDbContext(_mongoOptions); + var testMongoDbContext = new TestMongoDbContext(_mongoOptions, false); // Act - testMongoDbContext.CreateCollection(); - // Assert - Assert.True(testMongoDbContext.IsInitialized); + Assert.Throws( + () => _ = testMongoDbContext.CreateCollection()); } [Fact] - public void Constructor_Client_InitializationExecuted() + public void Constructor_Client_ThrowsWhenNoInitialize() { // Arrange - var testMongoDbContext = new TestMongoDbContext(_mongoOptions); + var testMongoDbContext = new TestMongoDbContext(_mongoOptions, false); // Act - _ = testMongoDbContext.Client; - // Assert - Assert.True(testMongoDbContext.IsInitialized); + Assert.Throws( + () => _ = testMongoDbContext.Client); } [Fact] - public void Constructor_MongoOptions_InitializationNotExecuted() + public void Constructor_MongoOptions_CanAccessWhenNotInitialize() { // Arrange - var testMongoDbContext = new TestMongoDbContext(_mongoOptions); + var testMongoDbContext = new TestMongoDbContext(_mongoOptions, false); // Act - _ = testMongoDbContext.MongoOptions; + MongoOptions mongoOptions = testMongoDbContext.MongoOptions; // Assert - Assert.False(testMongoDbContext.IsInitialized); + Assert.NotNull(mongoOptions); } #endregion diff --git a/src/Context/MongoDbContext.cs b/src/Context/MongoDbContext.cs index 1e48f63..a7272de 100644 --- a/src/Context/MongoDbContext.cs +++ b/src/Context/MongoDbContext.cs @@ -9,24 +9,22 @@ public abstract class MongoDbContext : IMongoDbContext private readonly object _lockObject = new object(); - public MongoDbContext(MongoOptions mongoOptions) : this(mongoOptions, false) + public MongoDbContext(MongoOptions mongoOptions) : this(mongoOptions, true) { } - [Obsolete] public MongoDbContext(MongoOptions mongoOptions, bool enableAutoInitialize) { if (mongoOptions == null) + { throw new ArgumentNullException(nameof(mongoOptions)); + } - mongoOptions.Validate(); - - MongoOptions = mongoOptions; + MongoOptions = mongoOptions.Validate(); - // This initialization should be removed and switched to Lazy initialization. if (enableAutoInitialize) { - Initialize(mongoOptions); + Initialize(); } } @@ -61,11 +59,19 @@ public IMongoCollection CreateCollection() private void EnsureInitialized() { - Initialize(MongoOptions); + if (_mongoDbContextData == null) + { + lock (_lockObject) + { + if (_mongoDbContextData == null) + { + throw new InvalidOperationException("MongoDbContext not initialized."); + } + } + } } - [Obsolete] - protected void Initialize(MongoOptions mongoOptions) + public virtual void Initialize() { if(_mongoDbContextData == null) {