Compare commits

...

3 Commits

Author SHA1 Message Date
0cf6d2690a chore(Infrastructure): bump to 2.5.0 2025-11-05 14:22:22 +01:00
afbbac7b81 core(Abstraction.Application): bump to 1.5.0 2025-11-05 14:21:29 +01:00
d16a4b6bb4 refactor(repository): make UpdateAsync more flexible with Action<TEntity>
- Introduced Action<TEntity>-based UpdateAsync method for more flexible updates
- Updated TDto-based UpdateAsync to delegate to the new method
- Reduced code duplication and Mapper dependency
- Preserved existing Create, Read, and Delete functionality
2025-11-05 14:20:12 +01:00
4 changed files with 129 additions and 117 deletions

View File

@ -12,9 +12,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.4.0</Version> <Version>1.5.0</Version>
<AssemblyVersion>1.4.0</AssemblyVersion> <AssemblyVersion>1.5.0</AssemblyVersion>
<FileVersion>1.4.0</FileVersion> <FileVersion>1.5.0</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,5 +1,4 @@
using DigitalData.Core.Abstractions.Interfaces; using System.Linq.Expressions;
using System.Linq.Expressions;
#if NETFRAMEWORK #if NETFRAMEWORK
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -15,90 +14,100 @@ namespace DigitalData.Core.Abstraction.Application.Repository
{ {
#endif #endif
public interface IRepository<TEntity> public interface IRepository<TEntity>
{ {
#region Create #region Create
#if NET #if NET
public public
#endif #endif
Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default); Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default);
#if NET #if NET
public public
#endif #endif
Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken cancel = default); Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken cancel = default);
#if NET #if NET
public public
#endif #endif
Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancel = default); Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancel = default);
#if NET #if NET
public public
#endif #endif
Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancel = default); Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancel = default);
#endregion Create #endregion Create
#region Read #region Read
#if NET #if NET
public public
#endif #endif
IQueryable<TEntity> Query { get; } IQueryable<TEntity> Query { get; }
#if NET #if NET
public public
#endif #endif
IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression); IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression);
#if NET #if NET
public public
#endif #endif
IEnumerable<TEntity> GetAll(); IEnumerable<TEntity> GetAll();
#if NET #if NET
public public
#endif #endif
Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancel = default); Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancel = default);
#endregion Read #endregion Read
#region Update #region Update
#if NET #if NET
public public
#endif #endif
Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default); Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
#if NET #if NET
public public
#endif #endif
Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default); Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
#endregion Update
#region Delete
#if NET
public
#endif
Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
#if NET #if NET
public public
#endif #endif
Task DeleteAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default); Task UpdateAsync(Action<TEntity> modification, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
#endregion Delete
#region Obsolete
[Obsolete("Use CreateAsync, UpdateAsync or DeleteAsync")]
#if NET #if NET
public public
#endif #endif
IQueryable<TEntity> Read(); Task UpdateAsync(Action<TEntity> modification, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
#endregion Update
[Obsolete("Use IRepository<TEntity>.Where")] #region Delete
#if NET #if NET
public public
#endif #endif
IQueryable<TEntity> ReadOnly(); Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
#endregion
} #if NET
public
#endif
Task DeleteAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
#endregion Delete
#region Obsolete
[Obsolete("Use CreateAsync, UpdateAsync or DeleteAsync")]
#if NET
public
#endif
IQueryable<TEntity> Read();
[Obsolete("Use IRepository<TEntity>.Where")]
#if NET
public
#endif
IQueryable<TEntity> ReadOnly();
#endregion
}
#if NETFRAMEWORK #if NETFRAMEWORK
} }
#endif #endif

View File

