You've already forked Extensions.Configuration.EntityFrameworkCore
Remove unnecessary TSetting type parameter from ISettingsDbContext & just use ISetting instead
This commit is contained in:
@@ -84,7 +84,7 @@ public record Setting : ISetting
|
||||
public required string Value { get; set; }
|
||||
}
|
||||
|
||||
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>, Setting>, ISettingsDbContextFactory<MyDbContext>
|
||||
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>>, ISettingsDbContextFactory<MyDbContext>
|
||||
{
|
||||
public DbSet<Setting> Settings { get; set; }
|
||||
|
||||
@@ -99,11 +99,11 @@ builder.Configuration.AddJsonFile("appsettings.json")
|
||||
...
|
||||
.AddUserSecrets<Program>(); // or wherever your connection string lives
|
||||
|
||||
builder.Configuration.AddDbSet<MyDbContext, Setting>(dbContextOptions => dbContextOptions.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
|
||||
builder.Configuration.AddDbContext<MyDbContext>(dbContextOptions => dbContextOptions.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
|
||||
|
||||
...
|
||||
// Add the PostgreSQLNotificationConfigurationReloader background service and supporting services to obtain setting reloading functionalty
|
||||
builder.Services.AddPostgreSQLNotificationConfigurationReloadService<SettingsDbContext>(); // uses default settings, other overrides exist - see code docs
|
||||
builder.Services.AddPostgreSQLNotificationConfigurationReloadService<MyDbContext>(); // uses default settings, other overrides exist - see code docs
|
||||
|
||||
await builder.Build().RunAsync(); // use config as normal
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ public static class ServiceCollectionExtensions
|
||||
/// <remarks>If your connection string contains a password then this method may not work, please use another overload</remarks>
|
||||
/// <exception cref="NullReferenceException">If your <typeparamref name="TDbContext"/> does not have a connection string</exception>
|
||||
public static IServiceCollection AddSqlServerNotificationConfigurationReloadService<TDbContext, TSetting>(this IServiceCollection services)
|
||||
where TDbContext : DbContext, ISettingsDbContext<DbSet<TSetting>, TSetting>
|
||||
where TDbContext : DbContext, ISettingsDbContext<IQueryable<TSetting>>
|
||||
where TSetting : class, ISetting
|
||||
{
|
||||
var optionsBuilder = services.AddCoreServices<TDbContext, TSetting>();
|
||||
@@ -49,7 +49,7 @@ public static class ServiceCollectionExtensions
|
||||
/// </param>
|
||||
/// <returns>The service collection it was called on now with added services</returns>
|
||||
public static IServiceCollection AddSqlServerNotificationConfigurationReloadService<TDbContext, TSetting>(this IServiceCollection services, Action<Options> configure)
|
||||
where TDbContext : DbContext, ISettingsDbContext<DbSet<TSetting>, TSetting>
|
||||
where TDbContext : DbContext, ISettingsDbContext<IQueryable<TSetting>>
|
||||
where TSetting : class, ISetting
|
||||
{
|
||||
var optionsBuilder = services.AddCoreServices<TDbContext, TSetting>();
|
||||
@@ -61,7 +61,7 @@ public static class ServiceCollectionExtensions
|
||||
|
||||
|
||||
private static OptionsBuilder<Options> AddCoreServices<TDbContext, TSetting>(this IServiceCollection services)
|
||||
where TDbContext : DbContext, ISettingsDbContext<DbSet<TSetting>, TSetting>
|
||||
where TDbContext : DbContext, ISettingsDbContext<IQueryable<TSetting>>
|
||||
where TSetting : class, ISetting
|
||||
{
|
||||
services.AddSingleton(static provider =>
|
||||
|
||||
@@ -57,7 +57,7 @@ public record Setting : ISetting
|
||||
public required string Value { get; set; }
|
||||
}
|
||||
|
||||
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>, Setting>, ISettingsDbContextFactory<MyDbContext>
|
||||
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>>, ISettingsDbContextFactory<MyDbContext>
|
||||
{
|
||||
public DbSet<Setting> Settings { get; set; }
|
||||
|
||||
@@ -72,7 +72,7 @@ builder.Configuration.AddJsonFile("appsettings.json")
|
||||
...
|
||||
.AddUserSecrets<Program>(); // or wherever your connection string lives
|
||||
|
||||
builder.Configuration.AddDbSet<MyDbContext, Setting>(dbContextOptions => dbContextOptions.UseSqlServer(builder.Configuration.GetConnectionString("Default")));
|
||||
builder.Configuration.AddDbContext<MyDbContext>(dbContextOptions => dbContextOptions.UseSqlServer(builder.Configuration.GetConnectionString("Default")));
|
||||
|
||||
...
|
||||
// Add the SqlServerNotificationConfigurationReloader background service and supporting services to obtain setting reloading functionalty
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Data;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -18,7 +16,7 @@ public class SqlServerNotificationConfigurationReloaderOptions // must be public
|
||||
|
||||
|
||||
internal class SqlServerNotificationConfigurationReloader<TDbContext, TSetting> : Microsoft.Extensions.Hosting.BackgroundService
|
||||
where TDbContext : DbContext, ISettingsDbContext<DbSet<TSetting>, TSetting>
|
||||
where TDbContext : DbContext, ISettingsDbContext<IQueryable<TSetting>>
|
||||
where TSetting : class, ISetting
|
||||
{
|
||||
private static string? _changesQueryTemplate;
|
||||
|
||||
@@ -14,9 +14,8 @@ internal interface IEntityFrameworkCoreDbSetConfigurationProvider : IConfigurati
|
||||
}
|
||||
|
||||
|
||||
internal class EntityFrameworkCoreDbSetConfigurationProvider<TDbContext, TSetting> : ConfigurationProvider, IEntityFrameworkCoreDbSetConfigurationProvider
|
||||
where TDbContext : DbContext, ISettingsDbContext<DbSet<TSetting>, TSetting>
|
||||
where TSetting : class, ISetting
|
||||
internal class EntityFrameworkCoreDbSetConfigurationProvider<TDbContext> : ConfigurationProvider, IEntityFrameworkCoreDbSetConfigurationProvider
|
||||
where TDbContext : DbContext, ISettingsDbContext<IQueryable<ISetting>>
|
||||
{
|
||||
private readonly IEntityFrameworkCoreDbSetConfigurationSource<TDbContext> _configurationSource;
|
||||
|
||||
@@ -41,14 +40,13 @@ internal interface IEntityFrameworkCoreDbSetConfigurationSource<TDbContext> wher
|
||||
internal Func<TDbContext> DbContextFactory { get; }
|
||||
}
|
||||
|
||||
internal class EntityFrameworkCoreDbSetConfigurationSource<TDbContext, TSetting> : IConfigurationSource, IEntityFrameworkCoreDbSetConfigurationSource<TDbContext>
|
||||
where TDbContext : DbContext, ISettingsDbContext<DbSet<TSetting>, TSetting>
|
||||
where TSetting : class, ISetting
|
||||
internal class EntityFrameworkCoreDbSetConfigurationSource<TDbContext> : IConfigurationSource, IEntityFrameworkCoreDbSetConfigurationSource<TDbContext>
|
||||
where TDbContext : DbContext, ISettingsDbContext<IQueryable<ISetting>>
|
||||
{
|
||||
public required Func<TDbContext> DbContextFactory { get; init; }
|
||||
|
||||
public IConfigurationProvider Build(IConfigurationBuilder builder)
|
||||
{
|
||||
return new EntityFrameworkCoreDbSetConfigurationProvider<TDbContext, TSetting>(this);
|
||||
return new EntityFrameworkCoreDbSetConfigurationProvider<TDbContext>(this);
|
||||
}
|
||||
}
|
||||
@@ -10,8 +10,7 @@ public static class ConfigurationBuilderExtensions
|
||||
/// <summary>
|
||||
/// Adds a <see cref="DbSet{}"/> off <typeparamref name="TDbContext"/> as a configuration provider to the <see cref="IConfigurationBuilder"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDbContext">Type of the <see cref="DbContext"/> which implements <see cref="ISettingsDbContext{,}"/></typeparam>
|
||||
/// <typeparam name="TSetting">Concrete type which implements <see cref="ISetting"/></typeparam>
|
||||
/// <typeparam name="TDbContext">Type of the <see cref="DbContext"/> which implements both <see cref="ISettingsDbContext{}"/> and <see cref="ISettingsDbContextFactory{}"/></typeparam>
|
||||
/// <param name="optionsTransformer">
|
||||
/// a <see cref="DbContextOptionsTransformer{}"/> which configures your <see cref="DbContextOptions{}"/>. eg.
|
||||
/// <code>
|
||||
@@ -19,12 +18,11 @@ public static class ConfigurationBuilderExtensions
|
||||
/// </code>
|
||||
/// </param>
|
||||
/// <returns>The <see cref="IConfigurationBuilder"/></returns>
|
||||
public static IConfigurationBuilder AddDbSet<TDbContext, TSetting>(this IConfigurationBuilder builder, DbContextOptionsTransformer<TDbContext> optionsTransformer)
|
||||
where TDbContext : DbContext, ISettingsDbContext<DbSet<TSetting>, TSetting>, ISettingsDbContextFactory<TDbContext>
|
||||
where TSetting : class, ISetting
|
||||
public static IConfigurationBuilder AddDbContext<TDbContext>(this IConfigurationBuilder builder, DbContextOptionsTransformer<TDbContext> optionsTransformer)
|
||||
where TDbContext : DbContext, ISettingsDbContext<System.Linq.IQueryable<ISetting>>, ISettingsDbContextFactory<TDbContext>
|
||||
{
|
||||
var options = optionsTransformer(new DbContextOptionsBuilder<TDbContext>()).Options;
|
||||
var configurationSource = new EntityFrameworkCoreDbSetConfigurationSource<TDbContext, TSetting>()
|
||||
var configurationSource = new EntityFrameworkCoreDbSetConfigurationSource<TDbContext>()
|
||||
{
|
||||
DbContextFactory = () => TDbContext.Create(options)
|
||||
};
|
||||
|
||||
@@ -4,11 +4,10 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace RAIC.Extensions.Configuration.EntityFrameworkCore;
|
||||
|
||||
public interface ISettingsDbContext<out TSettingDbSet, out TSetting> : IDisposable
|
||||
where TSettingDbSet : DbSet<TSetting>
|
||||
where TSetting : class, ISetting
|
||||
public interface ISettingsDbContext<out TSettings> : IDisposable
|
||||
where TSettings : System.Linq.IQueryable<ISetting>
|
||||
{
|
||||
TSettingDbSet Settings { get; }
|
||||
TSettings Settings { get; }
|
||||
}
|
||||
|
||||
public interface ISettingsDbContextFactory<TDbContext>
|
||||
|
||||
@@ -33,7 +33,7 @@ public record Setting : ISetting
|
||||
public required string Value { get; set; }
|
||||
}
|
||||
|
||||
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>, Setting>, ISettingsDbContextFactory<MyDbContext>
|
||||
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>>, ISettingsDbContextFactory<MyDbContext>
|
||||
{
|
||||
public DbSet<Setting> Settings { get; set; }
|
||||
|
||||
@@ -44,10 +44,11 @@ var builder = Host.CreateApplicationBuilder(args) // or WebApplication.CreateB
|
||||
|
||||
// build an initial configuration
|
||||
builder.Configuration.AddJsonFile("appsettings.json")
|
||||
...
|
||||
.AddUserSecrets<Program>(); // or whereever your connection string lives
|
||||
|
||||
// obtain connection string from preliminary config so can initialise other settings from DbSet
|
||||
builder.Configuration.AddDbSet<MyDbContext, Setting>(dbContextOptions => dbContextOptions.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
|
||||
// obtain connection string from preliminary config so can initialise other settings from DbContext
|
||||
builder.Configuration.AddDbContext<MyDbContext>(dbContextOptions => dbContextOptions.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
|
||||
|
||||
...
|
||||
|
||||
|
||||
Reference in New Issue
Block a user