Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support to pass MongoClientSettings as a parameter of Creat… #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/Arc4u.Standard.MongoDB/DefaultMongoClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public DefaultMongoClientFactory(IOptionsMonitor<MongoClientSettings> clientSett
_mongoContext = (TContext)serviceProvider.GetService(typeof(TContext));
}

IMongoDatabase _database;
IMongoClient _client;
private IMongoDatabase _database;
private IMongoClient _client;
private static object _locker = new object();
readonly IOptionsMonitor<MongoClientSettings> _clientSettings;
readonly TContext _mongoContext;
private readonly IOptionsMonitor<MongoClientSettings> _clientSettings;
private readonly TContext _mongoContext;

public IMongoClient CreateClient()
{
Expand All @@ -35,13 +35,24 @@ public IMongoClient CreateClient()
{
_client = new MongoClient(settings);
return _client;

}

throw new NullReferenceException($"No mongo client settings defined for key {_mongoContext.DatabaseName}");
}
}

public IMongoClient CreateClient(MongoClientSettings mongoClientSettings)
{
if (null == mongoClientSettings) throw new NullReferenceException($"No mongo client settings defined for key {_mongoContext.DatabaseName}");

lock (_locker)
{
if (null != _client)
return _client;

_client = new MongoClient(mongoClientSettings);
return _client;
}
}

private IMongoDatabase GetDatabase()
Expand Down Expand Up @@ -82,7 +93,6 @@ public IMongoCollection<TEntity> GetCollection<TEntity>()
var db = GetDatabase();

return db.GetCollection<TEntity>(collectionNames[0]);

}
}
}
}
5 changes: 3 additions & 2 deletions src/Arc4u.Standard.MongoDB/IMongoClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ public interface IMongoClientFactory<TContext> where TContext : DbContext
{
IMongoClient CreateClient();

IMongoClient CreateClient(MongoClientSettings mongoClientSettings);

IMongoCollection<TEntity> GetCollection<TEntity>(string collectionName);

IMongoCollection<TEntity> GetCollection<TEntity>();
}

}
}