refactor(repository): replace queryFactory with DbSetFactory in DbRepository
- Updated DbRepository constructor to use DbSetFactory<TDbContext, TEntity> instead of a Func<TDbContext, DbSet<TEntity>> queryFactory. - Adjusted namespace imports to include DigitalData.Core.Infrastructure.Factory. - Improved repository instantiation consistency and encapsulation.
This commit is contained in:
parent
db8c41368d
commit
e2853b64d1
@ -1,5 +1,6 @@
|
||||
using AutoMapper;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Infrastructure.Factory;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
@ -13,10 +14,10 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
||||
|
||||
public IMapper Mapper { get; }
|
||||
|
||||
public DbRepository(TDbContext context, Func<TDbContext, DbSet<TEntity>> queryFactory, IMapper mapper)
|
||||
public DbRepository(TDbContext context, DbSetFactory<TDbContext, TEntity> factory, IMapper mapper)
|
||||
{
|
||||
Context = context;
|
||||
Entities = queryFactory(context);
|
||||
Entities = factory.Create(context);
|
||||
Mapper = mapper;
|
||||
}
|
||||
|
||||
|
||||
@ -44,7 +44,7 @@ public static class DependencyInjection
|
||||
// 2. register entities (can overwrite)
|
||||
RegsEntity.InvokeAll(services);
|
||||
|
||||
// 1. register db set factories (can overwrite)
|
||||
// 3. register db set factories (can overwrite)
|
||||
RegsDbSetFactory.InvokeAll(services);
|
||||
}
|
||||
|
||||
@ -77,7 +77,7 @@ public static class DependencyInjection
|
||||
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
|
||||
|
||||
var genericMethod = addDbSetFactoryMethod!.MakeGenericMethod(typeof(TDbContext), entityType);
|
||||
genericMethod.Invoke(null, new [] { services });
|
||||
genericMethod.Invoke(null, new [] { services, null });
|
||||
#endregion DbSetFactory
|
||||
}
|
||||
}
|
||||
@ -85,7 +85,7 @@ public static class DependencyInjection
|
||||
RegsFromAssembly.Enqueue(reg);
|
||||
}
|
||||
|
||||
public void RegisterEntity<TDbContext, TEntity>(Func<TDbContext, DbSet<TEntity>>? dbSetFactory = null)
|
||||
public void RegisterEntity<TDbContext, TEntity>(Func<TDbContext, DbSet<TEntity>>? dbSetFactory = null)
|
||||
where TDbContext : DbContext
|
||||
where TEntity : class
|
||||
{
|
||||
@ -99,7 +99,7 @@ public static class DependencyInjection
|
||||
|
||||
public void RegisterDbSetFactory<TDbContext, TEntity>(Func<TDbContext, DbSet<TEntity>> dbSetFactory)
|
||||
where TDbContext : DbContext
|
||||
where TEntity : class
|
||||
where TEntity : class
|
||||
=> RegsDbSetFactory.Enqueue(s => s.AddDbSetFactory(dbSetFactory));
|
||||
}
|
||||
|
||||
@ -109,8 +109,8 @@ public static class DependencyInjection
|
||||
queue.Dequeue().Invoke(services);
|
||||
}
|
||||
|
||||
internal static IServiceCollection AddDbSetFactory<TDbContext, TEntity>(this IServiceCollection services, Func<TDbContext, DbSet<TEntity>>? create = null)
|
||||
where TDbContext : DbContext
|
||||
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>();
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
namespace DigitalData.Core.Tests.Infrastructure;
|
||||
|
||||
using DigitalData.Core.Tests.Mock;
|
||||
using DigitalData.Core.Tests.Mock;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
@ -8,6 +6,8 @@ using System.Reflection;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Infrastructure;
|
||||
|
||||
namespace DigitalData.Core.Tests.Infrastructure;
|
||||
|
||||
public class DbRepositoryTests
|
||||
{
|
||||
private IHost _host;
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace DigitalData.Core.Tests.Mock;
|
||||
|
||||
[Table("USER")]
|
||||
public class User : UserBase, IEntity
|
||||
{
|
||||
public required int Id { get; init; }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user