feat(infrastructure): add DbSetFactory registration in DependencyInjection

- Introduced AddDbSetFactory to support DbSet factory registration for entities.
- Updated RepositoryConfiguration.RegisterFromAssembly to register both repositories and DbSet factories.
- Changed RegisterFromAssembly and RegisterEntity to void methods instead of fluent interface.
- Extracted DbRepositoryFactory into its own Factory namespace for better separation of concerns.
This commit is contained in:
tekh 2025-09-11 17:20:47 +02:00
parent 33f7ced3b2
commit 90a12f52bc
3 changed files with 40 additions and 6 deletions

View File

@ -1,4 +1,5 @@
using DigitalData.Core.Abstraction.Application.Repository;
using DigitalData.Core.Infrastructure.Factory;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel.DataAnnotations.Schema;
@ -32,7 +33,7 @@ public static class DependencyInjection
internal RepositoryConfiguration() { }
public RepositoryConfiguration RegisterFromAssembly<TDbContext>(Assembly assembly) where TDbContext : DbContext
public void RegisterFromAssembly<TDbContext>(Assembly assembly) where TDbContext : DbContext
{
void reg(IServiceCollection services)
{
@ -42,27 +43,38 @@ public static class DependencyInjection
foreach (var entityType in entityTypes)
{
#region Repository
/// register repository
// create generic DbRepository<DbContext, TEntity> type
var repositoryType = typeof(DbRepository<,>).MakeGenericType(typeof(TDbContext), entityType);
var interfaceType = typeof(IRepository<>).MakeGenericType(entityType);
// add into DI container as Scoped
services.AddScoped(interfaceType, repositoryType);
#endregion Repository
#region DbSetFactory
/// register DbSetFactory
var addDbSetFactoryMethod = typeof(DependencyInjection)
.GetMethod(nameof(AddDbSetFactory),
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var genericMethod = addDbSetFactoryMethod!.MakeGenericMethod(typeof(TDbContext), entityType);
genericMethod.Invoke(null, new [] { services });
#endregion DbSetFactory
}
}
RegsFromAssembly.Enqueue(reg);
return this;
}
public RepositoryConfiguration RegisterEntity<TDbContext, TEntity>() where TDbContext : DbContext
public void RegisterEntity<TDbContext, TEntity>() where TDbContext : DbContext
where TEntity : class
{
static void reg(IServiceCollection services)
static void reg(IServiceCollection services)
=> services.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>();
RegsEntity.Enqueue(reg);
return this;
}
}
@ -71,4 +83,13 @@ public static class DependencyInjection
while (queue.Count > 0)
queue.Dequeue().Invoke(services);
}
internal static IServiceCollection AddDbSetFactory<TDbContext, TEntity>(this IServiceCollection services, Func<TDbContext, DbSet<TEntity>>? create = null)
where TDbContext : DbContext
where TEntity : class
{
create ??= ctx => ctx.Set<TEntity>();
services.AddSingleton(_ => new DbSetFactory<TDbContext, TEntity>(create));
return services;
}
}

View File

@ -1,7 +1,7 @@
using DigitalData.Core.Abstraction.Application.Repository;
using Microsoft.Extensions.DependencyInjection;
namespace DigitalData.Core.Infrastructure;
namespace DigitalData.Core.Infrastructure.Factory;
public class DbRepositoryFactory : IRepositoryFactory
{

View File

@ -0,0 +1,13 @@
using Microsoft.EntityFrameworkCore;
namespace DigitalData.Core.Infrastructure.Factory;
public class DbSetFactory<TDbContext,TEntity> where TDbContext : DbContext where TEntity : class
{
public readonly Func<TDbContext, DbSet<TEntity>> Create;
public DbSetFactory(Func<TDbContext, DbSet<TEntity>> create)
{
Create = create;
}
}