Archived
1
0
Fork 0
forked from Moux23333/FreeSR
This repository has been archived on 2024-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
FreeSR/FreeSR.Database/DatabaseManager.cs
2024-01-27 21:06:07 +08:00

55 lines
1.6 KiB
C#

namespace FreeSR.Database
{
using FreeSR.Database.Configuration;
using FreeSR.Shared;
using MongoDB.Driver;
using NLog;
public sealed class DatabaseManager : Singleton<DatabaseManager>
{
private static readonly Logger s_log = LogManager.GetCurrentClassLogger();
private DatabaseConfiguration _configuration;
private Dictionary<Type, object> _databases;
public IMongoDatabase MongoDatabase { get; private set; }
private DatabaseManager()
{
_databases = new Dictionary<Type, object>();
}
public void Initialize(DatabaseConfiguration configuration)
{
_configuration = configuration;
var mongoClient = new MongoClient(configuration.ConnectionString);
MongoDatabase = mongoClient.GetDatabase(configuration.Name);
}
public string GetCollectionName(DatabaseType databaseType)
{
foreach (var entry in _configuration.Entries)
{
if (entry.Type == databaseType)
return entry.CollectionName;
}
throw new DatabaseMisconfiguredException($"Can not find database of type {databaseType} in provided configuration.");
}
public DatabaseManager Add<T>(ISRDatabase<T> database) where T : class
{
_databases.Add(database.GetType(), database);
return this;
}
public T Get<T>() where T : class
{
if (_databases.TryGetValue(typeof(T), out var database))
return database as T;
return null;
}
}
}