You've already forked Extensions.Configuration.EntityFrameworkCore
Remove unnecessary TSetting type parameter from ISettingsDbContext & just use ISetting instead
This commit is contained in:
@@ -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