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