< Summary

Information
Class: Pozitron.QuerySpecification.RepositoryBase<T>
Assembly: Pozitron.QuerySpecification.EntityFrameworkCore
File(s): /home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification.EntityFrameworkCore/RepositoryBase.cs
Tag: 52_11740816328
Line coverage
100%
Covered lines: 63
Uncovered lines: 0
Coverable lines: 63
Total lines: 210
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
AddAsync()100%11100%
AddRangeAsync()100%11100%
UpdateAsync()100%11100%
DeleteAsync()100%11100%
DeleteRangeAsync()100%11100%
SaveChangesAsync()100%11100%
FindAsync()100%11100%
FirstAsync()100%22100%
FirstAsync()100%22100%
FirstOrDefaultAsync()100%11100%
FirstOrDefaultAsync()100%11100%
SingleOrDefaultAsync()100%11100%
SingleOrDefaultAsync()100%11100%
ListAsync()100%11100%
ListAsync()100%11100%
ListAsync()100%11100%
CountAsync()100%11100%
CountAsync()100%11100%
CountAsync()100%11100%
AnyAsync()100%11100%
AnyAsync()100%11100%
AnyAsync()100%11100%
AsAsyncEnumerable(...)100%11100%
GenerateQuery(...)100%11100%
GenerateQuery(...)100%11100%

File(s)

/home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification.EntityFrameworkCore/RepositoryBase.cs

