Class Database
- Namespace
- FishMMO.Database
- Assembly
- FishMMO-DB.dll
Main orchestrator and facade for the FishMMO database layer. Provides centralized access to database services, health monitoring, and metrics. This class initializes the database context factory, discovers and registers concrete service implementations, and exposes monitoring points to the host application.
public sealed class Database : IDatabase, IAsyncDisposable
- Inheritance
-
Database
- Implements
- Inherited Members
Constructors
Database(IConfiguration, bool, int?, int, int)
Initializes a new instance of the Database class with a pre-built IConfiguration. Creates the DbContext factory, discovers and registers all services, and sets up monitoring.
public Database(IConfiguration configuration, bool enableLogging = false, int? commandTimeout = null, int healthCheckWarningMs = 100, int healthCheckCriticalMs = 500)
Parameters
configurationIConfigurationApplication configuration containing an
Npgsqlsection. Cannot benull.enableLoggingboolEnable sensitive data logging for development (default: false).
commandTimeoutint?Optional database command timeout override in seconds.
healthCheckWarningMsintHealth check warning threshold in milliseconds (default: 100).
healthCheckCriticalMsintHealth check critical threshold in milliseconds (default: 500).
Exceptions
- ArgumentNullException
Thrown when
configurationisnull.- DatabaseException
Thrown when initialization fails due to configuration or registration issues.
Properties
DbContextFactory
Gets the database context factory. Exposed for direct access when needed (e.g., custom queries, migrations, raw SQL). In most cases, prefer using services from ServiceRegistry.
public INpgsqlDbContextFactory DbContextFactory { get; }
Property Value
Remarks
Exposes the configured INpgsqlDbContextFactory used to create short-lived NpgsqlDbContext instances. Prefer service interfaces from ServiceRegistry for application operations.
HealthMonitor
Gets the database health monitor for connectivity and performance checks. Use this to verify database availability and response times.
public DatabaseHealthMonitor HealthMonitor { get; }
Property Value
Remarks
The DatabaseHealthMonitor performs lightweight connectivity and response-time checks against the underlying database using INpgsqlDbContextFactory.
MetricsTracker
Gets the database metrics tracker for operation statistics. Provides insights into query performance and connection pool usage.
public DatabaseMetricsTracker MetricsTracker { get; }
Property Value
ServiceRegistry
Gets the service registry for accessing database services. Provides type-safe access to all registered database service interfaces.
public IDatabaseServiceRegistry ServiceRegistry { get; }
Property Value
Remarks
The service registry is populated during construction by discovering implementations
in the FishMMO.Database.Npgsql.Services assembly. Consumers should call
TryGet<TService>(out TService) to obtain services.
Methods
DisposeAsync()
Disposes the database orchestrator asynchronously. Delegates to ShutdownAsync(CancellationToken) for graceful cleanup.
public ValueTask DisposeAsync()
Returns
Remarks
NOTE: GC.SuppressFinalize is intentionally omitted because this class does not have a finalizer. Calling SuppressFinalize on a non-finalizable object is a no-op but adds unnecessary metadata. If a finalizer is ever added in the future, add GC.SuppressFinalize(this) back.
Shutdown()
Shuts down the database orchestrator. Unity-friendly synchronous shutdown (safe to call from main thread). After shutdown, new DbContext creation should be rejected.
public void Shutdown()
ShutdownAsync(CancellationToken)
Asynchronously shuts down the database orchestrator. After shutdown, new DbContext creation should be rejected.
public Task ShutdownAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationToken