Compare commits

...

7 Commits

Author SHA1 Message Date
db8c41368d arrange tests 2025-09-11 18:53:39 +02:00
8743325067 update DbRepository 2025-09-11 18:35:17 +02:00
be96bd0c07 refactor(repository): add query parameter to IRepository.Where and constrain IRepository.Entity
- Updated `IRepository<TEntity>.Where()` to accept an `Expression<Func<TEntity, bool>>` for filtering.
- Added `where TEntity : IEntity` constraint to `IRepository.Entity<TEntity>()` method.
- No functional logic changes, only interface refactoring for stronger typing and query support.
2025-09-11 18:30:38 +02:00
b181e1543f create IEntity and IDto interfaces 2025-09-11 18:03:55 +02:00
7c2a165479 feat(infrastructure): enhance repository DI with DbSetFactory and IRepositoryFactory
- Added `RegisterAllServices` method to centralize service registration
- Introduced `RegsDbSetFactory` queue for custom DbSetFactory registrations
- Extended `RegisterEntity` to support optional DbSetFactory
- Added `RegisterDbSetFactory` for explicit DbSetFactory registration
- Registered `IRepositoryFactory` with `DbRepositoryFactory` in DI
2025-09-11 17:56:45 +02:00
90a12f52bc 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.
2025-09-11 17:20:47 +02:00
33f7ced3b2 refactor(dependency-injection): replace generic AddDbRepository with configurable registration
- Replaced `AddDbRepository<TDbContext, TEntity>` with `AddDbRepository(Action<RepositoryConfiguration>)` for more flexible DI registration.
- Added `RepositoryConfiguration` class to support:
  - Registering repositories from an assembly (`RegisterFromAssembly`)
  - Registering individual entities (`RegisterEntity`)
- Queues (`RegsFromAssembly` and `RegsEntity`) are used to defer registration until `InvokeAll` is called.
- Allows overriding and scanning multiple entities dynamically instead of static generic method.
2025-09-11 13:07:45 +02:00
14 changed files with 196 additions and 70 deletions

View File

@ -19,37 +19,52 @@ public static class Extensions
#endregion #endregion
#region IRepository #region IRepository
public static IQueryable<TEntity> Get<TEntity>(this IRepository repository) => repository.Entity<TEntity>().Get(); public static IQueryable<TEntity> Get<TEntity>(this IRepository repository) where TEntity : IEntity
=> repository.Entity<TEntity>().Get();
public static IQueryable<TEntity> Where<TEntity>(this IRepository repository) => repository.Entity<TEntity>().Where(); public static IQueryable<TEntity> Where<TEntity>(this IRepository repository, Expression<Func<TEntity, bool>> expression)
where TEntity : IEntity
=> repository.Entity<TEntity>().Where(expression);
#region Create #region Create
public static Task<TEntity> CreateAsync<TEntity>(this IRepository repository, TEntity entity, CancellationToken cancel = default) public static Task<TEntity> CreateAsync<TEntity>(this IRepository repository, TEntity entity, CancellationToken cancel = default)
where TEntity : IEntity
=> repository.Entity<TEntity>().CreateAsync(entity, cancel); => repository.Entity<TEntity>().CreateAsync(entity, cancel);
public static Task<TEntity> CreateAsync<TEntity, TDto>(this IRepository repository, TDto dto, CancellationToken cancel = default) public static Task<TEntity> CreateAsync<TEntity, TDto>(this IRepository repository, TDto dto, CancellationToken cancel = default)
where TEntity : IEntity
where TDto : IDto<TEntity>
=> repository.Entity<TEntity>().CreateAsync(dto, cancel); => repository.Entity<TEntity>().CreateAsync(dto, cancel);
public static Task<IEnumerable<TEntity>> CreateAsync<TEntity>(this IRepository repository, IEnumerable<TEntity> entities, CancellationToken cancel = default) public static Task<IEnumerable<TEntity>> CreateAsync<TEntity>(this IRepository repository, IEnumerable<TEntity> entities, CancellationToken cancel = default)
where TEntity : IEntity
=> repository.Entity<TEntity>().CreateAsync(entities, cancel); => repository.Entity<TEntity>().CreateAsync(entities, cancel);
public static Task<IEnumerable<TEntity>> CreateAsync<TEntity, TDto>(this IRepository repository, IEnumerable<TDto> dtos, CancellationToken cancel = default) public static Task<IEnumerable<TEntity>> CreateAsync<TEntity, TDto>(this IRepository repository, IEnumerable<TDto> dtos, CancellationToken cancel = default)
where TEntity : IEntity
where TDto : IDto<TEntity>
=> repository.Entity<TEntity>().CreateAsync(dtos, cancel); => repository.Entity<TEntity>().CreateAsync(dtos, cancel);
#endregion Create #endregion Create
#region Update #region Update
public static Task UpdateAsync<TEntity, TDto>(this IRepository repository, TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default) public static Task UpdateAsync<TEntity, TDto>(this IRepository repository, TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default)
where TEntity : IEntity
where TDto : IDto<TEntity>
=> repository.Entity<TEntity>().UpdateAsync(dto, expression, cancel); => repository.Entity<TEntity>().UpdateAsync(dto, expression, cancel);
public static Task UpdateAsync<TEntity, TDto>(this IRepository repository, TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default) public static Task UpdateAsync<TEntity, TDto>(this IRepository repository, TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
where TEntity : IEntity
where TDto : IDto<TEntity>
=> repository.Entity<TEntity>().UpdateAsync(dto, query, cancel); => repository.Entity<TEntity>().UpdateAsync(dto, query, cancel);
#endregion #endregion
#region Delete #region Delete
public static Task DeleteAsync<TEntity>(this IRepository repository, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default) public static Task DeleteAsync<TEntity>(this IRepository repository, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default)
where TEntity : IEntity
=> repository.Entity<TEntity>().DeleteAsync(expression, cancel); => repository.Entity<TEntity>().DeleteAsync(expression, cancel);
public static Task DeleteAsync<TEntity>(this IRepository repository, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default) public static Task DeleteAsync<TEntity>(this IRepository repository, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
where TEntity : IEntity
=> repository.Entity<TEntity>().DeleteAsync(query, cancel); => repository.Entity<TEntity>().DeleteAsync(query, cancel);
#endregion #endregion
#endregion #endregion

View File

@ -0,0 +1,16 @@
namespace DigitalData.Core.Abstraction.Application.Repository;
/// <summary>
/// Ensures that extension methods are handled securely
/// </summary>
public interface IEntity
{
}
/// <summary>
/// Ensures that extension methods are handled securely
/// </summary>
/// <typeparam name="Entity"></typeparam>
public interface IDto<Entity> where Entity : IEntity
{
}

View File

@ -11,7 +11,7 @@ public interface IRepository<TEntity>
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken cancel = default); public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken cancel = default);
public IQueryable<TEntity> Where(); public IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression);
public IQueryable<TEntity> Get(); public IQueryable<TEntity> Get();
@ -34,5 +34,5 @@ public interface IRepository<TEntity>
public interface IRepository public interface IRepository
{ {
public IRepository<TEntity> Entity<TEntity>(); public IRepository<TEntity> Entity<TEntity>() where TEntity : IEntity;
} }