#LineLine coverage
 1namespace Pozitron.QuerySpecification;
 2
 3/// <summary>
 4/// Represents a base repository for accessing and modifying entities.
 5/// </summary>
 6/// <typeparam name="T">The type of the entity.</typeparam>
 7public abstract class RepositoryBase<T> : IRepositoryBase<T> where T : class
 8{
 9    private readonly DbContext _dbContext;
 10    private readonly SpecificationEvaluator _evaluator;
 11
 12    /// <summary>
 13    /// Initializes a new instance of the <see cref="RepositoryBase{T}"/> class.
 14    /// </summary>
 15    /// <param name="dbContext">The database context.</param>
 16    protected RepositoryBase(DbContext dbContext)
 4917        : this(dbContext, SpecificationEvaluator.Default)
 18    {
 4919    }
 20
 21    /// <summary>
 22    /// Initializes a new instance of the <see cref="RepositoryBase{T}"/> class with the specified specification evaluat
 23    /// </summary>
 24    /// <param name="dbContext">The database context.</param>
 25    /// <param name="specificationEvaluator">The specification evaluator.</param>
 5126    protected RepositoryBase(DbContext dbContext, SpecificationEvaluator specificationEvaluator)
 27    {
 5128        _dbContext = dbContext;
 5129        _evaluator = specificationEvaluator;
 5130    }
 31
 32    /// <inheritdoc/>
 33    public virtual async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
 34    {
 135        _dbContext.Set<T>().Add(entity);
 36
 137        await SaveChangesAsync(cancellationToken);
 38
 139        return entity;
 140    }
 41
 42    /// <inheritdoc/>
 43    public virtual async Task<IEnumerable<T>> AddRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken
 44    {
 145        _dbContext.Set<T>().AddRange(entities);
 46
 147        await SaveChangesAsync(cancellationToken);
 48
 149        return entities;
 150    }
 51
 52    /// <inheritdoc/>
 53    public virtual async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
 54    {
 55        //dbContext.Set<T>().Update(entity);
 56
 157        await SaveChangesAsync(cancellationToken);
 158    }
 59
 60    /// <inheritdoc/>
 61    public virtual async Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
 62    {
 163        _dbContext.Set<T>().Remove(entity);
 64
 165        await SaveChangesAsync(cancellationToken);
 166    }
 67
 68    /// <inheritdoc/>
 69    public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
 70    {
 171        _dbContext.Set<T>().RemoveRange(entities);
 72
 173        await SaveChangesAsync(cancellationToken);
 174    }
 75
 76    /// <inheritdoc/>
 77    public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
 78    {
 579        return await _dbContext.SaveChangesAsync(cancellationToken);
 580    }
 81
 82    /// <inheritdoc/>
 83    public virtual async ValueTask<T?> FindAsync<TId>(TId id, CancellationToken cancellationToken = default) where TId :
 84    {
 385        return await _dbContext.Set<T>().FindAsync([id], cancellationToken: cancellationToken);
 386    }
 87
 88    /// <inheritdoc/>
 89    public virtual async Task<T> FirstAsync(Specification<T> specification, CancellationToken cancellationToken = defaul
 90    {
 291        var result = await GenerateQuery(specification).FirstOrDefaultAsync(cancellationToken);
 292        return result ?? throw new EntityNotFoundException(typeof(T).Name);
 193    }
 94
 95    /// <inheritdoc/>
 96    public virtual async Task<TResult> FirstAsync<TResult>(Specification<T, TResult> specification, CancellationToken ca
 97    {
 298        var result = await GenerateQuery(specification).FirstOrDefaultAsync(cancellationToken);
 299        return result ?? throw new EntityNotFoundException(typeof(T).Name);
 1100    }
 101
 102    /// <inheritdoc/>
 103    public virtual async Task<T?> FirstOrDefaultAsync(Specification<T> specification, CancellationToken cancellationToke
 104    {
 2105        return await GenerateQuery(specification).FirstOrDefaultAsync(cancellationToken);
 2106    }
 107
 108    /// <inheritdoc/>
 109    public virtual async Task<TResult?> FirstOrDefaultAsync<TResult>(Specification<T, TResult> specification, Cancellati
 110    {
 2111        return await GenerateQuery(specification).FirstOrDefaultAsync(cancellationToken);
 2112    }
 113
 114    /// <inheritdoc/>
 115    public virtual async Task<T?> SingleOrDefaultAsync(Specification<T> specification, CancellationToken cancellationTok
 116    {
 3117        return await GenerateQuery(specification).SingleOrDefaultAsync(cancellationToken);
 2118    }
 119
 120    /// <inheritdoc/>
 121    public virtual async Task<TResult?> SingleOrDefaultAsync<TResult>(Specification<T, TResult> specification, Cancellat
 122    {
 3123        return await GenerateQuery(specification).SingleOrDefaultAsync(cancellationToken);
 2124    }
 125
 126    /// <inheritdoc/>
 127    public virtual async Task<List<T>> ListAsync(CancellationToken cancellationToken = default)
 128    {
 1129        return await _dbContext.Set<T>().ToListAsync(cancellationToken);
 1130    }
 131
 132    /// <inheritdoc/>
 133    public virtual async Task<List<T>> ListAsync(Specification<T> specification, CancellationToken cancellationToken = d
 134    {
 1135        return await GenerateQuery(specification).ToListAsync(cancellationToken);
 1136    }
 137
 138    /// <inheritdoc/>
 139    public virtual async Task<List<TResult>> ListAsync<TResult>(Specification<T, TResult> specification, CancellationTok
 140    {
 1141        return await GenerateQuery(specification).ToListAsync(cancellationToken);
 1142    }
 143
 144    /// <inheritdoc/>
 145    public virtual async Task<int> CountAsync(CancellationToken cancellationToken = default)
 146    {
 2147        return await _dbContext.Set<T>().CountAsync(cancellationToken);
 2148    }
 149
 150    /// <inheritdoc/>
 151    public virtual async Task<int> CountAsync(Specification<T> specification, CancellationToken cancellationToken = defa
 152    {
 3153        return await GenerateQuery(specification, true).CountAsync(cancellationToken);
 3154    }
 155
 156    /// <inheritdoc/>
 157    public virtual async Task<int> CountAsync<TResult>(Specification<T, TResult> specification, CancellationToken cancel
 158    {
 2159        return await GenerateQuery(specification, true).CountAsync(cancellationToken);
 2160    }
 161
 162    /// <inheritdoc/>
 163    public virtual async Task<bool> AnyAsync(CancellationToken cancellationToken = default)
 164    {
 2165        return await _dbContext.Set<T>().AnyAsync(cancellationToken);
 2166    }
 167
 168    /// <inheritdoc/>
 169    public virtual async Task<bool> AnyAsync(Specification<T> specification, CancellationToken cancellationToken = defau
 170    {
 3171        return await GenerateQuery(specification, true).AnyAsync(cancellationToken);
 3172    }
 173
 174    /// <inheritdoc/>
 175    public virtual async Task<bool> AnyAsync<TResult>(Specification<T, TResult> specification, CancellationToken cancell
 176    {
 2177        return await GenerateQuery(specification, true).AnyAsync(cancellationToken);
 2178    }
 179
 180    /// <inheritdoc/>
 181    public virtual IAsyncEnumerable<T> AsAsyncEnumerable(Specification<T> specification)
 182    {
 1183        return GenerateQuery(specification).AsAsyncEnumerable();
 184    }
 185
 186    /// <summary>
 187    /// Generates a query based on the specification.
 188    /// </summary>
 189    /// <param name="specification">The specification to evaluate.</param>
 190    /// <param name="ignorePaging">Whether to ignore paging settings (Take/Skip) defined in the specification.</param>
 191    /// <returns>The generated query.</returns>
 192    protected virtual IQueryable<T> GenerateQuery(Specification<T> specification, bool ignorePaging = false)
 193    {
 22194        var query = _evaluator.Evaluate(_dbContext.Set<T>(), specification, ignorePaging);
 22195        return query;
 196    }
 197
 198    /// <summary>
 199    /// Generates a query based on the specification.
 200    /// </summary>
 201    /// <typeparam name="TResult">The type of the result.</typeparam>
 202    /// <param name="specification">The specification to evaluate.</param>
 203    /// <param name="ignorePaging">Whether to ignore paging settings (Take/Skip) defined in the specification.</param>
 204    /// <returns>The generated query.</returns>
 205    protected virtual IQueryable<TResult> GenerateQuery<TResult>(Specification<T, TResult> specification, bool ignorePag
 206    {
 12207        var query = _evaluator.Evaluate(_dbContext.Set<T>(), specification, ignorePaging);
 12208        return query;
 209    }
 210}