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>
|
<PackageIcon>core_icon.png</PackageIcon>
|
||||||
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
||||||
<PackageTags>digital data core application clean architecture abstraction</PackageTags>
|
<PackageTags>digital data core application clean architecture abstraction</PackageTags>
|
||||||
<Version>1.2.1</Version>
|
<Version>1.3.0</Version>
|
||||||
<AssemblyVersion>1.2.1</AssemblyVersion>
|
<AssemblyVersion>1.3.0</AssemblyVersion>
|
||||||
<FileVersion>1.2.1</FileVersion>
|
<FileVersion>1.3.0</FileVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@ -4,27 +4,18 @@ namespace DigitalData.Core.Abstraction.Application.Repository;
|
|||||||
|
|
||||||
public static class Extensions
|
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
|
#region IRepository
|
||||||
public static IQueryable<TEntity> Get<TEntity>(this IRepository repository) where TEntity : IEntity
|
#region Read
|
||||||
=> repository.Entity<TEntity>().Get();
|
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)
|
public static IQueryable<TEntity> Where<TEntity>(this IRepository repository, Expression<Func<TEntity, bool>> expression)
|
||||||
where TEntity : IEntity
|
where TEntity : IEntity
|
||||||
=> repository.Entity<TEntity>().Where(expression);
|
=> repository.Entity<TEntity>().Where(expression);
|
||||||
|
#endregion
|
||||||
|
|
||||||
#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)
|
||||||
|
|||||||
@ -1,33 +1,44 @@
|
|||||||
using AutoMapper;
|
using System.Linq.Expressions;
|
||||||
using System.Linq.Expressions;
|
|
||||||
|
|
||||||
namespace DigitalData.Core.Abstraction.Application.Repository;
|
namespace DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
|
||||||
public interface IRepository<TEntity>
|
public interface IRepository<TEntity>
|
||||||
{
|
{
|
||||||
public IMapper Mapper { get; }
|
#region Create
|
||||||
|
|
||||||
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default);
|
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default);
|
||||||
|
|
||||||
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, 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> 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, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
|
||||||
|
|
||||||
public Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, 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(Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
|
||||||
|
|
||||||
public Task DeleteAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
|
public Task DeleteAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
|
||||||
|
#endregion Delete
|
||||||
|
|
||||||
#region Obsolete
|
#region Obsolete
|
||||||
[Obsolete("Use IRepository<TEntity>.Where")]
|
[Obsolete("Use CreateAsync, UpdateAsync or DeleteAsync")]
|
||||||
public IQueryable<TEntity> Read();
|
public IQueryable<TEntity> Read();
|
||||||
|
|
||||||
[Obsolete("Use IRepository<TEntity>.Get")]
|
[Obsolete("Use IRepository<TEntity>.Where")]
|
||||||
public IQueryable<TEntity> ReadOnly();
|
public IQueryable<TEntity> ReadOnly();
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
|
|
||||||
@ -11,15 +12,16 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
|||||||
|
|
||||||
protected internal readonly DbSet<TEntity> Entities;
|
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;
|
Context = context;
|
||||||
Entities = queryFactory(context);
|
Entities = factory.Create(context);
|
||||||
Mapper = mapper;
|
Mapper = mapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region Create
|
||||||
public virtual async Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default)
|
public virtual async Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default)
|
||||||
{
|
{
|
||||||
Entities.Add(entity);
|
Entities.Add(entity);
|
||||||
@ -34,10 +36,21 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
|||||||
return entities;
|
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 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)
|
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--)
|
for (int i = entities.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
Mapper.Map(dto, entities[i]);
|
Mapper!.Map(dto, entities[i]);
|
||||||
Entities.Update(entities[i]);
|
Entities.Update(entities[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Context.SaveChangesAsync(cancel);
|
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 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)
|
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);
|
await Context.SaveChangesAsync(cancel);
|
||||||
}
|
}
|
||||||
|
#endregion Delete
|
||||||
|
|
||||||
#region Obsolete
|
#region Obsolete
|
||||||
[Obsolete("Use IRepository<TEntity>.Where")]
|
[Obsolete("Use IRepository<TEntity>.Where")]
|
||||||
public IQueryable<TEntity> Read() => Entities.AsQueryable();
|
public virtual IQueryable<TEntity> Read() => Entities.AsQueryable();
|
||||||
|
|
||||||
[Obsolete("Use IRepository<TEntity>.Get")]
|
[Obsolete("Use IRepository<TEntity>.Get")]
|
||||||
public IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking();
|
public virtual IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking();
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,9 +15,9 @@
|
|||||||
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
||||||
<RepositoryType>digital data core abstractions clean architecture</RepositoryType>
|
<RepositoryType>digital data core abstractions clean architecture</RepositoryType>
|
||||||
<PackageTags>digital data core infrastructure clean architecture</PackageTags>
|
<PackageTags>digital data core infrastructure clean architecture</PackageTags>
|
||||||
<Version>2.3.1</Version>
|
<Version>2.4.0</Version>
|
||||||
<AssemblyVersion>2.3.1</AssemblyVersion>
|
<AssemblyVersion>2.4.0</AssemblyVersion>
|
||||||
<FileVersion>2.3.1</FileVersion>
|
<FileVersion>2.4.0</FileVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<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.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;
|
||||||
@ -63,7 +63,7 @@ public class DbRepositoryTests
|
|||||||
? await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto)
|
? await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto)
|
||||||
: await Repo.CreateAsync(Fake.User);
|
: 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
|
||||||
Assert.Multiple(() =>
|
Assert.Multiple(() =>
|
||||||
@ -82,7 +82,7 @@ public class DbRepositoryTests
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
await Repo.UpdateAsync<User, UserUpdateDto>(userUpdateDto, 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();
|
var upToDateUser = await Repo.Where<User>(u => u.Id == createdUser!.Id).FirstOrDefaultAsync();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Multiple(() =>
|
Assert.Multiple(() =>
|
||||||
@ -99,11 +99,11 @@ public class DbRepositoryTests
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
|
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
|
// Act
|
||||||
await Repo.DeleteAsync<User>(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();
|
var deletedUser = await Repo.Where<User>(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Multiple(() =>
|
Assert.Multiple(() =>
|
||||||
|
|||||||
@ -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; }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user