Remove the typeparam of DatabaseContext

- Also rename the design-time helper function.
This commit is contained in:
Jordan Brown
2020-05-27 17:39:18 -04:00
parent 47d1e94659
commit c4be9a8ef8
13 changed files with 57 additions and 58 deletions
@@ -4,7 +4,7 @@ using Newtonsoft.Json.Converters;
namespace Tgstation.Server.Host.Configuration
{
/// <summary>
/// Configuration options for the <see cref="Database.DatabaseContext{TParentContext}"/>
/// Configuration options for the <see cref="Database.DatabaseContext"/>
/// </summary>
sealed class DatabaseConfiguration
{
@@ -202,7 +202,7 @@ namespace Tgstation.Server.Host.Core
// CORS conditionally enabled later
services.AddCors();
void AddTypedContext<TContext>() where TContext : DatabaseContext<TContext>
void AddTypedContext<TContext>() where TContext : DatabaseContext
{
services.AddDbContext<TContext>(builder =>
{
@@ -18,95 +18,94 @@ namespace Tgstation.Server.Host.Database
/// <summary>
/// Backend abstract implementation of <see cref="IDatabaseContext"/>
/// </summary>
/// <typeparam name="TParentContext">The child <see cref="Type"/> used to implement a backend.</typeparam>
#pragma warning disable CA1506 // TODO: Decomplexify
abstract class DatabaseContext<TParentContext> : DbContext, IDatabaseContext where TParentContext : DbContext
abstract class DatabaseContext : DbContext, IDatabaseContext
{
/// <inheritdoc />
public DatabaseType DatabaseType => DatabaseConfiguration.DatabaseType;
/// <summary>
/// The <see cref="User"/>s in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="User"/>s in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<User> Users { get; set; }
/// <summary>
/// The <see cref="Instance"/>s in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="Instance"/>s in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<Instance> Instances { get; set; }
/// <summary>
/// The <see cref="CompileJob"/>s in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="CompileJob"/>s in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<CompileJob> CompileJobs { get; set; }
/// <summary>
/// The <see cref="RevisionInformation"/>s in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="RevisionInformation"/>s in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<RevisionInformation> RevisionInformations { get; set; }
/// <summary>
/// The <see cref="Models.DreamMakerSettings"/> in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="Models.DreamMakerSettings"/> in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<DreamMakerSettings> DreamMakerSettings { get; set; }
/// <summary>
/// The <see cref="ChatBot"/>s in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="ChatBot"/>s in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<ChatBot> ChatBots { get; set; }
/// <summary>
/// The <see cref="Models.DreamDaemonSettings"/> in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="Models.DreamDaemonSettings"/> in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<DreamDaemonSettings> DreamDaemonSettings { get; set; }
/// <summary>
/// The <see cref="Models.RepositorySettings"/> in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="Models.RepositorySettings"/> in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<RepositorySettings> RepositorySettings { get; set; }
/// <summary>
/// The <see cref="InstanceUser"/>s in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="InstanceUser"/>s in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<InstanceUser> InstanceUsers { get; set; }
/// <summary>
/// The <see cref="ChatChannel"/>s in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="ChatChannel"/>s in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<ChatChannel> ChatChannels { get; set; }
/// <summary>
/// The <see cref="Job"/>s in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="Job"/>s in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<Job> Jobs { get; set; }
/// <summary>
/// The <see cref="ReattachInformation"/>s in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="ReattachInformation"/>s in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<ReattachInformation> ReattachInformations { get; set; }
/// <summary>
/// The <see cref="DualReattachInformation"/>s in the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="DualReattachInformation"/>s in the <see cref="DatabaseContext"/>.
/// </summary>
public DbSet<DualReattachInformation> WatchdogReattachInformations { get; set; }
/// <summary>
/// The <see cref="TestMerge"/>s in the <see cref="DatabaseContext{TParentContext}"/>
/// The <see cref="TestMerge"/>s in the <see cref="DatabaseContext"/>
/// </summary>
public DbSet<TestMerge> TestMerges { get; set; }
/// <summary>
/// The <see cref="RevInfoTestMerge"/>s om the <see cref="DatabaseContext{TParentContext}"/>
/// The <see cref="RevInfoTestMerge"/>s om the <see cref="DatabaseContext"/>
/// </summary>
public DbSet<RevInfoTestMerge> RevInfoTestMerges { get; set; }
/// <summary>
/// The <see cref="ILogger"/> for the <see cref="DatabaseContext{TParentContext}"/>
/// The <see cref="ILogger"/> for the <see cref="DatabaseContext"/>
/// </summary>
protected ILogger Logger { get; }
/// <summary>
/// The <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext{TParentContext}"/>
/// The <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext"/>
/// </summary>
protected DatabaseConfiguration DatabaseConfiguration { get; }
@@ -150,7 +149,7 @@ namespace Tgstation.Server.Host.Database
IDatabaseCollection<DualReattachInformation> IDatabaseContext.WatchdogReattachInformations => watchdogReattachInformationsCollection;
/// <summary>
/// The <see cref="IDatabaseSeeder"/> for the <see cref="DatabaseContext{TParentContext}"/>
/// The <see cref="IDatabaseSeeder"/> for the <see cref="DatabaseContext"/>
/// </summary>
readonly IDatabaseSeeder databaseSeeder;
@@ -220,13 +219,13 @@ namespace Tgstation.Server.Host.Database
readonly IDatabaseCollection<DualReattachInformation> watchdogReattachInformationsCollection;
/// <summary>
/// Construct a <see cref="DatabaseContext{TParentContext}"/>
/// Construct a <see cref="DatabaseContext"/>
/// </summary>
/// <param name="dbContextOptions">The <see cref="DbContextOptions{TParentContext}"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="dbContextOptions">The <see cref="DbContextOptions"/> for the <see cref="DatabaseContext"/>.</param>
/// <param name="databaseConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="DatabaseConfiguration"/></param>
/// <param name="databaseSeeder">The value of <see cref="databaseSeeder"/></param>
/// <param name="logger">The value of <see cref="Logger"/></param>
public DatabaseContext(DbContextOptions<TParentContext> dbContextOptions, IOptions<DatabaseConfiguration> databaseConfigurationOptions, IDatabaseSeeder databaseSeeder, ILogger logger) : base(dbContextOptions)
public DatabaseContext(DbContextOptions dbContextOptions, IOptions<DatabaseConfiguration> databaseConfigurationOptions, IDatabaseSeeder databaseSeeder, ILogger logger) : base(dbContextOptions)
{
DatabaseConfiguration = databaseConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(databaseConfigurationOptions));
this.databaseSeeder = databaseSeeder ?? throw new ArgumentNullException(nameof(databaseSeeder));
@@ -410,7 +409,7 @@ namespace Tgstation.Server.Host.Database
}
/// <summary>
/// Ensure the <see cref="DatabaseType"/> is correct for the <see cref="DatabaseContext{TParentContext}"/>.
/// Ensure the <see cref="DatabaseType"/> is correct for the <see cref="DatabaseContext"/>.
/// </summary>
protected abstract void ValidateDatabaseType();
}
@@ -14,12 +14,12 @@ namespace Tgstation.Server.Host.Database
sealed class DatabaseSeeder : IDatabaseSeeder
{
/// <summary>
/// The <see cref="ICryptographySuite"/> for the <see cref="DatabaseContext{TParentContext}"/>
/// The <see cref="ICryptographySuite"/> for the <see cref="DatabaseSeeder"/>
/// </summary>
readonly ICryptographySuite cryptographySuite;
/// <summary>
/// The <see cref="IPlatformIdentifier"/> for the <see cref="DatabaseContext{TParentContext}"/>.
/// The <see cref="IPlatformIdentifier"/> for the <see cref="DatabaseSeeder"/>.
/// </summary>
readonly IPlatformIdentifier platformIdentifier;
@@ -4,7 +4,7 @@ using Tgstation.Server.Host.Configuration;
namespace Tgstation.Server.Host.Database.Design
{
/// <summary>
/// Contains helpers for creating design time <see cref="DatabaseContext{TParentContext}"/>s
/// Contains helpers for creating design time <see cref="DatabaseContext"/>s
/// </summary>
static class DesignTimeDbContextFactoryHelpers
{
@@ -14,7 +14,7 @@ namespace Tgstation.Server.Host.Database.Design
/// <param name="databaseType">The <see cref="DatabaseConfiguration.DatabaseType"/>.</param>
/// <param name="connectionString">The <see cref="DatabaseConfiguration.ConnectionString"/>.</param>
/// <returns>The <see cref="IOptions{TOptions}"/> for the <see cref="DatabaseConfiguration"/></returns>
public static IOptions<DatabaseConfiguration> GetDbContextOptions(DatabaseType databaseType, string connectionString)
public static IOptions<DatabaseConfiguration> GetDatabaseConfiguration(DatabaseType databaseType, string connectionString)
{
var dbConfig = new DatabaseConfiguration
{
@@ -20,7 +20,7 @@ namespace Tgstation.Server.Host.Database.Design
using var loggerFactory = new LoggerFactory();
return new MySqlDatabaseContext(
new DbContextOptions<MySqlDatabaseContext>(),
DesignTimeDbContextFactoryHelpers.GetDbContextOptions(
DesignTimeDbContextFactoryHelpers.GetDatabaseConfiguration(
DatabaseType.MariaDB,
"Server=127.0.0.1;User Id=root;Password=fake;Database=TGS_Design"),
new DatabaseSeeder(
@@ -18,7 +18,7 @@ namespace Tgstation.Server.Host.Database.Design
using var loggerFactory = new LoggerFactory();
return new PostgresSqlDatabaseContext(
new DbContextOptions<PostgresSqlDatabaseContext>(),
DesignTimeDbContextFactoryHelpers.GetDbContextOptions(
DesignTimeDbContextFactoryHelpers.GetDatabaseConfiguration(
DatabaseType.PostgresSql,
"Application Name=tgstation-server;Host=127.0.0.1;Password=qCkWimNgLfWwpr7TnUHs;Username=postgres;Database=TGS_Design"),
new DatabaseSeeder(
@@ -20,7 +20,7 @@ namespace Tgstation.Server.Host.Database.Design
using var loggerFactory = new LoggerFactory();
return new SqlServerDatabaseContext(
new DbContextOptions<SqlServerDatabaseContext>(),
DesignTimeDbContextFactoryHelpers.GetDbContextOptions(
DesignTimeDbContextFactoryHelpers.GetDatabaseConfiguration(
DatabaseType.SqlServer,
"Data Source=fake;Initial Catalog=TGS_Design;Integrated Security=True;Application Name=tgstation-server"),
new DatabaseSeeder(
@@ -20,7 +20,7 @@ namespace Tgstation.Server.Host.Database.Design
using var loggerFactory = new LoggerFactory();
return new SqliteDatabaseContext(
new DbContextOptions<SqliteDatabaseContext>(),
DesignTimeDbContextFactoryHelpers.GetDbContextOptions(
DesignTimeDbContextFactoryHelpers.GetDatabaseConfiguration(
DatabaseType.Sqlite,
"Data Source=tgs_design.sqlite3;Mode=ReadWriteCreate"),
new DatabaseSeeder(
@@ -9,17 +9,17 @@ using Tgstation.Server.Host.Configuration;
namespace Tgstation.Server.Host.Database
{
/// <summary>
/// <see cref="DatabaseContext{TParentContext}"/> for MySQL
/// <see cref="DatabaseContext"/> for MySQL
/// </summary>
sealed class MySqlDatabaseContext : DatabaseContext<MySqlDatabaseContext>
sealed class MySqlDatabaseContext : DatabaseContext
{
/// <summary>
/// Construct a <see cref="MySqlDatabaseContext"/>
/// </summary>
/// <param name="dbContextOptions">The <see cref="DbContextOptions{TContext}"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="databaseConfiguration">The <see cref="IOptions{TOptions}"/> of <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="databaseSeeder">The <see cref="IDatabaseSeeder"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="dbContextOptions">The <see cref="DbContextOptions{TContext}"/> for the <see cref="DatabaseContext"/></param>
/// <param name="databaseConfiguration">The <see cref="IOptions{TOptions}"/> of <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext"/></param>
/// <param name="databaseSeeder">The <see cref="IDatabaseSeeder"/> for the <see cref="DatabaseContext"/></param>
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="DatabaseContext"/></param>
public MySqlDatabaseContext(DbContextOptions<MySqlDatabaseContext> dbContextOptions, IOptions<DatabaseConfiguration> databaseConfiguration, IDatabaseSeeder databaseSeeder, ILogger<MySqlDatabaseContext> logger) : base(dbContextOptions, databaseConfiguration, databaseSeeder, logger)
{ }
@@ -8,17 +8,17 @@ using Tgstation.Server.Host.Configuration;
namespace Tgstation.Server.Host.Database
{
/// <summary>
/// <see cref="DatabaseContext{TParentContext}"/> for PostgresSQL.
/// <see cref="DatabaseContext"/> for PostgresSQL.
/// </summary>
sealed class PostgresSqlDatabaseContext : DatabaseContext<PostgresSqlDatabaseContext>
sealed class PostgresSqlDatabaseContext : DatabaseContext
{
/// <summary>
/// Construct a <see cref="SqlServerDatabaseContext"/>
/// </summary>
/// <param name="dbContextOptions">The <see cref="DbContextOptions{TContext}"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="databaseConfiguration">The <see cref="IOptions{TOptions}"/> of <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="databaseSeeder">The <see cref="IDatabaseSeeder"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="dbContextOptions">The <see cref="DbContextOptions{TContext}"/> for the <see cref="DatabaseContext"/></param>
/// <param name="databaseConfiguration">The <see cref="IOptions{TOptions}"/> of <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext"/></param>
/// <param name="databaseSeeder">The <see cref="IDatabaseSeeder"/> for the <see cref="DatabaseContext"/></param>
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="DatabaseContext"/></param>
public PostgresSqlDatabaseContext(
DbContextOptions<PostgresSqlDatabaseContext> dbContextOptions,
IOptions<DatabaseConfiguration> databaseConfiguration,
@@ -7,17 +7,17 @@ using Tgstation.Server.Host.Configuration;
namespace Tgstation.Server.Host.Database
{
/// <summary>
/// <see cref="DatabaseContext{TParentContext}"/> for Sqlserver
/// <see cref="DatabaseContext"/> for Sqlserver
/// </summary>
sealed class SqlServerDatabaseContext : DatabaseContext<SqlServerDatabaseContext>
sealed class SqlServerDatabaseContext : DatabaseContext
{
/// <summary>
/// Construct a <see cref="SqlServerDatabaseContext"/>
/// </summary>
/// <param name="dbContextOptions">The <see cref="DbContextOptions{TContext}"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="databaseConfiguration">The <see cref="IOptions{TOptions}"/> of <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="databaseSeeder">The <see cref="IDatabaseSeeder"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="dbContextOptions">The <see cref="DbContextOptions{TContext}"/> for the <see cref="DatabaseContext"/></param>
/// <param name="databaseConfiguration">The <see cref="IOptions{TOptions}"/> of <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext"/></param>
/// <param name="databaseSeeder">The <see cref="IDatabaseSeeder"/> for the <see cref="DatabaseContext"/></param>
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="DatabaseContext"/></param>
public SqlServerDatabaseContext(DbContextOptions<SqlServerDatabaseContext> dbContextOptions, IOptions<DatabaseConfiguration> databaseConfiguration, IDatabaseSeeder databaseSeeder, ILogger<SqlServerDatabaseContext> logger) : base(dbContextOptions, databaseConfiguration, databaseSeeder, logger)
{ }
@@ -9,17 +9,17 @@ using Tgstation.Server.Host.Configuration;
namespace Tgstation.Server.Host.Database
{
/// <summary>
/// <see cref="DatabaseContext{TParentContext}"/> for MySQL
/// <see cref="DatabaseContext"/> for MySQL
/// </summary>
sealed class SqliteDatabaseContext : DatabaseContext<SqliteDatabaseContext>
sealed class SqliteDatabaseContext : DatabaseContext
{
/// <summary>
/// Construct a <see cref="MySqlDatabaseContext"/>
/// </summary>
/// <param name="dbContextOptions">The <see cref="DbContextOptions{TContext}"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="databaseConfiguration">The <see cref="IOptions{TOptions}"/> of <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="databaseSeeder">The <see cref="IDatabaseSeeder"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
/// <param name="dbContextOptions">The <see cref="DbContextOptions{TContext}"/> for the <see cref="DatabaseContext"/></param>
/// <param name="databaseConfiguration">The <see cref="IOptions{TOptions}"/> of <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext"/></param>
/// <param name="databaseSeeder">The <see cref="IDatabaseSeeder"/> for the <see cref="DatabaseContext"/></param>
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="DatabaseContext"/></param>
public SqliteDatabaseContext(DbContextOptions<SqliteDatabaseContext> dbContextOptions, IOptions<DatabaseConfiguration> databaseConfiguration, IDatabaseSeeder databaseSeeder, ILogger<SqliteDatabaseContext> logger) : base(dbContextOptions, databaseConfiguration, databaseSeeder, logger)
{ }