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.
48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
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;
|
|
}
|
|
} |