< Summary

Information
Class: Pozitron.QuerySpecification.RepositoryBase<T>
Assembly: Pozitron.QuerySpecification.EntityFrameworkCore
File(s): /home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification.EntityFrameworkCore/RepositoryBase.cs
Tag: 85_23650561754
Line coverage
100%
Covered lines: 69
Uncovered lines: 0
Coverable lines: 69
Total lines: 228
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 Cyclomatic complexity NPath complexity Sequence 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%
ToListAsync()100%11100%
ToListAsync()100%11100%
ToListAsync()100%11100%
ToDictionaryAsync()100%11100%
ToDictionaryAsync()100%11100%
ToDictionaryAsync()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)
 5417        : this(dbContext, SpecificationEvaluator.Default)
 18    {
 5419    }
 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>
 5626    protected RepositoryBase(DbContext dbContext, SpecificationEvaluator specificationEvaluator)
 27    {
 5628        _dbContext = dbContext;
 5629        _evaluator = specificationEvaluator;
 5630    }
 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>> ToListAsync(CancellationToken cancellationToken = default)
 128    {
 1129        return await _dbContext.Set<T>().ToListAsync(cancellationToken);
 1130    }
 131
 132    /// <inheritdoc/>
 133    public virtual async Task<List<T>> ToListAsync(Specification<T> specification, CancellationToken cancellationToken =
 134    {
 1135        return await GenerateQuery(specification).ToListAsync(cancellationToken);
 1136    }
 137
 138    /// <inheritdoc/>
 139    public virtual async Task<List<TResult>> ToListAsync<TResult>(Specification<T, TResult> specification, CancellationT
 140    {
 1141        return await GenerateQuery(specification).ToListAsync(cancellationToken);
 1142    }
 143
 144    /// <inheritdoc/>
 145    public async Task<Dictionary<TKey, T>> ToDictionaryAsync<TKey>(Func<T, TKey> keySelector, CancellationToken cancella
 146    {
 1147        return await _dbContext.Set<T>().ToDictionaryAsync(keySelector, cancellationToken);
 1148    }
 149
 150    /// <inheritdoc/>
 151    public async Task<Dictionary<TKey, T>> ToDictionaryAsync<TKey>(Specification<T> specification, Func<T, TKey> keySele
 152    {
 1153        return await GenerateQuery(specification).ToDictionaryAsync(keySelector, cancellationToken);
 1154    }
 155
 156    /// <inheritdoc/>
 157    public async Task<Dictionary<TKey, TResult>> ToDictionaryAsync<TResult, TKey>(Specification<T, TResult> specificatio
 158    {
 1159        return await GenerateQuery(specification).ToDictionaryAsync(keySelector, cancellationToken);
 1160    }
 161
 162    /// <inheritdoc/>
 163    public virtual async Task<int> CountAsync(CancellationToken cancellationToken = default)
 164    {
 2165        return await _dbContext.Set<T>().CountAsync(cancellationToken);
 2166    }
 167
 168    /// <inheritdoc/>
 169    public virtual async Task<int> CountAsync(Specification<T> specification, CancellationToken cancellationToken = defa
 170    {
 3171        return await GenerateQuery(specification, true).CountAsync(cancellationToken);
 3172    }
 173
 174    /// <inheritdoc/>
 175    public virtual async Task<int> CountAsync<TResult>(Specification<T, TResult> specification, CancellationToken cancel
 176    {
 2177        return await GenerateQuery(specification, true).CountAsync(cancellationToken);
 2178    }
 179
 180    /// <inheritdoc/>
 181    public virtual async Task<bool> AnyAsync(CancellationToken cancellationToken = default)
 182    {
 2183        return await _dbContext.Set<T>().AnyAsync(cancellationToken);
 2184    }
 185
 186    /// <inheritdoc/>
 187    public virtual async Task<bool> AnyAsync(Specification<T> specification, CancellationToken cancellationToken = defau
 188    {
 3189        return await GenerateQuery(specification, true).AnyAsync(cancellationToken);
 3190    }
 191
 192    /// <inheritdoc/>
 193    public virtual async Task<bool> AnyAsync<TResult>(Specification<T, TResult> specification, CancellationToken cancell
 194    {
 2195        return await GenerateQuery(specification, true).AnyAsync(cancellationToken);
 2196    }
 197
 198    /// <inheritdoc/>
 199    public virtual IAsyncEnumerable<T> AsAsyncEnumerable(Specification<T> specification)
 200    {
 1201        return GenerateQuery(specification).AsAsyncEnumerable();
 202    }
 203
 204    /// <summary>
 205    /// Generates a query based on the specification.
 206    /// </summary>
 207    /// <param name="specification">The specification to evaluate.</param>
 208    /// <param name="ignorePaging">Whether to ignore paging settings (Take/Skip) defined in the specification.</param>
 209    /// <returns>The generated query.</returns>
 210    protected virtual IQueryable<T> GenerateQuery(Specification<T> specification, bool ignorePaging = false)
 211    {
 25212        var query = _evaluator.Evaluate(_dbContext.Set<T>(), specification, ignorePaging);
 25213        return query;
 214    }
 215
 216    /// <summary>
 217    /// Generates a query based on the specification.
 218    /// </summary>
 219    /// <typeparam name="TResult">The type of the result.</typeparam>
 220    /// <param name="specification">The specification to evaluate.</param>
 221    /// <param name="ignorePaging">Whether to ignore paging settings (Take/Skip) defined in the specification.</param>
 222    /// <returns>The generated query.</returns>
 223    protected virtual IQueryable<TResult> GenerateQuery<TResult>(Specification<T, TResult> specification, bool ignorePag
 224    {
 13225        var query = _evaluator.Evaluate(_dbContext.Set<T>(), specification, ignorePaging);
 13226        return query;
 227    }
 228}