diff --git a/DigitalData.Core.Infrastructure/DependencyInjection.cs b/DigitalData.Core.Infrastructure/DependencyInjection.cs index a2756b3..41a328a 100644 --- a/DigitalData.Core.Infrastructure/DependencyInjection.cs +++ b/DigitalData.Core.Infrastructure/DependencyInjection.cs @@ -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(Assembly assembly) where TDbContext : DbContext + public void RegisterFromAssembly(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 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() where TDbContext : DbContext + public void RegisterEntity() where TDbContext : DbContext where TEntity : class { - static void reg(IServiceCollection services) + static void reg(IServiceCollection services) => services.AddScoped, DbRepository>(); 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(this IServiceCollection services, Func>? create = null) + where TDbContext : DbContext + where TEntity : class + { + create ??= ctx => ctx.Set(); + services.AddSingleton(_ => new DbSetFactory(create)); + return services; + } } \ No newline at end of file diff --git a/DigitalData.Core.Infrastructure/DbRepositoryFactory.cs b/DigitalData.Core.Infrastructure/Factory/DbRepositoryFactory.cs similarity index 89% rename from DigitalData.Core.Infrastructure/DbRepositoryFactory.cs rename to DigitalData.Core.Infrastructure/Factory/DbRepositoryFactory.cs index 1e601a4..b2c44aa 100644 --- a/DigitalData.Core.Infrastructure/DbRepositoryFactory.cs +++ b/DigitalData.Core.Infrastructure/Factory/DbRepositoryFactory.cs @@ -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 { diff --git a/DigitalData.Core.Infrastructure/Factory/DbSetFactory.cs b/DigitalData.Core.Infrastructure/Factory/DbSetFactory.cs new file mode 100644 index 0000000..5e518e1 --- /dev/null +++ b/DigitalData.Core.Infrastructure/Factory/DbSetFactory.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; + +namespace DigitalData.Core.Infrastructure.Factory; + +public class DbSetFactory where TDbContext : DbContext where TEntity : class +{ + public readonly Func> Create; + + public DbSetFactory(Func> create) + { + Create = create; + } +} \ No newline at end of file