Skip to content

Commit

Permalink
397 make fields and methods protected (#398)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
eldamir committed May 21, 2024
1 parent 04bdf0f commit 4e54413
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ namespace Ardalis.Specification.EntityFramework6;
/// <inheritdoc/>
public abstract class RepositoryBase<T> : IRepositoryBase<T> where T : class
{
private readonly DbContext _dbContext;
private readonly ISpecificationEvaluator _specificationEvaluator;
protected DbContext DbContext { get; set; }
protected ISpecificationEvaluator SpecificationEvaluator { get; set; }

public RepositoryBase(DbContext dbContext)
: this(dbContext, SpecificationEvaluator.Default)
: this(dbContext, EntityFramework6.SpecificationEvaluator.Default)
{
}

/// <inheritdoc/>
public RepositoryBase(DbContext dbContext, ISpecificationEvaluator specificationEvaluator)
{
_dbContext = dbContext;
_specificationEvaluator = specificationEvaluator;
DbContext = dbContext;
SpecificationEvaluator = specificationEvaluator;
}

/// <inheritdoc/>
public virtual async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
{
_dbContext.Set<T>().Add(entity);
DbContext.Set<T>().Add(entity);

await SaveChangesAsync(cancellationToken);

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

await SaveChangesAsync(cancellationToken);

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

await SaveChangesAsync(cancellationToken);
}
Expand All @@ -58,7 +58,7 @@ public virtual async Task UpdateRangeAsync(IEnumerable<T> entities, Cancellation
{
foreach (var entity in entities)
{
_dbContext.Entry(entity).State = EntityState.Modified;
DbContext.Entry(entity).State = EntityState.Modified;
}

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

await SaveChangesAsync(cancellationToken);
}

/// <inheritdoc/>
public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
{
_dbContext.Set<T>().RemoveRange(entities);
DbContext.Set<T>().RemoveRange(entities);

await SaveChangesAsync(cancellationToken);
}

}

/// <inheritdoc/>
public virtual async Task DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
{
var query = ApplySpecification(specification);
_dbContext.Set<T>().RemoveRange(query);

{
var query = ApplySpecification(specification);
DbContext.Set<T>().RemoveRange(query);

await SaveChangesAsync(cancellationToken);
}

/// <inheritdoc/>
public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
return await _dbContext.SaveChangesAsync(cancellationToken);
return await DbContext.SaveChangesAsync(cancellationToken);
}

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

/// <inheritdoc/>
Expand Down Expand Up @@ -142,7 +142,7 @@ public virtual async Task<TResult> SingleOrDefaultAsync<TResult>(ISingleResultSp
/// <inheritdoc/>
public virtual async Task<List<T>> ListAsync(CancellationToken cancellationToken = default)
{
return await _dbContext.Set<T>().ToListAsync(cancellationToken);
return await DbContext.Set<T>().ToListAsync(cancellationToken);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -170,7 +170,7 @@ public virtual async Task<int> CountAsync(ISpecification<T> specification, Cance
/// <inheritdoc/>
public virtual async Task<int> CountAsync(CancellationToken cancellationToken = default)
{
return await _dbContext.Set<T>().CountAsync(cancellationToken);
return await DbContext.Set<T>().CountAsync(cancellationToken);
}

/// <inheritdoc/>
Expand All @@ -182,7 +182,7 @@ public virtual async Task<bool> AnyAsync(ISpecification<T> specification, Cancel
/// <inheritdoc/>
public virtual async Task<bool> AnyAsync(CancellationToken cancellationToken = default)
{
return await _dbContext.Set<T>().AnyAsync(cancellationToken);
return await DbContext.Set<T>().AnyAsync(cancellationToken);
}

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

/// <summary>
Expand All @@ -216,6 +216,6 @@ protected virtual IQueryable<T> ApplySpecification(ISpecification<T> specificati
/// <returns>The filtered projected entities as an <see cref="IQueryable{T}"/>.</returns>
protected virtual IQueryable<TResult> ApplySpecification<TResult>(ISpecification<T, TResult> specification)
{
return _specificationEvaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), specification);
return SpecificationEvaluator.GetQuery(DbContext.Set<T>().AsQueryable(), specification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ public class Repository<T> : RepositoryBase<T> where T : class
{
protected readonly TestDbContext dbContext;

public Repository(TestDbContext dbContext) : this(dbContext, SpecificationEvaluator.Default)
public Repository(TestDbContext dbContext) : this(dbContext, EntityFramework6.SpecificationEvaluator.Default)
{
}

public Repository(TestDbContext dbContext, ISpecificationEvaluator specificationEvaluator) : base(dbContext, specificationEvaluator)
{
this.dbContext = dbContext;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ namespace Ardalis.Specification.EntityFrameworkCore;
/// <inheritdoc/>
public abstract class RepositoryBase<T> : IRepositoryBase<T> where T : class
{
private readonly DbContext _dbContext;
private readonly ISpecificationEvaluator _specificationEvaluator;
protected DbContext DbContext { get; set; }
protected ISpecificationEvaluator SpecificationEvaluator { get; set; }

public RepositoryBase(DbContext dbContext)
: this(dbContext, SpecificationEvaluator.Default)
: this(dbContext, EntityFrameworkCore.SpecificationEvaluator.Default)
{
}

/// <inheritdoc/>
public RepositoryBase(DbContext dbContext, ISpecificationEvaluator specificationEvaluator)
{
_dbContext = dbContext;
_specificationEvaluator = specificationEvaluator;
DbContext = dbContext;
SpecificationEvaluator = specificationEvaluator;
}

/// <inheritdoc/>
public virtual async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
{
_dbContext.Set<T>().Add(entity);
DbContext.Set<T>().Add(entity);

await SaveChangesAsync(cancellationToken);

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

await SaveChangesAsync(cancellationToken);

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

await SaveChangesAsync(cancellationToken);
}

/// <inheritdoc/>
public virtual async Task UpdateRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
{
_dbContext.Set<T>().UpdateRange(entities);
DbContext.Set<T>().UpdateRange(entities);

await SaveChangesAsync(cancellationToken);
}

/// <inheritdoc/>
public virtual async Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
{
_dbContext.Set<T>().Remove(entity);
DbContext.Set<T>().Remove(entity);

await SaveChangesAsync(cancellationToken);
}

}

/// <inheritdoc/>
public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
{
_dbContext.Set<T>().RemoveRange(entities);
DbContext.Set<T>().RemoveRange(entities);

await SaveChangesAsync(cancellationToken);
}

}

/// <inheritdoc/>
public virtual async Task DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
{
var query = ApplySpecification(specification);
_dbContext.Set<T>().RemoveRange(query);

{
var query = ApplySpecification(specification);
DbContext.Set<T>().RemoveRange(query);

await SaveChangesAsync(cancellationToken);
}

/// <inheritdoc/>
public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
return await _dbContext.SaveChangesAsync(cancellationToken);
return await DbContext.SaveChangesAsync(cancellationToken);
}

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

/// <inheritdoc/>
Expand Down Expand Up @@ -134,7 +134,7 @@ public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationTo
/// <inheritdoc/>
public virtual async Task<List<T>> ListAsync(CancellationToken cancellationToken = default)
{
return await _dbContext.Set<T>().ToListAsync(cancellationToken);
return await DbContext.Set<T>().ToListAsync(cancellationToken);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -162,7 +162,7 @@ public virtual async Task<int> CountAsync(ISpecification<T> specification, Cance
/// <inheritdoc/>
public virtual async Task<int> CountAsync(CancellationToken cancellationToken = default)
{
return await _dbContext.Set<T>().CountAsync(cancellationToken);
return await DbContext.Set<T>().CountAsync(cancellationToken);
}

/// <inheritdoc/>
Expand All @@ -174,7 +174,7 @@ public virtual async Task<bool> AnyAsync(ISpecification<T> specification, Cancel
/// <inheritdoc/>
public virtual async Task<bool> AnyAsync(CancellationToken cancellationToken = default)
{
return await _dbContext.Set<T>().AnyAsync(cancellationToken);
return await DbContext.Set<T>().AnyAsync(cancellationToken);
}

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

/// <summary>
Expand All @@ -206,6 +206,6 @@ protected virtual IQueryable<T> ApplySpecification(ISpecification<T> specificati
/// <returns>The filtered projected entities as an <see cref="IQueryable{T}"/>.</returns>
protected virtual IQueryable<TResult> ApplySpecification<TResult>(ISpecification<T, TResult> specification)
{
return _specificationEvaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), specification);
return SpecificationEvaluator.GetQuery(DbContext.Set<T>().AsQueryable(), specification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class Repository<T> : RepositoryBase<T> where T : class
{
protected readonly TestDbContext dbContext;

public Repository(TestDbContext dbContext) : this(dbContext, SpecificationEvaluator.Default)
public Repository(TestDbContext dbContext) : this(dbContext, EntityFrameworkCore.SpecificationEvaluator.Default)
{
}

Expand Down
Loading

0 comments on commit 4e54413

Please sign in to comment.