Browse Source

Merge pull request #9774 from abpframework/liangshiwei/mongodb

Make MongoClientSettings configurable
pull/9782/head
maliming 5 years ago
committed by GitHub
parent
commit
144d9d4e65
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContextOptions.cs
  2. 14
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDbContextProvider.cs

10
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContextOptions.cs

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Driver;
namespace Volo.Abp.MongoDB namespace Volo.Abp.MongoDB
{ {
@ -7,11 +9,13 @@ namespace Volo.Abp.MongoDB
{ {
internal Dictionary<Type, Type> DbContextReplacements { get; } internal Dictionary<Type, Type> DbContextReplacements { get; }
public Action<MongoClientSettings> MongoClientSettingsConfigurer { get; set; }
public AbpMongoDbContextOptions() public AbpMongoDbContextOptions()
{ {
DbContextReplacements = new Dictionary<Type, Type>(); DbContextReplacements = new Dictionary<Type, Type>();
} }
internal Type GetReplacedTypeOrSelf(Type dbContextType) internal Type GetReplacedTypeOrSelf(Type dbContextType)
{ {
var replacementType = dbContextType; var replacementType = dbContextType;
@ -26,7 +30,7 @@ namespace Volo.Abp.MongoDB
dbContextType.AssemblyQualifiedName dbContextType.AssemblyQualifiedName
); );
} }
replacementType = foundType; replacementType = foundType;
} }
else else
@ -36,4 +40,4 @@ namespace Volo.Abp.MongoDB
} }
} }
} }
} }

14
framework/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDbContextProvider.cs

@ -29,7 +29,7 @@ namespace Volo.Abp.Uow.MongoDB
IUnitOfWorkManager unitOfWorkManager, IUnitOfWorkManager unitOfWorkManager,
IConnectionStringResolver connectionStringResolver, IConnectionStringResolver connectionStringResolver,
ICancellationTokenProvider cancellationTokenProvider, ICancellationTokenProvider cancellationTokenProvider,
ICurrentTenant currentTenant, ICurrentTenant currentTenant,
IOptions<AbpMongoDbContextOptions> options) IOptions<AbpMongoDbContextOptions> options)
{ {
_unitOfWorkManager = unitOfWorkManager; _unitOfWorkManager = unitOfWorkManager;
@ -124,7 +124,7 @@ namespace Volo.Abp.Uow.MongoDB
private TMongoDbContext CreateDbContext(IUnitOfWork unitOfWork, MongoUrl mongoUrl, string databaseName) private TMongoDbContext CreateDbContext(IUnitOfWork unitOfWork, MongoUrl mongoUrl, string databaseName)
{ {
var client = new MongoClient(mongoUrl); var client = CreateMongoClient(mongoUrl);
var database = client.GetDatabase(databaseName); var database = client.GetDatabase(databaseName);
if (unitOfWork.Options.IsTransactional) if (unitOfWork.Options.IsTransactional)
@ -144,7 +144,7 @@ namespace Volo.Abp.Uow.MongoDB
string databaseName, string databaseName,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var client = new MongoClient(mongoUrl); var client = CreateMongoClient(mongoUrl);
var database = client.GetDatabase(databaseName); var database = client.GetDatabase(databaseName);
if (unitOfWork.Options.IsTransactional) if (unitOfWork.Options.IsTransactional)
@ -273,6 +273,14 @@ namespace Volo.Abp.Uow.MongoDB
return _connectionStringResolver.Resolve(dbContextType); return _connectionStringResolver.Resolve(dbContextType);
} }
private MongoClient CreateMongoClient(MongoUrl mongoUrl)
{
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl);
_options.MongoClientSettingsConfigurer?.Invoke(mongoClientSettings);
return new MongoClient(mongoClientSettings);
}
protected virtual CancellationToken GetCancellationToken(CancellationToken preferredValue = default) protected virtual CancellationToken GetCancellationToken(CancellationToken preferredValue = default)
{ {
return _cancellationTokenProvider.FallbackToProvider(preferredValue); return _cancellationTokenProvider.FallbackToProvider(preferredValue);

Loading…
Cancel
Save