Refactor repository pattern and mark methods obsolete
Updated `IRepository<TEntity>` to introduce new reading methods and mark existing ones as obsolete. The `CRUDRepository` class is now deprecated, encouraging a transition to alternative implementations. Significant changes in `DbRepository<TDbContext, TEntity>` include the removal of old read methods in favor of a new `Read` method returning `IReadQuery<TEntity>`. Added a `ReadQuery<TEntity>` class to enhance querying capabilities with a fluent API. Obsolete methods are organized under a `#region Obsolete` directive for better management.
This commit is contained in:
parent
5995b334eb
commit
f7e2bb2434
@ -16,6 +16,7 @@ public interface IRepository<TEntity>
|
||||
|
||||
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellation = default);
|
||||
|
||||
#region Obsolete
|
||||
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
|
||||
public Task<IEnumerable<TEntity>> ReadAllAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken cancellation = default);
|
||||
|
||||
@ -27,4 +28,5 @@ public interface IRepository<TEntity>
|
||||
|
||||
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
|
||||
public Task<TDto?> ReadOrDefaultAsync<TDto>(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken cancellation = default);
|
||||
#endregion
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@ namespace DigitalData.Core.Infrastructure
|
||||
/// This repository abstracts the common database operations, offering an asynchronous API to work with the entity's data.
|
||||
/// It leverages the EF Core's DbContext and DbSet to perform these operations.
|
||||
/// </remarks>
|
||||
[Obsolete("Use Repository")]
|
||||
public class CRUDRepository<TEntity, TId, TDbContext> : ICRUDRepository<TEntity, TId>
|
||||
where TEntity : class
|
||||
where TDbContext : DbContext
|
||||
|
||||
@ -33,24 +33,7 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
||||
return entities;
|
||||
}
|
||||
|
||||
public virtual async Task<IEnumerable<TEntity>> ReadAllAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default)
|
||||
=> expression is null
|
||||
? await Entities.AsNoTracking().ToListAsync(ct)
|
||||
: await Entities.AsNoTracking().Where(expression).ToListAsync(ct);
|
||||
|
||||
public virtual async Task<TEntity?> ReadOrDefaultAsync(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default)
|
||||
=> single
|
||||
? await Entities.AsNoTracking().Where(expression).SingleOrDefaultAsync(ct)
|
||||
: await Entities.AsNoTracking().Where(expression).FirstOrDefaultAsync(ct);
|
||||
|
||||
public virtual async Task<IEnumerable<TDto>> ReadAllAsync<TDto>(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default)
|
||||
=> Mapper.Map<TDto>(await ReadAllAsync(expression, ct));
|
||||
|
||||
public virtual async Task<TDto?> ReadOrDefaultAsync<TDto>(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default)
|
||||
{
|
||||
var entity = await ReadOrDefaultAsync(expression, single, ct);
|
||||
return entity is null ? default : Mapper.Map<TDto>(entity);
|
||||
}
|
||||
public IReadQuery<TEntity> Read(params Expression<Func<TEntity, bool>>[] expressions) => new ReadQuery<TEntity>(Entities.AsNoTracking()).Where(expressions);
|
||||
|
||||
public virtual async Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default)
|
||||
{
|
||||
@ -76,4 +59,29 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
||||
|
||||
await Context.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
#region Obsolete
|
||||
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
|
||||
public virtual async Task<IEnumerable<TEntity>> ReadAllAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default)
|
||||
=> expression is null
|
||||
? await Entities.AsNoTracking().ToListAsync(ct)
|
||||
: await Entities.AsNoTracking().Where(expression).ToListAsync(ct);
|
||||
|
||||
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
|
||||
public virtual async Task<TEntity?> ReadOrDefaultAsync(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default)
|
||||
=> single
|
||||
? await Entities.AsNoTracking().Where(expression).SingleOrDefaultAsync(ct)
|
||||
: await Entities.AsNoTracking().Where(expression).FirstOrDefaultAsync(ct);
|
||||
|
||||
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
|
||||
public virtual async Task<IEnumerable<TDto>> ReadAllAsync<TDto>(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default)
|
||||
=> Mapper.Map<TDto>(await ReadAllAsync(expression, ct));
|
||||
|
||||
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
|
||||
public virtual async Task<TDto?> ReadOrDefaultAsync<TDto>(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default)
|
||||
{
|
||||
var entity = await ReadOrDefaultAsync(expression, single, ct);
|
||||
return entity is null ? default : Mapper.Map<TDto>(entity);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
48
DigitalData.Core.Infrastructure/ReadQuery.cs
Normal file
48
DigitalData.Core.Infrastructure/ReadQuery.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using DigitalData.Core.Application.Interfaces.Repository;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace DigitalData.Core.Infrastructure;
|
||||
|
||||
public sealed record ReadQuery<TEntity> : IReadQuery<TEntity>
|
||||
{
|
||||
private IQueryable<TEntity> _query;
|
||||
|
||||
internal ReadQuery(IQueryable<TEntity> queryable)
|
||||
{
|
||||
_query = queryable;
|
||||
}
|
||||
|
||||
public TEntity First() => _query.First();
|
||||
|
||||
public Task<TEntity> FirstAsync(CancellationToken cancellation = default) => _query.FirstAsync(cancellation);
|
||||
|
||||
public TEntity? FirstOrDefault() => _query.FirstOrDefault();
|
||||
|
||||
|
||||
public Task<TEntity?> FirstOrDefaultAsync(CancellationToken cancellation = default) => _query.FirstOrDefaultAsync(cancellation);
|
||||
|
||||
|
||||
public TEntity Single() => _query.Single();
|
||||
|
||||
|
||||
public Task<TEntity> SingleAsync(CancellationToken cancellation = default) => _query.SingleAsync(cancellation);
|
||||
|
||||
|
||||
public TEntity? SingleOrDefault() => _query.SingleOrDefault();
|
||||
|
||||
|
||||
public Task<TEntity?> SingleOrDefaultAsync(CancellationToken cancellation = default) => _query.SingleOrDefaultAsync(cancellation);
|
||||
|
||||
|
||||
public IEnumerable<TEntity> ToList() => _query.ToList();
|
||||
|
||||
|
||||
public async Task<IEnumerable<TEntity>> ToListAsync(CancellationToken cancellation = default) => await _query.ToListAsync(cancellation);
|
||||
|
||||
public IReadQuery<TEntity> Where(Expression<Func<TEntity, bool>> expression)
|
||||
{
|
||||
_query = _query.Where(expression);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user