Skip to content

Commit

Permalink
MongoDbContext lazy initialization (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
glucaci authored Oct 19, 2020
1 parent 44658df commit fdac807
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 16 deletions.
59 changes: 50 additions & 9 deletions src/Context.Tests/MongoDbContextTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver;
using Squadron;
using Xunit;

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

// 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);
Expand All @@ -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)
{
}
Expand Down
39 changes: 32 additions & 7 deletions src/Context/MongoDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using MongoDB.Driver;


namespace MongoDB.Extensions.Context
{
public abstract class MongoDbContext : IMongoDbContext
Expand All @@ -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)
Expand All @@ -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<TDocument> CreateCollection<TDocument>() where TDocument : class
public IMongoCollection<TDocument> CreateCollection<TDocument>()
where TDocument : class
{
EnsureInitialized();
return _mongoDbContextData.CreateCollection<TDocument>();
}

protected abstract void OnConfiguring(IMongoDatabaseBuilder mongoDatabaseBuilder);

private void EnsureInitialized()
{
Initialize(MongoOptions);
}

[Obsolete]
protected void Initialize(MongoOptions mongoOptions)
{
if(_mongoDbContextData == null)
Expand All @@ -48,7 +73,7 @@ protected void Initialize(MongoOptions mongoOptions)
{
if (_mongoDbContextData == null)
{
var mongoDatabaseBuilder = new MongoDatabaseBuilder(mongoOptions);
var mongoDatabaseBuilder = new MongoDatabaseBuilder(MongoOptions);

OnConfiguring(mongoDatabaseBuilder);

Expand Down

0 comments on commit fdac807

Please sign in to comment.