Compare commits
7 Commits
84cd52cc45
...
db8c41368d
| Author | SHA1 | Date | |
|---|---|---|---|
| db8c41368d | |||
| 8743325067 | |||
| be96bd0c07 | |||
| b181e1543f | |||
| 7c2a165479 | |||
| 90a12f52bc | |||
| 33f7ced3b2 |
@ -19,37 +19,52 @@ public static class Extensions
|
||||
#endregion
|
||||
|
||||
#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
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
#endregion Create
|
||||
|
||||
#region Update
|
||||
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);
|
||||
|
||||
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);
|
||||
#endregion
|
||||
|
||||
#region Delete
|
||||
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);
|
||||
|
||||
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);
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
@ -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
|
||||
{
|
||||
}
|
||||
@ -11,7 +11,7 @@ public interface IRepository<TEntity>
|
||||
|
||||
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();
|
||||
|
||||
@ -34,5 +34,5 @@ public interface IRepository<TEntity>
|
||||
|
||||
public interface IRepository
|
||||
{
|
||||
public IRepository<TEntity> Entity<TEntity>();
|
||||
public IRepository<TEntity> Entity<TEntity>() where TEntity : IEntity;
|
||||
}
|
||||
@ -1,7 +1,6 @@
|
||||
using AutoMapper;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace DigitalData.Core.Infrastructure;
|
||||
@ -35,7 +34,7 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
||||
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();
|
||||
|
||||
@ -86,5 +85,5 @@ public class DbRepository : IRepository
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
public IRepository<TEntity> Entity<TEntity>() => _factory.Get<TEntity>();
|
||||
public IRepository<TEntity> Entity<TEntity>() where TEntity : IEntity => _factory.Get<TEntity>();
|
||||
}
|
||||
@ -1,19 +1,120 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Infrastructure.Factory;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DigitalData.Core.Infrastructure;
|
||||
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static EntityConfigurationOptions<TEntity> AddDbRepository<TDbContext, TEntity>(this IServiceCollection services, Func<TDbContext, DbSet<TEntity>> queryFactory)
|
||||
where TDbContext : DbContext
|
||||
public static IServiceCollection AddDbRepository(this IServiceCollection services, Action<RepositoryConfiguration> options)
|
||||
{
|
||||
// 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
|
||||
{
|
||||
services
|
||||
.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>()
|
||||
.AddSingleton(queryFactory);
|
||||
|
||||
return new EntityConfigurationOptions<TEntity>(services);
|
||||
create ??= ctx => ctx.Set<TEntity>();
|
||||
services.AddSingleton(_ => new DbSetFactory<TDbContext, TEntity>(create));
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -26,7 +26,6 @@
|
||||
<ProjectReference Include="..\DigitalData.Core.Application\DigitalData.Core.Application.csproj" />
|
||||
<ProjectReference Include="..\DigitalData.Core.Client\DigitalData.Core.Client.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.Security\DigitalData.Core.Security.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -1,19 +1,18 @@
|
||||
namespace DigitalData.Core.Tests.Infrastructure;
|
||||
|
||||
using DigitalData.Core.Infrastructure;
|
||||
using DigitalData.Core.Tests.Mock;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System.Reflection;
|
||||
using DigitalData.Core.Infrastructure.AutoMapper;
|
||||
using DigitalData.Core.Application.Interfaces.Repository;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Infrastructure;
|
||||
|
||||
public class DbRepositoryTests
|
||||
{
|
||||
private IHost _host;
|
||||
|
||||
private IRepository<User> _userRepo;
|
||||
private IRepository Repo;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
@ -22,13 +21,16 @@ public class DbRepositoryTests
|
||||
|
||||
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());
|
||||
|
||||
_host = builder.Build();
|
||||
|
||||
_userRepo = _host.Services.GetRequiredService<IRepository<User>>();
|
||||
Repo = _host.Services.GetRequiredService<IRepository>();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
@ -47,9 +49,9 @@ public class DbRepositoryTests
|
||||
|
||||
// Act & Assert
|
||||
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
|
||||
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate()));
|
||||
Assert.DoesNotThrowAsync(async () => await Repo.CreateAsync(faker.Generate()));
|
||||
}
|
||||
|
||||
[TestCase(true, TestName = "WhenDtoUsed")]
|
||||
@ -58,10 +60,10 @@ public class DbRepositoryTests
|
||||
{
|
||||
// Act
|
||||
var createdUser = useDto
|
||||
? await _userRepo.CreateAsync(Fake.UserCreateDto)
|
||||
: await _userRepo.CreateAsync(Fake.User);
|
||||
? await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto)
|
||||
: 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.Multiple(() =>
|
||||
@ -75,12 +77,12 @@ public class DbRepositoryTests
|
||||
public async Task ReadAsync_ShouldReturnUpdated()
|
||||
{
|
||||
// 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" };
|
||||
|
||||
// Act
|
||||
await _userRepo.UpdateAsync(userUpdateDto, u => u.Id == createdUser!.Id);
|
||||
var upToDateUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser!.Id);
|
||||
await Repo.UpdateAsync<User, UserUpdateDto>(userUpdateDto, u => u.Id == createdUser!.Id);
|
||||
var upToDateUser = await Repo.Get<User>().Where(u => u.Id == createdUser!.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
@ -96,12 +98,12 @@ public class DbRepositoryTests
|
||||
public async Task ReadAsync_ShouldNotReturnDeleted()
|
||||
{
|
||||
// Arrange
|
||||
var createdUser = await _userRepo.CreateAsync(Fake.UserCreateDto);
|
||||
var readUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
|
||||
var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
|
||||
var readUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Act
|
||||
await _userRepo.DeleteAsync(u => u.Id == createdUser.Id);
|
||||
var deletedUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
|
||||
await Repo.DeleteAsync<User>(u => u.Id == createdUser.Id);
|
||||
var deletedUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
|
||||
@ -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; }
|
||||
|
||||
|
||||
@ -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>
|
||||
{
|
||||
}
|
||||
|
||||
@ -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>
|
||||
{
|
||||
}
|
||||
|
||||
@ -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>
|
||||
{
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user