@ -21,114 +21,117 @@ namespace DigitalData.Core.Infrastructure
{ {
#endif #endif
public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbContext : DbContext where TEntity : class public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbContext : DbContext where TEntity : class
{ {
protected internal readonly TDbContext Context; protected internal readonly TDbContext Context;
protected internal readonly DbSet<TEntity> Entities; protected internal readonly DbSet<TEntity> Entities;
public IMapper public IMapper
#if NET #if NET
? ?
#endif #endif
Mapper { get; } Mapper { get; }
public DbRepository(TDbContext context, DbSetFactory<TDbContext, TEntity> factory, IMapper public DbRepository(TDbContext context, DbSetFactory<TDbContext, TEntity> factory, IMapper
#if NET #if NET
? ?
#endif #endif
mapper = null) mapper = null)
{ {
Context = context; Context = context;
Entities = factory.Create(context); Entities = factory.Create(context);
Mapper = mapper; Mapper = mapper;
} }
#region Create #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);
await Context.SaveChangesAsync(cancel); await Context.SaveChangesAsync(cancel);
return entity; return entity;
} }
public virtual async Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken cancel = default) public virtual async Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken cancel = default)
{ {
Entities.AddRange(entities); Entities.AddRange(entities);
await Context.SaveChangesAsync(cancel); await Context.SaveChangesAsync(cancel);
return entities; return entities;
} }
public virtual Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancel = default) public virtual Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancel = default)
=> CreateAsync(Mapper => CreateAsync(Mapper
#if NET #if NET
! !
#endif #endif
.Map<TEntity>(dto), cancel); .Map<TEntity>(dto), cancel);
public virtual Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancel = default) public virtual Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancel = default)
=> CreateAsync(Mapper => CreateAsync(Mapper
#if NET #if NET
! !
#endif #endif
.Map<IEnumerable<TEntity>>(dtos), cancel); .Map<IEnumerable<TEntity>>(dtos), cancel);
#endregion Create #endregion Create
#region Read #region Read
public virtual IQueryable<TEntity> Query => Entities.AsNoTracking(); public virtual IQueryable<TEntity> Query => Entities.AsNoTracking();
public virtual IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression) => Entities.AsNoTracking().Where(expression); public virtual IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression) => Entities.AsNoTracking().Where(expression);
public virtual IEnumerable<TEntity> GetAll() => Entities.AsNoTracking().ToList(); public virtual IEnumerable<TEntity> GetAll() => Entities.AsNoTracking().ToList();
public virtual async Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancel = default) => await Entities.AsNoTracking().ToListAsync(cancel); public virtual async Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancel = default) => await Entities.AsNoTracking().ToListAsync(cancel);
#endregion Read #endregion Read
#region Update #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 Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
{ => UpdateAsync(entity => Mapper
var entities = await query(Entities).ToListAsync(cancel);
for (int i = entities.Count - 1; i >= 0; i--)
{
Mapper
#if NET #if NET
! !
#endif #endif
.Map(dto, entities[i]); .Map(dto, entity), query, cancel);
Entities.Update(entities[i]);
}
await Context.SaveChangesAsync(cancel); public virtual async Task UpdateAsync(Action<TEntity> modification, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
}
#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)
{
var entities = await query(Entities).ToListAsync(cancel);
for (int i = entities.Count - 1; i >= 0; i--)
{ {
Entities.Remove(entities[i]); var entities = await query(Entities).ToListAsync(cancel);
for (int i = entities.Count - 1; i >= 0; i--)
modification.Invoke(entities[i]);
await Context.SaveChangesAsync(cancel);
} }
await Context.SaveChangesAsync(cancel); public virtual Task UpdateAsync(Action<TEntity> modification, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default)
=> UpdateAsync(modification, q => q.Where(expression), 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)
{
var entities = await query(Entities).ToListAsync(cancel);
for (int i = entities.Count - 1; i >= 0; i--)
{
Entities.Remove(entities[i]);
}
await Context.SaveChangesAsync(cancel);
}
#endregion Delete
#region Obsolete
[Obsolete("Use IRepository<TEntity>.Where")]
public virtual IQueryable<TEntity> Read() => Entities.AsQueryable();
[Obsolete("Use IRepository<TEntity>.Get")]
public virtual IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking();
#endregion
} }
#endregion Delete
#region Obsolete
[Obsolete("Use IRepository<TEntity>.Where")]
public virtual IQueryable<TEntity> Read() => Entities.AsQueryable();
[Obsolete("Use IRepository<TEntity>.Get")]
public virtual IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking();
#endregion
}
#if NETFRAMEWORK #if NETFRAMEWORK
} }
#endif #endif

View File

@ -13,9 +13,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.4.5</Version> <Version>2.5.0</Version>
<AssemblyVersion>2.4.5</AssemblyVersion> <AssemblyVersion>2.5.0</AssemblyVersion>
<FileVersion>2.4.5</FileVersion> <FileVersion>2.5.0</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>