mirror of https://github.com/abpframework/abp.git
7 changed files with 88 additions and 86 deletions
@ -0,0 +1,44 @@ |
|||||
|
using System.Collections.Concurrent; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using MongoDB.Driver; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.MongoDB.Clients; |
||||
|
|
||||
|
public class AbpMongoClientFactory : IAbpMongoClientFactory, ISingletonDependency |
||||
|
{ |
||||
|
protected ConcurrentDictionary<string, MongoClient> ClientCache { get; } |
||||
|
protected AbpMongoDbContextOptions Options { get; } |
||||
|
|
||||
|
public AbpMongoClientFactory(IOptions<AbpMongoDbContextOptions> options) |
||||
|
{ |
||||
|
Options = options.Value; |
||||
|
ClientCache = new ConcurrentDictionary<string, MongoClient>(); |
||||
|
} |
||||
|
|
||||
|
public virtual Task<MongoClient> GetAsync(MongoUrl mongoUrl) |
||||
|
{ |
||||
|
Check.NotNull(mongoUrl, nameof(mongoUrl)); |
||||
|
|
||||
|
return Task.FromResult( |
||||
|
ClientCache.GetOrAdd(mongoUrl.ToString(), _ => |
||||
|
{ |
||||
|
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl); |
||||
|
Options.MongoClientSettingsConfigurer?.Invoke(mongoClientSettings); |
||||
|
return new MongoClient(mongoClientSettings); |
||||
|
})); |
||||
|
} |
||||
|
|
||||
|
public virtual MongoClient Get(MongoUrl mongoUrl) |
||||
|
{ |
||||
|
Check.NotNull(mongoUrl, nameof(mongoUrl)); |
||||
|
|
||||
|
return ClientCache.GetOrAdd(mongoUrl.ToString(), _ => |
||||
|
{ |
||||
|
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl); |
||||
|
Options.MongoClientSettingsConfigurer?.Invoke(mongoClientSettings); |
||||
|
return new MongoClient(mongoClientSettings); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using MongoDB.Driver; |
||||
|
|
||||
|
namespace Volo.Abp.MongoDB.Clients; |
||||
|
|
||||
|
public interface IAbpMongoClientFactory |
||||
|
{ |
||||
|
Task<MongoClient> GetAsync(MongoUrl mongoUrl); |
||||
|
|
||||
|
[Obsolete("Use GetAsync method")] |
||||
|
MongoClient Get(MongoUrl mongoUrl); |
||||
|
} |
||||
@ -1,9 +0,0 @@ |
|||||
using MongoDB.Driver; |
|
||||
using Volo.Abp.DependencyInjection; |
|
||||
|
|
||||
namespace Volo.Abp.MongoDB.Clients; |
|
||||
|
|
||||
public interface IMongoClientFactory : ISingletonDependency |
|
||||
{ |
|
||||
MongoClient GetClient(string connectionString); |
|
||||
} |
|
||||
@ -1,32 +0,0 @@ |
|||||
using System; |
|
||||
using System.Collections.Concurrent; |
|
||||
using Microsoft.Extensions.Options; |
|
||||
using MongoDB.Driver; |
|
||||
|
|
||||
namespace Volo.Abp.MongoDB.Clients; |
|
||||
|
|
||||
public class MongoClientFactory : IMongoClientFactory |
|
||||
{ |
|
||||
private readonly ConcurrentDictionary<string, MongoClient> _clients = new(); |
|
||||
private readonly AbpMongoDbContextOptions Options; |
|
||||
|
|
||||
public MongoClientFactory(IOptions<AbpMongoDbContextOptions> options) |
|
||||
{ |
|
||||
Options = options.Value; |
|
||||
} |
|
||||
|
|
||||
public MongoClient GetClient(string connectionString) |
|
||||
{ |
|
||||
if (string.IsNullOrWhiteSpace(connectionString)) |
|
||||
{ |
|
||||
throw new ArgumentException("Connection string must not be null or empty.", nameof(connectionString)); |
|
||||
} |
|
||||
|
|
||||
return _clients.GetOrAdd(connectionString, cs => |
|
||||
{ |
|
||||
var mongoClientSettings = MongoClientSettings.FromUrl(new MongoUrl(cs)); |
|
||||
Options.MongoClientSettingsConfigurer?.Invoke(mongoClientSettings); |
|
||||
return new MongoClient(mongoClientSettings); |
|
||||
}); |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue