Skip to content

Commit 4e54413

Browse files
authored
397 make fields and methods protected (#398)
* Make repository fields protected rather than private * Make repository fields protected in sample apps too * Turn protected fields into protected properties instead * Make the setters protected rather than private. This affords maximum extensibility, while still encapsulating the values within a property
1 parent 04bdf0f commit 4e54413

File tree

5 files changed

+73
-73
lines changed

5 files changed

+73
-73
lines changed

Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ namespace Ardalis.Specification.EntityFramework6;
1010
/// <inheritdoc/>
1111
public abstract class RepositoryBase<T> : IRepositoryBase<T> where T : class
1212
{
13-
private readonly DbContext _dbContext;
14-
private readonly ISpecificationEvaluator _specificationEvaluator;
13+
protected DbContext DbContext { get; set; }
14+
protected ISpecificationEvaluator SpecificationEvaluator { get; set; }
1515

1616
public RepositoryBase(DbContext dbContext)
17-
: this(dbContext, SpecificationEvaluator.Default)
17+
: this(dbContext, EntityFramework6.SpecificationEvaluator.Default)
1818
{
1919
}
2020

2121
/// <inheritdoc/>
2222
public RepositoryBase(DbContext dbContext, ISpecificationEvaluator specificationEvaluator)
2323
{
24-
_dbContext = dbContext;
25-
_specificationEvaluator = specificationEvaluator;
24+
DbContext = dbContext;
25+
SpecificationEvaluator = specificationEvaluator;
2626
}
2727

2828
/// <inheritdoc/>
2929
public virtual async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
3030
{
31-
_dbContext.Set<T>().Add(entity);
31+
DbContext.Set<T>().Add(entity);
3232

3333
await SaveChangesAsync(cancellationToken);
3434

@@ -38,7 +38,7 @@ public virtual async Task<T> AddAsync(T entity, CancellationToken cancellationTo
3838
/// <inheritdoc/>
3939
public virtual async Task<IEnumerable<T>> AddRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
4040
{
41-
_dbContext.Set<T>().AddRange(entities);
41+
DbContext.Set<T>().AddRange(entities);
4242

4343
await SaveChangesAsync(cancellationToken);
4444

@@ -48,7 +48,7 @@ public virtual async Task<IEnumerable<T>> AddRangeAsync(IEnumerable<T> entities,
4848
/// <inheritdoc/>
4949
public virtual async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
5050
{
51-
_dbContext.Entry(entity).State = EntityState.Modified;
51+
DbContext.Entry(entity).State = EntityState.Modified;
5252

5353
await SaveChangesAsync(cancellationToken);
5454
}
@@ -58,7 +58,7 @@ public virtual async Task UpdateRangeAsync(IEnumerable<T> entities, Cancellation
5858
{
5959
foreach (var entity in entities)
6060
{
61-
_dbContext.Entry(entity).State = EntityState.Modified;
61+
DbContext.Entry(entity).State = EntityState.Modified;
6262
}
6363

6464
await SaveChangesAsync(cancellationToken);
@@ -67,38 +67,38 @@ public virtual async Task UpdateRangeAsync(IEnumerable<T> entities, Cancellation
6767
/// <inheritdoc/>
6868
public virtual async Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
6969
{
70-
_dbContext.Set<T>().Remove(entity);
70+
DbContext.Set<T>().Remove(entity);
7171

7272
await SaveChangesAsync(cancellationToken);
7373
}
7474

7575
/// <inheritdoc/>
7676
public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
7777
{
78-
_dbContext.Set<T>().RemoveRange(entities);
78+
DbContext.Set<T>().RemoveRange(entities);
7979

8080
await SaveChangesAsync(cancellationToken);
81-
}
82-
81+
}
82+
8383
/// <inheritdoc/>
8484
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+
8989
await SaveChangesAsync(cancellationToken);
9090
}
9191

9292
/// <inheritdoc/>
9393
public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
9494
{
95-
return await _dbContext.SaveChangesAsync(cancellationToken);
95+
return await DbContext.SaveChangesAsync(cancellationToken);
9696
}
9797

9898
/// <inheritdoc/>
9999
public virtual async Task<T> GetByIdAsync<TId>(TId id, CancellationToken cancellationToken = default)
100100
{
101-
return await _dbContext.Set<T>().FindAsync(cancellationToken: cancellationToken, new object[] { id });
101+
return await DbContext.Set<T>().FindAsync(cancellationToken: cancellationToken, new object[] { id });
102102
}
103103

104104
/// <inheritdoc/>
@@ -142,7 +142,7 @@ public virtual async Task<TResult> SingleOrDefaultAsync<TResult>(ISingleResultSp
142142
/// <inheritdoc/>
143143
public virtual async Task<List<T>> ListAsync(CancellationToken cancellationToken = default)
144144
{
145-
return await _dbContext.Set<T>().ToListAsync(cancellationToken);
145+
return await DbContext.Set<T>().ToListAsync(cancellationToken);
146146
}
147147

148148
/// <inheritdoc/>
@@ -170,7 +170,7 @@ public virtual async Task<int> CountAsync(ISpecification<T> specification, Cance
170170
/// <inheritdoc/>
171171
public virtual async Task<int> CountAsync(CancellationToken cancellationToken = default)
172172
{
173-
return await _dbContext.Set<T>().CountAsync(cancellationToken);
173+
return await DbContext.Set<T>().CountAsync(cancellationToken);
174174
}
175175

176176
/// <inheritdoc/>
@@ -182,7 +182,7 @@ public virtual async Task<bool> AnyAsync(ISpecification<T> specification, Cancel
182182
/// <inheritdoc/>
183183
public virtual async Task<bool> AnyAsync(CancellationToken cancellationToken = default)
184184
{
185-
return await _dbContext.Set<T>().AnyAsync(cancellationToken);
185+
return await DbContext.Set<T>().AnyAsync(cancellationToken);
186186
}
187187

188188
#if NET6_0_OR_GREATER
@@ -201,7 +201,7 @@ public virtual IAsyncEnumerable<T> AsAsyncEnumerable(ISpecification<T> specifica
201201
/// <returns>The filtered entities as an <see cref="IQueryable{T}"/>.</returns>
202202
protected virtual IQueryable<T> ApplySpecification(ISpecification<T> specification, bool evaluateCriteriaOnly = false)
203203
{
204-
return _specificationEvaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), specification, evaluateCriteriaOnly);
204+
return SpecificationEvaluator.GetQuery(DbContext.Set<T>().AsQueryable(), specification, evaluateCriteriaOnly);
205205
}
206206

207207
/// <summary>
@@ -216,6 +216,6 @@ protected virtual IQueryable<T> ApplySpecification(ISpecification<T> specificati
216216
/// <returns>The filtered projected entities as an <see cref="IQueryable{T}"/>.</returns>
217217
protected virtual IQueryable<TResult> ApplySpecification<TResult>(ISpecification<T, TResult> specification)
218218
{
219-
return _specificationEvaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), specification);
219+
return SpecificationEvaluator.GetQuery(DbContext.Set<T>().AsQueryable(), specification);
220220
}
221221
}

Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/RepositoryOfT.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ public class Repository<T> : RepositoryBase<T> where T : class
55
{
66
protected readonly TestDbContext dbContext;
77

8-
public Repository(TestDbContext dbContext) : this(dbContext, SpecificationEvaluator.Default)
8+
public Repository(TestDbContext dbContext) : this(dbContext, EntityFramework6.SpecificationEvaluator.Default)
99
{
1010
}
1111

1212
public Repository(TestDbContext dbContext, ISpecificationEvaluator specificationEvaluator) : base(dbContext, specificationEvaluator)
1313
{
1414
this.dbContext = dbContext;
1515
}
16-
}
16+
}

Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ namespace Ardalis.Specification.EntityFrameworkCore;
55
/// <inheritdoc/>
66
public abstract class RepositoryBase<T> : IRepositoryBase<T> where T : class
77
{
8-
private readonly DbContext _dbContext;
9-
private readonly ISpecificationEvaluator _specificationEvaluator;
8+
protected DbContext DbContext { get; set; }
9+
protected ISpecificationEvaluator SpecificationEvaluator { get; set; }
1010

1111
public RepositoryBase(DbContext dbContext)
12-
: this(dbContext, SpecificationEvaluator.Default)
12+
: this(dbContext, EntityFrameworkCore.SpecificationEvaluator.Default)
1313
{
1414
}
1515

1616
/// <inheritdoc/>
1717
public RepositoryBase(DbContext dbContext, ISpecificationEvaluator specificationEvaluator)
1818
{
19-
_dbContext = dbContext;
20-
_specificationEvaluator = specificationEvaluator;
19+
DbContext = dbContext;
20+
SpecificationEvaluator = specificationEvaluator;
2121
}
2222

2323
/// <inheritdoc/>
2424
public virtual async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
2525
{
26-
_dbContext.Set<T>().Add(entity);
26+
DbContext.Set<T>().Add(entity);
2727

2828
await SaveChangesAsync(cancellationToken);
2929

@@ -33,7 +33,7 @@ public virtual async Task<T> AddAsync(T entity, CancellationToken cancellationTo
3333
/// <inheritdoc/>
3434
public virtual async Task<IEnumerable<T>> AddRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
3535
{
36-
_dbContext.Set<T>().AddRange(entities);
36+
DbContext.Set<T>().AddRange(entities);
3737

3838
await SaveChangesAsync(cancellationToken);
3939

@@ -43,54 +43,54 @@ public virtual async Task<IEnumerable<T>> AddRangeAsync(IEnumerable<T> entities,
4343
/// <inheritdoc/>
4444
public virtual async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
4545
{
46-
_dbContext.Set<T>().Update(entity);
46+
DbContext.Set<T>().Update(entity);
4747

4848
await SaveChangesAsync(cancellationToken);
4949
}
5050

5151
/// <inheritdoc/>
5252
public virtual async Task UpdateRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
5353
{
54-
_dbContext.Set<T>().UpdateRange(entities);
54+
DbContext.Set<T>().UpdateRange(entities);
5555

5656
await SaveChangesAsync(cancellationToken);
5757
}
5858

5959
/// <inheritdoc/>
6060
public virtual async Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
6161
{
62-
_dbContext.Set<T>().Remove(entity);
62+
DbContext.Set<T>().Remove(entity);
6363

6464
await SaveChangesAsync(cancellationToken);
65-
}
66-
65+
}
66+
6767
/// <inheritdoc/>
6868
public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
6969
{
70-
_dbContext.Set<T>().RemoveRange(entities);
70+
DbContext.Set<T>().RemoveRange(entities);
7171

7272
await SaveChangesAsync(cancellationToken);
73-
}
74-
73+
}
74+
7575
/// <inheritdoc/>
7676
public virtual async Task DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
77-
{
78-
var query = ApplySpecification(specification);
79-
_dbContext.Set<T>().RemoveRange(query);
80-
77+
{
78+
var query = ApplySpecification(specification);
79+
DbContext.Set<T>().RemoveRange(query);
80+
8181
await SaveChangesAsync(cancellationToken);
8282
}
8383

8484
/// <inheritdoc/>
8585
public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
8686
{
87-
return await _dbContext.SaveChangesAsync(cancellationToken);
87+
return await DbContext.SaveChangesAsync(cancellationToken);
8888
}
8989

9090
/// <inheritdoc/>
9191
public virtual async Task<T?> GetByIdAsync<TId>(TId id, CancellationToken cancellationToken = default) where TId : notnull
9292
{
93-
return await _dbContext.Set<T>().FindAsync(new object[] { id }, cancellationToken: cancellationToken);
93+
return await DbContext.Set<T>().FindAsync(new object[] { id }, cancellationToken: cancellationToken);
9494
}
9595

9696
/// <inheritdoc/>
@@ -134,7 +134,7 @@ public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationTo
134134
/// <inheritdoc/>
135135
public virtual async Task<List<T>> ListAsync(CancellationToken cancellationToken = default)
136136
{
137-
return await _dbContext.Set<T>().ToListAsync(cancellationToken);
137+
return await DbContext.Set<T>().ToListAsync(cancellationToken);
138138
}
139139

140140
/// <inheritdoc/>
@@ -162,7 +162,7 @@ public virtual async Task<int> CountAsync(ISpecification<T> specification, Cance
162162
/// <inheritdoc/>
163163
public virtual async Task<int> CountAsync(CancellationToken cancellationToken = default)
164164
{
165-
return await _dbContext.Set<T>().CountAsync(cancellationToken);
165+
return await DbContext.Set<T>().CountAsync(cancellationToken);
166166
}
167167

168168
/// <inheritdoc/>
@@ -174,7 +174,7 @@ public virtual async Task<bool> AnyAsync(ISpecification<T> specification, Cancel
174174
/// <inheritdoc/>
175175
public virtual async Task<bool> AnyAsync(CancellationToken cancellationToken = default)
176176
{
177-
return await _dbContext.Set<T>().AnyAsync(cancellationToken);
177+
return await DbContext.Set<T>().AnyAsync(cancellationToken);
178178
}
179179

180180
/// <inheritdoc/>
@@ -191,7 +191,7 @@ public virtual IAsyncEnumerable<T> AsAsyncEnumerable(ISpecification<T> specifica
191191
/// <returns>The filtered entities as an <see cref="IQueryable{T}"/>.</returns>
192192
protected virtual IQueryable<T> ApplySpecification(ISpecification<T> specification, bool evaluateCriteriaOnly = false)
193193
{
194-
return _specificationEvaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), specification, evaluateCriteriaOnly);
194+
return SpecificationEvaluator.GetQuery(DbContext.Set<T>().AsQueryable(), specification, evaluateCriteriaOnly);
195195
}
196196

197197
/// <summary>
@@ -206,6 +206,6 @@ protected virtual IQueryable<T> ApplySpecification(ISpecification<T> specificati
206206
/// <returns>The filtered projected entities as an <see cref="IQueryable{T}"/>.</returns>
207207
protected virtual IQueryable<TResult> ApplySpecification<TResult>(ISpecification<T, TResult> specification)
208208
{
209-
return _specificationEvaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), specification);
209+
return SpecificationEvaluator.GetQuery(DbContext.Set<T>().AsQueryable(), specification);
210210
}
211211
}

Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class Repository<T> : RepositoryBase<T> where T : class
55
{
66
protected readonly TestDbContext dbContext;
77

8-
public Repository(TestDbContext dbContext) : this(dbContext, SpecificationEvaluator.Default)
8+
public Repository(TestDbContext dbContext) : this(dbContext, EntityFrameworkCore.SpecificationEvaluator.Default)
99
{
1010
}
1111

0 commit comments

Comments
 (0)