Skip to content

Commit

Permalink
Switch to default initialize the context (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
glucaci authored Nov 3, 2020
1 parent 20acbfc commit 862e22c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
42 changes: 20 additions & 22 deletions src/Context.Tests/MongoDbContextTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using MongoDB.Bson;
using MongoDB.Driver;
using Squadron;
Expand All @@ -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

Expand All @@ -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<InvalidOperationException>(
() => _ = 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<BsonDocument>();

// Assert
Assert.True(testMongoDbContext.IsInitialized);
Assert.Throws<InvalidOperationException>(
() => _ = testMongoDbContext.CreateCollection<BsonDocument>());
}

[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<InvalidOperationException>(
() => _ = 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
Expand Down
26 changes: 16 additions & 10 deletions src/Context/MongoDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -61,11 +59,19 @@ public IMongoCollection<TDocument> CreateCollection<TDocument>()

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)
{
Expand Down

0 comments on commit 862e22c

Please sign in to comment.