| | | 1 | | namespace 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> |
| | | 7 | | public abstract class RepositoryWithMapper<T> : RepositoryBase<T>, IProjectionRepository<T> where T : class |
| | | 8 | | { |
| | 51 | 9 | | 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) |
| | 48 | 16 | | : base(dbContext) |
| | | 17 | | { |
| | 48 | 18 | | } |
| | | 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) |
| | 1 | 26 | | : base(dbContext, specificationEvaluator) |
| | | 27 | | { |
| | 1 | 28 | | } |
| | | 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) |
| | 1 | 36 | | : base(dbContext) |
| | | 37 | | { |
| | 1 | 38 | | _paginationSettings = paginationSettings; |
| | 1 | 39 | | } |
| | | 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 |
| | 1 | 48 | | : base(dbContext, specificationEvaluator) |
| | | 49 | | { |
| | 1 | 50 | | _paginationSettings = paginationSettings; |
| | 1 | 51 | | } |
| | | 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 | | { |
| | 2 | 64 | | var query = GenerateQuery(specification).AsNoTracking(); |
| | | 65 | | |
| | 2 | 66 | | var projectedQuery = Map<TResult>(query); |
| | | 67 | | |
| | 2 | 68 | | var result = await projectedQuery.FirstOrDefaultAsync(cancellationToken); |
| | | 69 | | |
| | 2 | 70 | | return result ?? throw new EntityNotFoundException(typeof(T).Name); |
| | 1 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <inheritdoc/> |
| | | 74 | | public virtual async Task<TResult?> ProjectToFirstOrDefaultAsync<TResult>(Specification<T> specification, Cancellati |
| | | 75 | | { |
| | 2 | 76 | | var query = GenerateQuery(specification).AsNoTracking(); |
| | | 77 | | |
| | 2 | 78 | | var projectedQuery = Map<TResult>(query); |
| | | 79 | | |
| | 2 | 80 | | return await projectedQuery.FirstOrDefaultAsync(cancellationToken); |
| | 2 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <inheritdoc/> |
| | | 84 | | public virtual async Task<List<TResult>> ProjectToListAsync<TResult>(Specification<T> specification, CancellationTok |
| | | 85 | | { |
| | 1 | 86 | | var query = GenerateQuery(specification).AsNoTracking(); |
| | | 87 | | |
| | 1 | 88 | | var projectedQuery = Map<TResult>(query); |
| | | 89 | | |
| | 1 | 90 | | return await projectedQuery.ToListAsync(cancellationToken); |
| | 1 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <inheritdoc/> |
| | | 94 | | public virtual async Task<PagedResult<TResult>> ProjectToListAsync<TResult>(Specification<T> specification, PagingFi |
| | | 95 | | { |
| | 2 | 96 | | var query = GenerateQuery(specification, true).AsNoTracking(); |
| | 2 | 97 | | var projectedQuery = Map<TResult>(query); |
| | | 98 | | |
| | 2 | 99 | | var count = await projectedQuery.CountAsync(cancellationToken); |
| | 2 | 100 | | var pagination = new Pagination(_paginationSettings, count, filter); |
| | | 101 | | |
| | 2 | 102 | | projectedQuery = projectedQuery.ApplyPaging(pagination); |
| | 2 | 103 | | var data = await projectedQuery.ToListAsync(cancellationToken); |
| | | 104 | | |
| | 2 | 105 | | return new PagedResult<TResult>(data, pagination); |
| | 2 | 106 | | } |
| | | 107 | | } |