View File

@ -1,7 +1,6 @@
using AutoMapper; using AutoMapper;
using DigitalData.Core.Abstraction.Application.Repository; using DigitalData.Core.Abstraction.Application.Repository;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Linq.Expressions; using System.Linq.Expressions;
namespace DigitalData.Core.Infrastructure; namespace DigitalData.Core.Infrastructure;
@ -35,7 +34,7 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
return entities; return entities;
} }
public IQueryable<TEntity> Where() => Entities.AsQueryable(); public IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression) => Entities.AsQueryable().Where(expression);
public IQueryable<TEntity> Get() => Entities.AsNoTracking(); public IQueryable<TEntity> Get() => Entities.AsNoTracking();
@ -86,5 +85,5 @@ public class DbRepository : IRepository
_factory = factory; _factory = factory;
} }
public IRepository<TEntity> Entity<TEntity>() => _factory.Get<TEntity>(); public IRepository<TEntity> Entity<TEntity>() where TEntity : IEntity => _factory.Get<TEntity>();
} }

View File

@ -1,19 +1,120 @@
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.Reflection;
namespace DigitalData.Core.Infrastructure; namespace DigitalData.Core.Infrastructure;
public static class DependencyInjection public static class DependencyInjection
{ {
public static EntityConfigurationOptions<TEntity> AddDbRepository<TDbContext, TEntity>(this IServiceCollection services, Func<TDbContext, DbSet<TEntity>> queryFactory) public static IServiceCollection AddDbRepository(this IServiceCollection services, Action<RepositoryConfiguration> options)
where TDbContext : DbContext {
// register services from configuration
var cfg = new RepositoryConfiguration();
options.Invoke(cfg);
cfg.RegisterAllServices(services);
// register db repository
services.AddSingleton<IRepository, DbRepository>();
// register db repository factory
services.AddSingleton<IRepositoryFactory, DbRepositoryFactory>();
return services;
}
public class RepositoryConfiguration
{
// 1. register from assembly
private readonly Queue<Action<IServiceCollection>> RegsFromAssembly = new();
// 2. register entities (can overwrite)
private readonly Queue<Action<IServiceCollection>> RegsEntity = new();
// 3. register db set factories (can overwrite)
private readonly Queue<Action<IServiceCollection>> RegsDbSetFactory = new();
internal void RegisterAllServices(IServiceCollection services)
{
// 1. register from assembly
RegsFromAssembly.InvokeAll(services);
// 2. register entities (can overwrite)
RegsEntity.InvokeAll(services);
// 1. register db set factories (can overwrite)
RegsDbSetFactory.InvokeAll(services);
}
internal RepositoryConfiguration() { }
public void RegisterFromAssembly<TDbContext>(Assembly assembly) where TDbContext : DbContext
{
void reg(IServiceCollection services)
{
// scan all types in the Assembly
var entityTypes = assembly.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && t.GetCustomAttribute<TableAttribute>() != null);
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);
}
public void RegisterEntity<TDbContext, TEntity>(Func<TDbContext, DbSet<TEntity>>? dbSetFactory = null)
where TDbContext : DbContext
where TEntity : class
{
void reg(IServiceCollection services)
=> services
.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>()
.AddDbSetFactory(dbSetFactory);
RegsEntity.Enqueue(reg);
}
public void RegisterDbSetFactory<TDbContext, TEntity>(Func<TDbContext, DbSet<TEntity>> dbSetFactory)
where TDbContext : DbContext
where TEntity : class
=> RegsDbSetFactory.Enqueue(s => s.AddDbSetFactory(dbSetFactory));
}
private static void InvokeAll<T>(this Queue<Action<T>> queue, T services)
{
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 where TEntity : class
{ {
services create ??= ctx => ctx.Set<TEntity>();
.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>() services.AddSingleton(_ => new DbSetFactory<TDbContext, TEntity>(create));
.AddSingleton(queryFactory); return services;
return new EntityConfigurationOptions<TEntity>(services);
} }
} }

