feat(infrastructure): enhance repository DI with DbSetFactory and IRepositoryFactory
- Added `RegisterAllServices` method to centralize service registration - Introduced `RegsDbSetFactory` queue for custom DbSetFactory registrations - Extended `RegisterEntity` to support optional DbSetFactory - Added `RegisterDbSetFactory` for explicit DbSetFactory registration - Registered `IRepositoryFactory` with `DbRepositoryFactory` in DI
This commit is contained in:
parent
90a12f52bc
commit
7c2a165479
@ -11,14 +11,13 @@ public static class DependencyInjection
|
|||||||
{
|
{
|
||||||
public static IServiceCollection AddDbRepository(this IServiceCollection services, Action<RepositoryConfiguration> options)
|
public static IServiceCollection AddDbRepository(this IServiceCollection services, Action<RepositoryConfiguration> options)
|
||||||
{
|
{
|
||||||
|
// register services from configuration
|
||||||
var cfg = new RepositoryConfiguration();
|
var cfg = new RepositoryConfiguration();
|
||||||
options.Invoke(cfg);
|
options.Invoke(cfg);
|
||||||
|
cfg.RegisterAllServices(services);
|
||||||
|
|
||||||
// 1. FromAssembly
|
// register db repository factory
|
||||||
cfg.RegsFromAssembly.InvokeAll(services);
|
services.AddSingleton<IRepositoryFactory, DbRepositoryFactory>();
|
||||||
|
|
||||||
// 2. Entities to be able overwrite
|
|
||||||
cfg.RegsEntity.InvokeAll(services);
|
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
@ -26,10 +25,25 @@ public static class DependencyInjection
|
|||||||
public class RepositoryConfiguration
|
public class RepositoryConfiguration
|
||||||
{
|
{
|
||||||
// 1. register from assembly
|
// 1. register from assembly
|
||||||
internal Queue<Action<IServiceCollection>> RegsFromAssembly = new();
|
private readonly Queue<Action<IServiceCollection>> RegsFromAssembly = new();
|
||||||
|
|
||||||
// 2. register entities (can overwrite)
|
// 2. register entities (can overwrite)
|
||||||
internal Queue<Action<IServiceCollection>> RegsEntity = new();
|
private readonly Queue<Action<IServiceCollection>> RegsEntity = new();
|
||||||
|
|
||||||
|
// 3. register db set factories (can overwrite)
|
||||||
|
private readonly Queue<Action<IServiceCollection>> RegsDbSetFactory = new();
|
||||||
|
|
||||||
|
internal void RegisterAllServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
// 1. register from assembly
|
||||||
|
RegsFromAssembly.InvokeAll(services);
|
||||||
|
|
||||||
|
// 2. register entities (can overwrite)
|
||||||
|
RegsEntity.InvokeAll(services);
|
||||||
|
|
||||||
|
// 1. register db set factories (can overwrite)
|
||||||
|
RegsDbSetFactory.InvokeAll(services);
|
||||||
|
}
|
||||||
|
|
||||||
internal RepositoryConfiguration() { }
|
internal RepositoryConfiguration() { }
|
||||||
|
|
||||||
@ -68,14 +82,22 @@ public static class DependencyInjection
|
|||||||
RegsFromAssembly.Enqueue(reg);
|
RegsFromAssembly.Enqueue(reg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RegisterEntity<TDbContext, TEntity>() where TDbContext : DbContext
|
public void RegisterEntity<TDbContext, TEntity>(Func<TDbContext, DbSet<TEntity>>? dbSetFactory = null)
|
||||||
|
where TDbContext : DbContext
|
||||||
where TEntity : class
|
where TEntity : class
|
||||||
{
|
{
|
||||||
static void reg(IServiceCollection services)
|
void reg(IServiceCollection services)
|
||||||
=> services.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>();
|
=> services
|
||||||
|
.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>()
|
||||||
|
.AddDbSetFactory(dbSetFactory);
|
||||||
|
|
||||||
RegsEntity.Enqueue(reg);
|
RegsEntity.Enqueue(reg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RegisterDbSetFactory<TDbContext, TEntity>(Func<TDbContext, DbSet<TEntity>> dbSetFactory)
|
||||||
|
where TDbContext : DbContext
|
||||||
|
where TEntity : class
|
||||||
|
=> RegsDbSetFactory.Enqueue(s => s.AddDbSetFactory(dbSetFactory));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void InvokeAll<T>(this Queue<Action<T>> queue, T services)
|
private static void InvokeAll<T>(this Queue<Action<T>> queue, T services)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user