@@ -10,25 +10,25 @@ namespace Ardalis.Specification.EntityFramework6;
10
10
/// <inheritdoc/>
11
11
public abstract class RepositoryBase < T > : IRepositoryBase < T > where T : class
12
12
{
13
- private readonly DbContext _dbContext ;
14
- private readonly ISpecificationEvaluator _specificationEvaluator ;
13
+ protected DbContext DbContext { get ; set ; }
14
+ protected ISpecificationEvaluator SpecificationEvaluator { get ; set ; }
15
15
16
16
public RepositoryBase ( DbContext dbContext )
17
- : this ( dbContext , SpecificationEvaluator . Default )
17
+ : this ( dbContext , EntityFramework6 . SpecificationEvaluator . Default )
18
18
{
19
19
}
20
20
21
21
/// <inheritdoc/>
22
22
public RepositoryBase ( DbContext dbContext , ISpecificationEvaluator specificationEvaluator )
23
23
{
24
- _dbContext = dbContext ;
25
- _specificationEvaluator = specificationEvaluator ;
24
+ DbContext = dbContext ;
25
+ SpecificationEvaluator = specificationEvaluator ;
26
26
}
27
27
28
28
/// <inheritdoc/>
29
29
public virtual async Task < T > AddAsync ( T entity , CancellationToken cancellationToken = default )
30
30
{
31
- _dbContext . Set < T > ( ) . Add ( entity ) ;
31
+ DbContext . Set < T > ( ) . Add ( entity ) ;
32
32
33
33
await SaveChangesAsync ( cancellationToken ) ;
34
34
@@ -38,7 +38,7 @@ public virtual async Task<T> AddAsync(T entity, CancellationToken cancellationTo
38
38
/// <inheritdoc/>
39
39
public virtual async Task < IEnumerable < T > > AddRangeAsync ( IEnumerable < T > entities , CancellationToken cancellationToken = default )
40
40
{
41
- _dbContext . Set < T > ( ) . AddRange ( entities ) ;
41
+ DbContext . Set < T > ( ) . AddRange ( entities ) ;
42
42
43
43
await SaveChangesAsync ( cancellationToken ) ;
44
44
@@ -48,7 +48,7 @@ public virtual async Task<IEnumerable<T>> AddRangeAsync(IEnumerable<T> entities,
48
48
/// <inheritdoc/>
49
49
public virtual async Task UpdateAsync ( T entity , CancellationToken cancellationToken = default )
50
50
{
51
- _dbContext . Entry ( entity ) . State = EntityState . Modified ;
51
+ DbContext . Entry ( entity ) . State = EntityState . Modified ;
52
52
53
53
await SaveChangesAsync ( cancellationToken ) ;
54
54
}
@@ -58,7 +58,7 @@ public virtual async Task UpdateRangeAsync(IEnumerable<T> entities, Cancellation
58
58
{
59
59
foreach ( var entity in entities )
60
60
{
61
- _dbContext . Entry ( entity ) . State = EntityState . Modified ;
61
+ DbContext . Entry ( entity ) . State = EntityState . Modified ;
62
62
}
63
63
64
64
await SaveChangesAsync ( cancellationToken ) ;
@@ -67,38 +67,38 @@ public virtual async Task UpdateRangeAsync(IEnumerable<T> entities, Cancellation
67
67
/// <inheritdoc/>
68
68
public virtual async Task DeleteAsync ( T entity , CancellationToken cancellationToken = default )
69
69
{
70
- _dbContext . Set < T > ( ) . Remove ( entity ) ;
70
+ DbContext . Set < T > ( ) . Remove ( entity ) ;
71
71
72
72
await SaveChangesAsync ( cancellationToken ) ;
73
73
}
74
74
75
75
/// <inheritdoc/>
76
76
public virtual async Task DeleteRangeAsync ( IEnumerable < T > entities , CancellationToken cancellationToken = default )
77
77
{
78
- _dbContext . Set < T > ( ) . RemoveRange ( entities ) ;
78
+ DbContext . Set < T > ( ) . RemoveRange ( entities ) ;
79
79
80
80
await SaveChangesAsync ( cancellationToken ) ;
81
- }
82
-
81
+ }
82
+
83
83
/// <inheritdoc/>
84
84
public virtual async Task DeleteRangeAsync ( ISpecification < T > specification , CancellationToken cancellationToken = default )
85
- {
86
- var query = ApplySpecification ( specification ) ;
87
- _dbContext . Set < T > ( ) . RemoveRange ( query ) ;
88
-
85
+ {
86
+ var query = ApplySpecification ( specification ) ;
87
+ DbContext . Set < T > ( ) . RemoveRange ( query ) ;
88
+
89
89
await SaveChangesAsync ( cancellationToken ) ;
90
90
}
91
91
92
92
/// <inheritdoc/>
93
93
public virtual async Task < int > SaveChangesAsync ( CancellationToken cancellationToken = default )
94
94
{
95
- return await _dbContext . SaveChangesAsync ( cancellationToken ) ;
95
+ return await DbContext . SaveChangesAsync ( cancellationToken ) ;
96
96
}
97
97
98
98
/// <inheritdoc/>
99
99
public virtual async Task < T > GetByIdAsync < TId > ( TId id , CancellationToken cancellationToken = default )
100
100
{
101
- return await _dbContext . Set < T > ( ) . FindAsync ( cancellationToken : cancellationToken , new object [ ] { id } ) ;
101
+ return await DbContext . Set < T > ( ) . FindAsync ( cancellationToken : cancellationToken , new object [ ] { id } ) ;
102
102
}
103
103
104
104
/// <inheritdoc/>
@@ -142,7 +142,7 @@ public virtual async Task<TResult> SingleOrDefaultAsync<TResult>(ISingleResultSp
142
142
/// <inheritdoc/>
143
143
public virtual async Task < List < T > > ListAsync ( CancellationToken cancellationToken = default )
144
144
{
145
- return await _dbContext . Set < T > ( ) . ToListAsync ( cancellationToken ) ;
145
+ return await DbContext . Set < T > ( ) . ToListAsync ( cancellationToken ) ;
146
146
}
147
147
148
148
/// <inheritdoc/>
@@ -170,7 +170,7 @@ public virtual async Task<int> CountAsync(ISpecification<T> specification, Cance
170
170
/// <inheritdoc/>
171
171
public virtual async Task < int > CountAsync ( CancellationToken cancellationToken = default )
172
172
{
173
- return await _dbContext . Set < T > ( ) . CountAsync ( cancellationToken ) ;
173
+ return await DbContext . Set < T > ( ) . CountAsync ( cancellationToken ) ;
174
174
}
175
175
176
176
/// <inheritdoc/>
@@ -182,7 +182,7 @@ public virtual async Task<bool> AnyAsync(ISpecification<T> specification, Cancel
182
182
/// <inheritdoc/>
183
183
public virtual async Task < bool > AnyAsync ( CancellationToken cancellationToken = default )
184
184
{
185
- return await _dbContext . Set < T > ( ) . AnyAsync ( cancellationToken ) ;
185
+ return await DbContext . Set < T > ( ) . AnyAsync ( cancellationToken ) ;
186
186
}
187
187
188
188
#if NET6_0_OR_GREATER
@@ -201,7 +201,7 @@ public virtual IAsyncEnumerable<T> AsAsyncEnumerable(ISpecification<T> specifica
201
201
/// <returns>The filtered entities as an <see cref="IQueryable{T}"/>.</returns>
202
202
protected virtual IQueryable < T > ApplySpecification ( ISpecification < T > specification , bool evaluateCriteriaOnly = false )
203
203
{
204
- return _specificationEvaluator . GetQuery ( _dbContext . Set < T > ( ) . AsQueryable ( ) , specification , evaluateCriteriaOnly ) ;
204
+ return SpecificationEvaluator . GetQuery ( DbContext . Set < T > ( ) . AsQueryable ( ) , specification , evaluateCriteriaOnly ) ;
205
205
}
206
206
207
207
/// <summary>
@@ -216,6 +216,6 @@ protected virtual IQueryable<T> ApplySpecification(ISpecification<T> specificati
216
216
/// <returns>The filtered projected entities as an <see cref="IQueryable{T}"/>.</returns>
217
217
protected virtual IQueryable < TResult > ApplySpecification < TResult > ( ISpecification < T , TResult > specification )
218
218
{
219
- return _specificationEvaluator . GetQuery ( _dbContext . Set < T > ( ) . AsQueryable ( ) , specification ) ;
219
+ return SpecificationEvaluator . GetQuery ( DbContext . Set < T > ( ) . AsQueryable ( ) , specification ) ;
220
220
}
221
221
}
0 commit comments