View File

@ -1,27 +0,0 @@
using DigitalData.Core.Abstraction.Application.Repository;
using Microsoft.Extensions.DependencyInjection;
namespace DigitalData.Core.Infrastructure;
public class EntityConfigurationOptions<TEntity>
{
private readonly IServiceCollection _services;
public EntityConfigurationOptions(IServiceCollection services)
{
_services = services;
}
public EntityConfigurationOptions<TEntity> AddCustomMapper<TEntityMapper>(Action<IServiceCollection> configure, Func<IServiceProvider, TEntityMapper>? factory = null)
where TEntityMapper : class, IEntityMapper<TEntity>
{
configure(_services);
if (factory is null)
_services.AddSingleton<IEntityMapper<TEntity>, TEntityMapper>();
else
_services.AddSingleton(factory);
return this;
}
}

View File

@ -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
{ {

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;
}
}

View File

@ -26,7 +26,6 @@
<ProjectReference Include="..\DigitalData.Core.Application\DigitalData.Core.Application.csproj" /> <ProjectReference Include="..\DigitalData.Core.Application\DigitalData.Core.Application.csproj" />
<ProjectReference Include="..\DigitalData.Core.Client\DigitalData.Core.Client.csproj" /> <ProjectReference Include="..\DigitalData.Core.Client\DigitalData.Core.Client.csproj" />
<ProjectReference Include="..\DigitalData.Core.DTO\DigitalData.Core.DTO.csproj" /> <ProjectReference Include="..\DigitalData.Core.DTO\DigitalData.Core.DTO.csproj" />
<ProjectReference Include="..\DigitalData.Core.Infrastructure.AutoMapper\DigitalData.Core.Infrastructure.AutoMapper.csproj" />
<ProjectReference Include="..\DigitalData.Core.Infrastructure\DigitalData.Core.Infrastructure.csproj" /> <ProjectReference Include="..\DigitalData.Core.Infrastructure\DigitalData.Core.Infrastructure.csproj" />
<ProjectReference Include="..\DigitalData.Core.Security\DigitalData.Core.Security.csproj" /> <ProjectReference Include="..\DigitalData.Core.Security\DigitalData.Core.Security.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -1,19 +1,18 @@
namespace DigitalData.Core.Tests.Infrastructure; namespace DigitalData.Core.Tests.Infrastructure;
using DigitalData.Core.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;
using System.Reflection; using System.Reflection;
using DigitalData.Core.Infrastructure.AutoMapper; using DigitalData.Core.Abstraction.Application.Repository;
using DigitalData.Core.Application.Interfaces.Repository; using DigitalData.Core.Infrastructure;
public class DbRepositoryTests public class DbRepositoryTests
{ {
private IHost _host; private IHost _host;
private IRepository<User> _userRepo; private IRepository Repo;
[SetUp] [SetUp]
public void Setup() public void Setup()
@ -22,13 +21,16 @@ public class DbRepositoryTests
builder.Services.AddDbContext<MockDbContext>(opt => opt.UseInMemoryDatabase("MockDB")); builder.Services.AddDbContext<MockDbContext>(opt => opt.UseInMemoryDatabase("MockDB"));
builder.Services.AddDbRepository<MockDbContext, User>(context => context.Users).UseAutoMapper(typeof(UserCreateDto), typeof(UserReadDto), typeof(UserBase)); builder.Services.AddDbRepository(opt =>
{
opt.RegisterFromAssembly<MockDbContext>(typeof(User).Assembly);
});
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly()); builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
_host = builder.Build(); _host = builder.Build();
_userRepo = _host.Services.GetRequiredService<IRepository<User>>(); Repo = _host.Services.GetRequiredService<IRepository>();
} }
[TearDown] [TearDown]
@ -47,9 +49,9 @@ public class DbRepositoryTests
// Act & Assert // Act & Assert
if (multiple) if (multiple)
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate(Random.Shared.Next(1, 10)))); Assert.DoesNotThrowAsync(async () => await Repo.CreateAsync(faker.Generate(Random.Shared.Next(1, 10))));
else else
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate())); Assert.DoesNotThrowAsync(async () => await Repo.CreateAsync(faker.Generate()));
} }
[TestCase(true, TestName = "WhenDtoUsed")] [TestCase(true, TestName = "WhenDtoUsed")]
@ -58,10 +60,10 @@ public class DbRepositoryTests
{ {
// Act // Act
var createdUser = useDto var createdUser = useDto
? await _userRepo.CreateAsync(Fake.UserCreateDto) ? await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto)
: await _userRepo.CreateAsync(Fake.User); : await Repo.CreateAsync(Fake.User);
var readUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id); var readUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
// Assert // Assert
Assert.Multiple(() => Assert.Multiple(() =>
@ -75,12 +77,12 @@ public class DbRepositoryTests
public async Task ReadAsync_ShouldReturnUpdated() public async Task ReadAsync_ShouldReturnUpdated()
{ {
// Arrange // Arrange
var createdUser = await _userRepo.CreateAsync(Fake.UserCreateDto); var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
var userUpdateDto = new UserUpdateDto() { Age = 10, Email = "Bar", FirstName = "Foo" }; var userUpdateDto = new UserUpdateDto() { Age = 10, Email = "Bar", FirstName = "Foo" };
// Act // Act
await _userRepo.UpdateAsync(userUpdateDto, u => u.Id == createdUser!.Id); await Repo.UpdateAsync<User, UserUpdateDto>(userUpdateDto, u => u.Id == createdUser!.Id);
var upToDateUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser!.Id); var upToDateUser = await Repo.Get<User>().Where(u => u.Id == createdUser!.Id).FirstOrDefaultAsync();
// Assert // Assert
Assert.Multiple(() => Assert.Multiple(() =>
@ -96,12 +98,12 @@ public class DbRepositoryTests
public async Task ReadAsync_ShouldNotReturnDeleted() public async Task ReadAsync_ShouldNotReturnDeleted()
{ {
// Arrange // Arrange
var createdUser = await _userRepo.CreateAsync(Fake.UserCreateDto); var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
var readUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id); var readUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
// Act // Act
await _userRepo.DeleteAsync(u => u.Id == createdUser.Id); await Repo.DeleteAsync<User>(u => u.Id == createdUser.Id);
var deletedUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id); var deletedUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
// Assert // Assert
Assert.Multiple(() => Assert.Multiple(() =>

View File

@ -1,6 +1,8 @@
namespace DigitalData.Core.Tests.Mock; using DigitalData.Core.Abstraction.Application.Repository;
public class User : UserBase namespace DigitalData.Core.Tests.Mock;
public class User : UserBase, IEntity
{ {
public required int Id { get; init; } public required int Id { get; init; }

View File

@ -1,5 +1,7 @@
namespace DigitalData.Core.Tests.Mock; using DigitalData.Core.Abstraction.Application.Repository;
public class UserCreateDto : UserBase namespace DigitalData.Core.Tests.Mock;
public class UserCreateDto : UserBase, IDto<User>
{ {
} }

View File

@ -1,5 +1,7 @@
namespace DigitalData.Core.Tests.Mock; using DigitalData.Core.Abstraction.Application.Repository;
public class UserReadDto : UserBase namespace DigitalData.Core.Tests.Mock;
public class UserReadDto : UserBase, IDto<User>
{ {
} }

View File

@ -1,5 +1,7 @@
namespace DigitalData.Core.Tests.Mock; using DigitalData.Core.Abstraction.Application.Repository;
public class UserUpdateDto : UserBase namespace DigitalData.Core.Tests.Mock;
public class UserUpdateDto : UserBase, IDto<User>
{ {
} }