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:
tekh 2025-09-12 11:35:40 +02:00
parent db8c41368d
commit e2853b64d1
4 changed files with 14 additions and 11 deletions

View File

@ -1,5 +1,6 @@
using AutoMapper; using AutoMapper;
using DigitalData.Core.Abstraction.Application.Repository; using DigitalData.Core.Abstraction.Application.Repository;
using DigitalData.Core.Infrastructure.Factory;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions; using System.Linq.Expressions;
@ -13,10 +14,10 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
public IMapper Mapper { get; } 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; Context = context;
Entities = queryFactory(context); Entities = factory.Create(context);
Mapper = mapper; Mapper = mapper;
} }

View File

@ -44,7 +44,7 @@ public static class DependencyInjection
// 2. register entities (can overwrite) // 2. register entities (can overwrite)
RegsEntity.InvokeAll(services); RegsEntity.InvokeAll(services);
// 1. register db set factories (can overwrite) // 3. register db set factories (can overwrite)
RegsDbSetFactory.InvokeAll(services); RegsDbSetFactory.InvokeAll(services);
} }
@ -77,7 +77,7 @@ public static class DependencyInjection
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var genericMethod = addDbSetFactoryMethod!.MakeGenericMethod(typeof(TDbContext), entityType); var genericMethod = addDbSetFactoryMethod!.MakeGenericMethod(typeof(TDbContext), entityType);
genericMethod.Invoke(null, new [] { services }); genericMethod.Invoke(null, new [] { services, null });
#endregion DbSetFactory #endregion DbSetFactory
} }
} }

View File

@ -1,6 +1,4 @@
namespace DigitalData.Core.Tests.Infrastructure; using DigitalData.Core.Tests.Mock;
using DigitalData.Core.Tests.Mock;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -8,6 +6,8 @@ using System.Reflection;
using DigitalData.Core.Abstraction.Application.Repository; using DigitalData.Core.Abstraction.Application.Repository;
using DigitalData.Core.Infrastructure; using DigitalData.Core.Infrastructure;
namespace DigitalData.Core.Tests.Infrastructure;
public class DbRepositoryTests public class DbRepositoryTests
{ {
private IHost _host; private IHost _host;

View File

@ -1,7 +1,9 @@
using DigitalData.Core.Abstraction.Application.Repository; using DigitalData.Core.Abstraction.Application.Repository;
using System.ComponentModel.DataAnnotations.Schema;
namespace DigitalData.Core.Tests.Mock; namespace DigitalData.Core.Tests.Mock;
[Table("USER")]
public class User : UserBase, IEntity public class User : UserBase, IEntity
{ {
public required int Id { get; init; } public required int Id { get; init; }