< Summary

Information
Class: Pozitron.QuerySpecification.RepositoryWithMapper<T>
Assembly: Pozitron.QuerySpecification.EntityFrameworkCore
File(s): /home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification.EntityFrameworkCore/RepositoryWithMapper.cs
Tag: 52_11740816328
Line coverage
100%
Covered lines: 32
Uncovered lines: 0
Coverable lines: 32
Total lines: 107
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
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%
.ctor(...)100%11100%
.ctor(...)100%11100%
ProjectToFirstAsync()100%22100%
ProjectToFirstOrDefaultAsync()100%11100%
ProjectToListAsync()100%11100%
ProjectToListAsync()100%11100%

File(s)

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

#LineLine coverage
 1namespace Pozitron.QuerySpecification;
 2
 3/// <summary>
 4/// Represents a repository with mapping capabilities for projecting entities to different result types.
 5/// </summary>
 6/// <typeparam name="T">The type of the entity.</typeparam>
 7public abstract class RepositoryWithMapper<T> : RepositoryBase<T>, IProjectionRepository<T> where T : class
 8{
 519    private readonly PaginationSettings _paginationSettings = PaginationSettings.Default;
 10
 11    /// <summary>
 12    /// Initializes a new instance of the <see cref="RepositoryWithMapper{T}"/> class.
 13    /// </summary>
 14    /// <param name="dbContext">The database context.</param>
 15    protected RepositoryWithMapper(DbContext dbContext)
 4816        : base(dbContext)
 17    {
 4818    }
 19
 20    /// <summary>
 21    /// Initializes a new instance of the <see cref="RepositoryWithMapper{T}"/> class with the specified specification e
 22    /// </summary>
 23    /// <param name="dbContext">The database context.</param>
 24    /// <param name="specificationEvaluator">The specification evaluator.</param>
 25    protected RepositoryWithMapper(DbContext dbContext, SpecificationEvaluator specificationEvaluator)
 126        : base(dbContext, specificationEvaluator)
 27    {
 128    }
 29
 30    /// <summary>
 31    /// Initializes a new instance of the <see cref="RepositoryWithMapper{T}"/> class with the specified pagination sett
 32    /// </summary>
 33    /// <param name="dbContext">The database context.</param>
 34    /// <param name="paginationSettings">The pagination settings.</param>
 35    protected RepositoryWithMapper(DbContext dbContext, PaginationSettings paginationSettings)
 136        : base(dbContext)
 37    {
 138        _paginationSettings = paginationSettings;
 139    }
 40
 41    /// <summary>
 42    /// Initializes a new instance of the <see cref="RepositoryWithMapper{T}"/> class with the specified specification e
 43    /// </summary>
 44    /// <param name="dbContext">The database context.</param>
 45    /// <param name="specificationEvaluator">The specification evaluator.</param>
 46    /// <param name="paginationSettings">The pagination settings.</param>
 47    protected RepositoryWithMapper(DbContext dbContext, SpecificationEvaluator specificationEvaluator, PaginationSetting
 148        : base(dbContext, specificationEvaluator)
 49    {
 150        _paginationSettings = paginationSettings;
 151    }
 52
 53    /// <summary>
 54    /// Maps the source query to the result type.
 55    /// </summary>
 56    /// <typeparam name="TResult">The type of the result.</typeparam>
 57    /// <param name="source">The source query.</param>
 58    /// <returns>The mapped query.</returns>
 59    protected abstract IQueryable<TResult> Map<TResult>(IQueryable<T> source);
 60
 61    /// <inheritdoc/>
 62    public virtual async Task<TResult> ProjectToFirstAsync<TResult>(Specification<T> specification, CancellationToken ca
 63    {
 264        var query = GenerateQuery(specification).AsNoTracking();
 65
 266        var projectedQuery = Map<TResult>(query);
 67
 268        var result = await projectedQuery.FirstOrDefaultAsync(cancellationToken);
 69
 270        return result ?? throw new EntityNotFoundException(typeof(T).Name);
 171    }
 72
 73    /// <inheritdoc/>
 74    public virtual async Task<TResult?> ProjectToFirstOrDefaultAsync<TResult>(Specification<T> specification, Cancellati
 75    {
 276        var query = GenerateQuery(specification).AsNoTracking();
 77
 278        var projectedQuery = Map<TResult>(query);
 79
 280        return await projectedQuery.FirstOrDefaultAsync(cancellationToken);
 281    }
 82
 83    /// <inheritdoc/>
 84    public virtual async Task<List<TResult>> ProjectToListAsync<TResult>(Specification<T> specification, CancellationTok
 85    {
 186        var query = GenerateQuery(specification).AsNoTracking();
 87
 188        var projectedQuery = Map<TResult>(query);
 89
 190        return await projectedQuery.ToListAsync(cancellationToken);
 191    }
 92
 93    /// <inheritdoc/>
 94    public virtual async Task<PagedResult<TResult>> ProjectToListAsync<TResult>(Specification<T> specification, PagingFi
 95    {
 296        var query = GenerateQuery(specification, true).AsNoTracking();
 297        var projectedQuery = Map<TResult>(query);
 98
 299        var count = await projectedQuery.CountAsync(cancellationToken);
 2100        var pagination = new Pagination(_paginationSettings, count, filter);
 101
 2102        projectedQuery = projectedQuery.ApplyPaging(pagination);
 2103        var data = await projectedQuery.ToListAsync(cancellationToken);
 104
 2105        return new PagedResult<TResult>(data, pagination);
 2106    }
 107}