Skip to content

Commit

Permalink
MongoDbContext auto initialization configurable. (#21)
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel Lucaci <[email protected]>
  • Loading branch information
nscheibe and glucaci authored Mar 18, 2020
1 parent eca5634 commit a68972b
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 10 deletions.
85 changes: 85 additions & 0 deletions src/Context.Tests/MongoDbContextTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using MongoDB.Driver;
using Squadron;
using Xunit;

namespace MongoDB.Extensions.Context.Tests
{
public class MongoDbContextTests : IClassFixture<MongoResource>
{
private readonly MongoOptions _mongoOptions;
private readonly IMongoDatabase _mongoDatabase;

public MongoDbContextTests(MongoResource mongoResource)
{
_mongoDatabase = mongoResource.CreateDatabase();
_mongoOptions = new MongoOptions
{
ConnectionString = mongoResource.ConnectionString,
DatabaseName = _mongoDatabase.DatabaseNamespace.DatabaseName
};
}

#region Constructor Tests

[Fact]
public void Constructor_AutoInitializeDefault_InitializationExecuted()
{
// Arrange

// Act
var testMongoDbContext = new TestMongoDbContext(_mongoOptions);

// Assert
Assert.True(testMongoDbContext.IsInitialized);
}

[Fact]
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);
}

#endregion

#region Private Helpers

private class TestMongoDbContext : MongoDbContext
{
public TestMongoDbContext(MongoOptions mongoOptions) : base(mongoOptions)
{
}

public TestMongoDbContext(MongoOptions mongoOptions, bool enableAutoInit)
: base(mongoOptions, enableAutoInit)
{
}

protected override void OnConfiguring(IMongoDatabaseBuilder mongoDatabaseBuilder)
{
IsInitialized = true;
}

public bool IsInitialized { get; private set; }
}

#endregion
}
}
38 changes: 28 additions & 10 deletions src/Context/MongoDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ namespace MongoDB.Extensions.Context
{
public abstract class MongoDbContext : IMongoDbContext
{
private readonly MongoDbContextData _mongoDbContextData;
private MongoDbContextData _mongoDbContextData;

public MongoDbContext(MongoOptions mongoOptions)
private readonly object _lockObject = new object();

public MongoDbContext(MongoOptions mongoOptions) : this(mongoOptions, true)
{
}

public MongoDbContext(MongoOptions mongoOptions, bool enableAutoInitialize)
{
if(mongoOptions == null)
if (mongoOptions == null)
throw new ArgumentNullException(nameof(mongoOptions));

mongoOptions.Validate();

MongoOptions = mongoOptions;

_mongoDbContextData = Initialize(mongoOptions);
if(enableAutoInitialize)
{
Initialize(mongoOptions);
}
}

public IMongoClient Client => _mongoDbContextData.Client;
Expand All @@ -31,13 +40,22 @@ public IMongoCollection<TDocument> CreateCollection<TDocument>() where TDocument

protected abstract void OnConfiguring(IMongoDatabaseBuilder mongoDatabaseBuilder);

private MongoDbContextData Initialize(MongoOptions mongoOptions)
protected void Initialize(MongoOptions mongoOptions)
{
var mongoDatabaseBuilder = new MongoDatabaseBuilder(mongoOptions);

OnConfiguring(mongoDatabaseBuilder);

return mongoDatabaseBuilder.Build();
if(_mongoDbContextData == null)
{
lock (_lockObject)
{
if (_mongoDbContextData == null)
{
var mongoDatabaseBuilder = new MongoDatabaseBuilder(mongoOptions);

OnConfiguring(mongoDatabaseBuilder);

_mongoDbContextData = mongoDatabaseBuilder.Build();
}
}
}
}
}
}

0 comments on commit a68972b

Please sign in to comment.