초기커밋
This commit is contained in:
84
ServerCore/MongoDB/MongoDbConnectorBase.cs
Normal file
84
ServerCore/MongoDB/MongoDbConnectorBase.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Driver;
|
||||
|
||||
|
||||
using MONGO_DB_LIST = System.String;
|
||||
|
||||
|
||||
namespace ServerCore;
|
||||
|
||||
|
||||
public abstract class MongoDbConnectorBase : IDisposable
|
||||
{
|
||||
private IMongoClient? m_mongo_client;
|
||||
|
||||
public MongoDbConnectorBase()
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<(bool, List<MONGO_DB_LIST>)> initAndVerifyDb( string connectionString
|
||||
, int minConnectionPoolSize, int maxConnectionPoolSize
|
||||
, int waitQueueTimeoutSecs )
|
||||
{
|
||||
var err_msg = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
if (true == connectionString.isNullOrWhiteSpace())
|
||||
{
|
||||
throw new ArgumentException("Connection string is null or empty.", nameof(connectionString));
|
||||
}
|
||||
|
||||
if (minConnectionPoolSize < 0 || maxConnectionPoolSize <= 0 || minConnectionPoolSize > maxConnectionPoolSize)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("Invalid pool size configuration.");
|
||||
}
|
||||
|
||||
if (waitQueueTimeoutSecs < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(waitQueueTimeoutSecs), "Wait queue timeout must be non-negative.");
|
||||
}
|
||||
|
||||
var settings = MongoClientSettings.FromConnectionString(connectionString);
|
||||
settings.MinConnectionPoolSize = minConnectionPoolSize;
|
||||
settings.MaxConnectionPoolSize = maxConnectionPoolSize;
|
||||
settings.WaitQueueTimeout = TimeSpan.FromSeconds(waitQueueTimeoutSecs);
|
||||
|
||||
var mongo_client = new MongoClient(settings);
|
||||
|
||||
var cursor = await mongo_client.ListDatabaseNamesAsync();
|
||||
var db_list = await cursor.ToListAsync();
|
||||
|
||||
m_mongo_client = mongo_client;
|
||||
|
||||
return (true, db_list);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.getLogger().error($"Exception !!!, Failed to perform in MongoDbConnectorBase.initAndVerifyDb() !!! : exception:{e} - connectionString:{connectionString}");
|
||||
}
|
||||
|
||||
return (false, new List<MONGO_DB_LIST>());
|
||||
}
|
||||
|
||||
public IMongoClient getMongoClient()
|
||||
{
|
||||
NullReferenceCheckHelper.throwIfNull(m_mongo_client, () => $"m_mongo_client is null !!!");
|
||||
return m_mongo_client;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
m_mongo_client = null;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public void disposeMongoDb()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
19
ServerCore/MongoDB/MongoDbRepository.cs
Normal file
19
ServerCore/MongoDB/MongoDbRepository.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using MongoDB.Driver;
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
|
||||
public class MongoDbRepository<TCollection>
|
||||
where TCollection : class
|
||||
{
|
||||
protected IMongoCollection<TCollection> m_collection;
|
||||
|
||||
protected MongoDbRepository(IMongoClient client, string database, string collection)
|
||||
{
|
||||
var mongoDb = client.GetDatabase(database);
|
||||
m_collection = mongoDb.GetCollection<TCollection>(collection);
|
||||
}
|
||||
|
||||
public IMongoCollection<TCollection> getCollection() => m_collection;
|
||||
}
|
||||
Reference in New Issue
Block a user