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:
parent
33f7ced3b2
commit
90a12f52bc
@ -1,4 +1,5 @@
|
|||||||
using DigitalData.Core.Abstraction.Application.Repository;
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
using DigitalData.Core.Infrastructure.Factory;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System.ComponentModel.DataAnnotations.Schema;
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
@ -32,7 +33,7 @@ public static class DependencyInjection
|
|||||||
|
|
||||||
internal RepositoryConfiguration() { }
|
internal RepositoryConfiguration() { }
|
||||||
|
|
||||||
public RepositoryConfiguration RegisterFromAssembly<TDbContext>(Assembly assembly) where TDbContext : DbContext
|
public void RegisterFromAssembly<TDbContext>(Assembly assembly) where TDbContext : DbContext
|
||||||
{
|
{
|
||||||
void reg(IServiceCollection services)
|
void reg(IServiceCollection services)
|
||||||
{
|
{
|
||||||
@ -42,27 +43,38 @@ public static class DependencyInjection
|
|||||||
|
|
||||||
foreach (var entityType in entityTypes)
|
foreach (var entityType in entityTypes)
|
||||||
{
|
{
|
||||||
|
#region Repository
|
||||||
|
/// register repository
|
||||||
// create generic DbRepository<DbContext, TEntity> type
|
// create generic DbRepository<DbContext, TEntity> type
|
||||||
var repositoryType = typeof(DbRepository<,>).MakeGenericType(typeof(TDbContext), entityType);
|
var repositoryType = typeof(DbRepository<,>).MakeGenericType(typeof(TDbContext), entityType);
|
||||||
var interfaceType = typeof(IRepository<>).MakeGenericType(entityType);
|
var interfaceType = typeof(IRepository<>).MakeGenericType(entityType);
|
||||||
|
|
||||||
// add into DI container as Scoped
|
// add into DI container as Scoped
|
||||||
services.AddScoped(interfaceType, repositoryType);
|
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);
|
RegsFromAssembly.Enqueue(reg);
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public RepositoryConfiguration RegisterEntity<TDbContext, TEntity>() where TDbContext : DbContext
|
public void RegisterEntity<TDbContext, TEntity>() where TDbContext : DbContext
|
||||||
where TEntity : class
|
where TEntity : class
|
||||||
{
|
{
|
||||||
static void reg(IServiceCollection services)
|
static void reg(IServiceCollection services)
|
||||||
=> services.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>();
|
=> services.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>();
|
||||||
|
|
||||||
RegsEntity.Enqueue(reg);
|
RegsEntity.Enqueue(reg);
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,4 +83,13 @@ public static class DependencyInjection
|
|||||||
while (queue.Count > 0)
|
while (queue.Count > 0)
|
||||||
queue.Dequeue().Invoke(services);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
using DigitalData.Core.Abstraction.Application.Repository;
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace DigitalData.Core.Infrastructure;
|
namespace DigitalData.Core.Infrastructure.Factory;
|
||||||
|
|
||||||
public class DbRepositoryFactory : IRepositoryFactory
|
public class DbRepositoryFactory : IRepositoryFactory
|
||||||
{
|
{
|
||||||
13
DigitalData.Core.Infrastructure/Factory/DbSetFactory.cs
Normal file
13
DigitalData.Core.Infrastructure/Factory/DbSetFactory.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user