Compare commits
11 Commits
db8c41368d
...
ad734f77f9
| Author | SHA1 | Date | |
|---|---|---|---|
| ad734f77f9 | |||
| 4c003745ff | |||
| 89de237aff | |||
| a2567791b7 | |||
| 4526ba189a | |||
| f544ea4887 | |||
| 173c4fdbc4 | |||
| babddfff83 | |||
| 56b467ddfc | |||
| 63c97b4dc7 | |||
| e2853b64d1 |
@ -14,9 +14,9 @@
|
||||
<PackageIcon>core_icon.png</PackageIcon>
|
||||
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
||||
<PackageTags>digital data core application clean architecture abstraction</PackageTags>
|
||||
<Version>1.2.1</Version>
|
||||
<AssemblyVersion>1.2.1</AssemblyVersion>
|
||||
<FileVersion>1.2.1</FileVersion>
|
||||
<Version>1.3.0</Version>
|
||||
<AssemblyVersion>1.3.0</AssemblyVersion>
|
||||
<FileVersion>1.3.0</FileVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -4,27 +4,18 @@ namespace DigitalData.Core.Abstraction.Application.Repository;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
#region Create
|
||||
public static Task<TEntity> CreateAsync<TEntity, TDto>(this IRepository<TEntity> repository, TDto dto, CancellationToken ct = default)
|
||||
{
|
||||
var entity = repository.Mapper.Map<TEntity>(dto);
|
||||
return repository.CreateAsync(entity, ct);
|
||||
}
|
||||
|
||||
public static Task<IEnumerable<TEntity>> CreateAsync<TEntity, TDto>(this IRepository<TEntity> repository, IEnumerable<TDto> dtos, CancellationToken ct = default)
|
||||
{
|
||||
var entities = dtos.Select(dto => repository.Mapper.Map<TEntity>(dto));
|
||||
return repository.CreateAsync(entities, ct);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region IRepository
|
||||
public static IQueryable<TEntity> Get<TEntity>(this IRepository repository) where TEntity : IEntity
|
||||
=> repository.Entity<TEntity>().Get();
|
||||
#region Read
|
||||
public static IEnumerable<TEntity> GetAll<TEntity>(this IRepository repository) where TEntity : IEntity
|
||||
=> repository.Entity<TEntity>().GetAll();
|
||||
|
||||
public static Task<IEnumerable<TEntity>> GetAllAsync<TEntity>(this IRepository repository, CancellationToken cancel = default) where TEntity : IEntity
|
||||
=> repository.Entity<TEntity>().GetAllAsync(cancel);
|
||||
|
||||
public static IQueryable<TEntity> Where<TEntity>(this IRepository repository, Expression<Func<TEntity, bool>> expression)
|
||||
where TEntity : IEntity
|
||||
=> repository.Entity<TEntity>().Where(expression);
|
||||
#endregion
|
||||
|
||||
#region Create
|
||||
public static Task<TEntity> CreateAsync<TEntity>(this IRepository repository, TEntity entity, CancellationToken cancel = default)
|
||||
|
||||
@ -1,33 +1,44 @@
|
||||
using AutoMapper;
|
||||
using System.Linq.Expressions;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace DigitalData.Core.Abstraction.Application.Repository;
|
||||
|
||||
public interface IRepository<TEntity>
|
||||
{
|
||||
public IMapper Mapper { get; }
|
||||
|
||||
#region Create
|
||||
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default);
|
||||
|
||||
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken cancel = default);
|
||||
|
||||
public Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancel = default);
|
||||
|
||||
public Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancel = default);
|
||||
#endregion Create
|
||||
|
||||
#region Read
|
||||
public IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression);
|
||||
|
||||
public IQueryable<TEntity> Get();
|
||||
public IEnumerable<TEntity> GetAll();
|
||||
|
||||
public Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancel = default);
|
||||
#endregion Read
|
||||
|
||||
#region Update
|
||||
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
|
||||
|
||||
public Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
|
||||
#endregion Update
|
||||
|
||||
#region Delete
|
||||
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
|
||||
|
||||
public Task DeleteAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
|
||||
#endregion Delete
|
||||
|
||||
#region Obsolete
|
||||
[Obsolete("Use IRepository<TEntity>.Where")]
|
||||
[Obsolete("Use CreateAsync, UpdateAsync or DeleteAsync")]
|
||||
public IQueryable<TEntity> Read();
|
||||
|
||||
[Obsolete("Use IRepository<TEntity>.Get")]
|
||||
[Obsolete("Use IRepository<TEntity>.Where")]
|
||||
public IQueryable<TEntity> ReadOnly();
|
||||
#endregion
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using AutoMapper;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Infrastructure.Factory;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
@ -11,15 +12,16 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
||||
|
||||
protected internal readonly DbSet<TEntity> Entities;
|
||||
|
||||
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 = null)
|
||||
{
|
||||
Context = context;
|
||||
Entities = queryFactory(context);
|
||||
Entities = factory.Create(context);
|
||||
Mapper = mapper;
|
||||
}
|
||||
|
||||
#region Create
|
||||
public virtual async Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default)
|
||||
{
|
||||
Entities.Add(entity);
|
||||
@ -34,10 +36,21 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
||||
return entities;
|
||||
}
|
||||
|
||||
public IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression) => Entities.AsQueryable().Where(expression);
|
||||
public virtual Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancel = default) => CreateAsync(Mapper!.Map<TEntity>(dto), cancel);
|
||||
|
||||
public IQueryable<TEntity> Get() => Entities.AsNoTracking();
|
||||
public virtual Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancel = default)
|
||||
=> CreateAsync(Mapper!.Map<IEnumerable<TEntity>>(dtos), cancel);
|
||||
#endregion Create
|
||||
|
||||
#region Read
|
||||
public virtual IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression) => Entities.AsNoTracking().Where(expression);
|
||||
|
||||
public virtual IEnumerable<TEntity> GetAll() => Entities.AsNoTracking().ToList();
|
||||
|
||||
public virtual async Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancel = default) => await Entities.AsNoTracking().ToListAsync(cancel);
|
||||
#endregion Read
|
||||
|
||||
#region Update
|
||||
public virtual Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default) => UpdateAsync(dto, q => q.Where(expression), cancel);
|
||||
|
||||
public virtual async Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
|
||||
@ -46,13 +59,15 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
||||
|
||||
for (int i = entities.Count - 1; i >= 0; i--)
|
||||
{
|
||||
Mapper.Map(dto, entities[i]);
|
||||
Mapper!.Map(dto, entities[i]);
|
||||
Entities.Update(entities[i]);
|
||||
}
|
||||
|
||||
await Context.SaveChangesAsync(cancel);
|
||||
}
|
||||
#endregion Update
|
||||
|
||||
#region Delete
|
||||
public virtual Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default) => DeleteAsync(q => q.Where(expression), cancel);
|
||||
|
||||
public virtual async Task DeleteAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
|
||||
@ -66,13 +81,14 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
||||
|
||||
await Context.SaveChangesAsync(cancel);
|
||||
}
|
||||
#endregion Delete
|
||||
|
||||
#region Obsolete
|
||||
[Obsolete("Use IRepository<TEntity>.Where")]
|
||||
public IQueryable<TEntity> Read() => Entities.AsQueryable();
|
||||
public virtual IQueryable<TEntity> Read() => Entities.AsQueryable();
|
||||
|
||||
[Obsolete("Use IRepository<TEntity>.Get")]
|
||||
public IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking();
|
||||
public virtual IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking();
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
||||
@ -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>();
|
||||
|
||||
@ -15,9 +15,9 @@
|
||||
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
||||
<RepositoryType>digital data core abstractions clean architecture</RepositoryType>
|
||||
<PackageTags>digital data core infrastructure clean architecture</PackageTags>
|
||||
<Version>2.3.1</Version>
|
||||
<AssemblyVersion>2.3.1</AssemblyVersion>
|
||||
<FileVersion>2.3.1</FileVersion>
|
||||
<Version>2.4.0</Version>
|
||||
<AssemblyVersion>2.4.0</AssemblyVersion>
|
||||
<FileVersion>2.4.0</FileVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -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;
|
||||
@ -63,7 +63,7 @@ public class DbRepositoryTests
|
||||
? await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto)
|
||||
: await Repo.CreateAsync(Fake.User);
|
||||
|
||||
var readUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
var readUser = await Repo.Where<User>(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
@ -82,7 +82,7 @@ public class DbRepositoryTests
|
||||
|
||||
// Act
|
||||
await Repo.UpdateAsync<User, UserUpdateDto>(userUpdateDto, u => u.Id == createdUser!.Id);
|
||||
var upToDateUser = await Repo.Get<User>().Where(u => u.Id == createdUser!.Id).FirstOrDefaultAsync();
|
||||
var upToDateUser = await Repo.Where<User>(u => u.Id == createdUser!.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
@ -99,11 +99,11 @@ public class DbRepositoryTests
|
||||
{
|
||||
// Arrange
|
||||
var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
|
||||
var readUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
var readUser = await Repo.Where<User>(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Act
|
||||
await Repo.DeleteAsync<User>(u => u.Id == createdUser.Id);
|
||||
var deletedUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
var deletedUser = await Repo.Where<User>(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
|
||||
@ -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