| | | 1 | | namespace 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> |
| | | 7 | | public 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) |
| | 49 | 17 | | : this(dbContext, SpecificationEvaluator.Default) |
| | | 18 | | { |
| | 49 | 19 | | } |
| | | 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> |
| | 51 | 26 | | protected RepositoryBase(DbContext dbContext, SpecificationEvaluator specificationEvaluator) |
| | | 27 | | { |
| | 51 | 28 | | _dbContext = dbContext; |
| | 51 | 29 | | _evaluator = specificationEvaluator; |
| | 51 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc/> |
| | | 33 | | public virtual async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default) |
| | | 34 | | { |
| | 1 | 35 | | _dbContext.Set<T>().Add(entity); |
| | | 36 | | |
| | 1 | 37 | | await SaveChangesAsync(cancellationToken); |
| | | 38 | | |
| | 1 | 39 | | return entity; |
| | 1 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc/> |
| | | 43 | | public virtual async Task<IEnumerable<T>> AddRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken |
| | | 44 | | { |
| | 1 | 45 | | _dbContext.Set<T>().AddRange(entities); |
| | | 46 | | |
| | 1 | 47 | | await SaveChangesAsync(cancellationToken); |
| | | 48 | | |
| | 1 | 49 | | return entities; |
| | 1 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc/> |
| | | 53 | | public virtual async Task UpdateAsync(T entity, CancellationToken cancellationToken = default) |
| | | 54 | | { |
| | | 55 | | //dbContext.Set<T>().Update(entity); |
| | | 56 | | |
| | 1 | 57 | | await SaveChangesAsync(cancellationToken); |
| | 1 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc/> |
| | | 61 | | public virtual async Task DeleteAsync(T entity, CancellationToken cancellationToken = default) |
| | | 62 | | { |
| | 1 | 63 | | _dbContext.Set<T>().Remove(entity); |
| | | 64 | | |
| | 1 | 65 | | await SaveChangesAsync(cancellationToken); |
| | 1 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc/> |
| | | 69 | | public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default) |
| | | 70 | | { |
| | 1 | 71 | | _dbContext.Set<T>().RemoveRange(entities); |
| | | 72 | | |
| | 1 | 73 | | await SaveChangesAsync(cancellationToken); |
| | 1 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc/> |
| | | 77 | | public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default) |
| | | 78 | | { |
| | 5 | 79 | | return await _dbContext.SaveChangesAsync(cancellationToken); |
| | 5 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc/> |
| | | 83 | | public virtual async ValueTask<T?> FindAsync<TId>(TId id, CancellationToken cancellationToken = default) where TId : |
| | | 84 | | { |
| | 3 | 85 | | return await _dbContext.Set<T>().FindAsync([id], cancellationToken: cancellationToken); |
| | 3 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <inheritdoc/> |
| | | 89 | | public virtual async Task<T> FirstAsync(Specification<T> specification, CancellationToken cancellationToken = defaul |
| | | 90 | | { |
| | 2 | 91 | | var result = await GenerateQuery(specification).FirstOrDefaultAsync(cancellationToken); |
| | 2 | 92 | | return result ?? throw new EntityNotFoundException(typeof(T).Name); |
| | 1 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <inheritdoc/> |
| | | 96 | | public virtual async Task<TResult> FirstAsync<TResult>(Specification<T, TResult> specification, CancellationToken ca |
| | | 97 | | { |
| | 2 | 98 | | var result = await GenerateQuery(specification).FirstOrDefaultAsync(cancellationToken); |
| | 2 | 99 | | return result ?? throw new EntityNotFoundException(typeof(T).Name); |
| | 1 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <inheritdoc/> |
| | | 103 | | public virtual async Task<T?> FirstOrDefaultAsync(Specification<T> specification, CancellationToken cancellationToke |
| | | 104 | | { |
| | 2 | 105 | | return await GenerateQuery(specification).FirstOrDefaultAsync(cancellationToken); |
| | 2 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <inheritdoc/> |
| | | 109 | | public virtual async Task<TResult?> FirstOrDefaultAsync<TResult>(Specification<T, TResult> specification, Cancellati |
| | | 110 | | { |
| | 2 | 111 | | return await GenerateQuery(specification).FirstOrDefaultAsync(cancellationToken); |
| | 2 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <inheritdoc/> |
| | | 115 | | public virtual async Task<T?> SingleOrDefaultAsync(Specification<T> specification, CancellationToken cancellationTok |
| | | 116 | | { |
| | 3 | 117 | | return await GenerateQuery(specification).SingleOrDefaultAsync(cancellationToken); |
| | 2 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <inheritdoc/> |
| | | 121 | | public virtual async Task<TResult?> SingleOrDefaultAsync<TResult>(Specification<T, TResult> specification, Cancellat |
| | | 122 | | { |
| | 3 | 123 | | return await GenerateQuery(specification).SingleOrDefaultAsync(cancellationToken); |
| | 2 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <inheritdoc/> |
| | | 127 | | public virtual async Task<List<T>> ListAsync(CancellationToken cancellationToken = default) |
| | | 128 | | { |
| | 1 | 129 | | return await _dbContext.Set<T>().ToListAsync(cancellationToken); |
| | 1 | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <inheritdoc/> |
| | | 133 | | public virtual async Task<List<T>> ListAsync(Specification<T> specification, CancellationToken cancellationToken = d |
| | | 134 | | { |
| | 1 | 135 | | return await GenerateQuery(specification).ToListAsync(cancellationToken); |
| | 1 | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <inheritdoc/> |
| | | 139 | | public virtual async Task<List<TResult>> ListAsync<TResult>(Specification<T, TResult> specification, CancellationTok |
| | | 140 | | { |
| | 1 | 141 | | return await GenerateQuery(specification).ToListAsync(cancellationToken); |
| | 1 | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <inheritdoc/> |
| | | 145 | | public virtual async Task<int> CountAsync(CancellationToken cancellationToken = default) |
| | | 146 | | { |
| | 2 | 147 | | return await _dbContext.Set<T>().CountAsync(cancellationToken); |
| | 2 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <inheritdoc/> |
| | | 151 | | public virtual async Task<int> CountAsync(Specification<T> specification, CancellationToken cancellationToken = defa |
| | | 152 | | { |
| | 3 | 153 | | return await GenerateQuery(specification, true).CountAsync(cancellationToken); |
| | 3 | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <inheritdoc/> |
| | | 157 | | public virtual async Task<int> CountAsync<TResult>(Specification<T, TResult> specification, CancellationToken cancel |
| | | 158 | | { |
| | 2 | 159 | | return await GenerateQuery(specification, true).CountAsync(cancellationToken); |
| | 2 | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <inheritdoc/> |
| | | 163 | | public virtual async Task<bool> AnyAsync(CancellationToken cancellationToken = default) |
| | | 164 | | { |
| | 2 | 165 | | return await _dbContext.Set<T>().AnyAsync(cancellationToken); |
| | 2 | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <inheritdoc/> |
| | | 169 | | public virtual async Task<bool> AnyAsync(Specification<T> specification, CancellationToken cancellationToken = defau |
| | | 170 | | { |
| | 3 | 171 | | return await GenerateQuery(specification, true).AnyAsync(cancellationToken); |
| | 3 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <inheritdoc/> |
| | | 175 | | public virtual async Task<bool> AnyAsync<TResult>(Specification<T, TResult> specification, CancellationToken cancell |
| | | 176 | | { |
| | 2 | 177 | | return await GenerateQuery(specification, true).AnyAsync(cancellationToken); |
| | 2 | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <inheritdoc/> |
| | | 181 | | public virtual IAsyncEnumerable<T> AsAsyncEnumerable(Specification<T> specification) |
| | | 182 | | { |
| | 1 | 183 | | 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 | | { |
| | 22 | 194 | | var query = _evaluator.Evaluate(_dbContext.Set<T>(), specification, ignorePaging); |
| | 22 | 195 | | 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 | | { |
| | 12 | 207 | | var query = _evaluator.Evaluate(_dbContext.Set<T>(), specification, ignorePaging); |
| | 12 | 208 | | return query; |
| | | 209 | | } |
| | | 210 | | } |