From 4524f2940a1a7304c636a46ba2a24759d541a755 Mon Sep 17 00:00:00 2001 From: Ruben Nielsen Date: Thu, 9 May 2024 20:29:45 +0200 Subject: [PATCH 1/4] Make repository fields protected rather than private --- .../RepositoryBaseOfT.cs | 16 +++++++-------- .../RepositoryBaseOfT.cs | 20 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs index ca2424f..cabde97 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs @@ -10,8 +10,8 @@ namespace Ardalis.Specification.EntityFramework6; /// public abstract class RepositoryBase : IRepositoryBase where T : class { - private readonly DbContext _dbContext; - private readonly ISpecificationEvaluator _specificationEvaluator; + protected readonly DbContext _dbContext; + protected readonly ISpecificationEvaluator _specificationEvaluator; public RepositoryBase(DbContext dbContext) : this(dbContext, SpecificationEvaluator.Default) @@ -78,14 +78,14 @@ public virtual async Task DeleteRangeAsync(IEnumerable entities, Cancellation _dbContext.Set().RemoveRange(entities); await SaveChangesAsync(cancellationToken); - } - + } + /// public virtual async Task DeleteRangeAsync(ISpecification specification, CancellationToken cancellationToken = default) - { - var query = ApplySpecification(specification); - _dbContext.Set().RemoveRange(query); - + { + var query = ApplySpecification(specification); + _dbContext.Set().RemoveRange(query); + await SaveChangesAsync(cancellationToken); } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs index dd7fbc7..d7aa7a5 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs @@ -5,8 +5,8 @@ namespace Ardalis.Specification.EntityFrameworkCore; /// public abstract class RepositoryBase : IRepositoryBase where T : class { - private readonly DbContext _dbContext; - private readonly ISpecificationEvaluator _specificationEvaluator; + protected readonly DbContext _dbContext; + protected readonly ISpecificationEvaluator _specificationEvaluator; public RepositoryBase(DbContext dbContext) : this(dbContext, SpecificationEvaluator.Default) @@ -62,22 +62,22 @@ public virtual async Task DeleteAsync(T entity, CancellationToken cancellationTo _dbContext.Set().Remove(entity); await SaveChangesAsync(cancellationToken); - } - + } + /// public virtual async Task DeleteRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { _dbContext.Set().RemoveRange(entities); await SaveChangesAsync(cancellationToken); - } - + } + /// public virtual async Task DeleteRangeAsync(ISpecification specification, CancellationToken cancellationToken = default) - { - var query = ApplySpecification(specification); - _dbContext.Set().RemoveRange(query); - + { + var query = ApplySpecification(specification); + _dbContext.Set().RemoveRange(query); + await SaveChangesAsync(cancellationToken); } From d8b12112f3ca1fc44f529ce932d90a6586ca5b11 Mon Sep 17 00:00:00 2001 From: Ruben Nielsen Date: Thu, 9 May 2024 20:44:59 +0200 Subject: [PATCH 2/4] Make repository fields protected in sample apps too --- sample/Ardalis.Sample.App3/RepositoryBase.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sample/Ardalis.Sample.App3/RepositoryBase.cs b/sample/Ardalis.Sample.App3/RepositoryBase.cs index 79a7007..eac1413 100644 --- a/sample/Ardalis.Sample.App3/RepositoryBase.cs +++ b/sample/Ardalis.Sample.App3/RepositoryBase.cs @@ -9,8 +9,8 @@ namespace Ardalis.Sample.App3; public abstract class RepositoryBase : IReadRepository where T : class { - private readonly DbContext _dbContext; - private readonly AutoMapper.IConfigurationProvider _configurationProvider; + protected readonly DbContext _dbContext; + protected readonly AutoMapper.IConfigurationProvider _configurationProvider; protected ISpecificationEvaluator Evaluator { get; } // We have a custom evaluator for QueryTag, therefore we're passing our custom specification evaluator @@ -59,12 +59,12 @@ public virtual async Task DeleteRangeAsync(IEnumerable entities, Cancellation _dbContext.Set().RemoveRange(entities); await SaveChangesAsync(cancellationToken); - } + } public virtual async Task DeleteRangeAsync(ISpecification specification, CancellationToken cancellationToken = default) - { - var query = ApplySpecification(specification); - _dbContext.Set().RemoveRange(query); - + { + var query = ApplySpecification(specification); + _dbContext.Set().RemoveRange(query); + await SaveChangesAsync(cancellationToken); } public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = default) From ef13433abe1a28e7bcddd839a62ef11fe1b5267e Mon Sep 17 00:00:00 2001 From: Ruben Nielsen Date: Mon, 13 May 2024 08:12:40 +0200 Subject: [PATCH 3/4] Turn protected fields into protected properties instead --- .../RepositoryBaseOfT.cs | 38 +++++++++---------- .../Fixture/RepositoryOfT.cs | 4 +- .../RepositoryBaseOfT.cs | 38 +++++++++---------- .../Fixture/RepositoryOfT.cs | 2 +- sample/Ardalis.Sample.App3/RepositoryBase.cs | 30 +++++++-------- 5 files changed, 56 insertions(+), 56 deletions(-) diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs index cabde97..6af066d 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs @@ -10,25 +10,25 @@ namespace Ardalis.Specification.EntityFramework6; /// public abstract class RepositoryBase : IRepositoryBase where T : class { - protected readonly DbContext _dbContext; - protected readonly ISpecificationEvaluator _specificationEvaluator; + protected DbContext DbContext { get; private set; } + protected ISpecificationEvaluator SpecificationEvaluator { get; private set; } public RepositoryBase(DbContext dbContext) - : this(dbContext, SpecificationEvaluator.Default) + : this(dbContext, EntityFramework6.SpecificationEvaluator.Default) { } /// public RepositoryBase(DbContext dbContext, ISpecificationEvaluator specificationEvaluator) { - _dbContext = dbContext; - _specificationEvaluator = specificationEvaluator; + DbContext = dbContext; + SpecificationEvaluator = specificationEvaluator; } /// public virtual async Task AddAsync(T entity, CancellationToken cancellationToken = default) { - _dbContext.Set().Add(entity); + DbContext.Set().Add(entity); await SaveChangesAsync(cancellationToken); @@ -38,7 +38,7 @@ public virtual async Task AddAsync(T entity, CancellationToken cancellationTo /// public virtual async Task> AddRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - _dbContext.Set().AddRange(entities); + DbContext.Set().AddRange(entities); await SaveChangesAsync(cancellationToken); @@ -48,7 +48,7 @@ public virtual async Task> AddRangeAsync(IEnumerable entities, /// 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); } @@ -58,7 +58,7 @@ public virtual async Task UpdateRangeAsync(IEnumerable entities, Cancellation { foreach (var entity in entities) { - _dbContext.Entry(entity).State = EntityState.Modified; + DbContext.Entry(entity).State = EntityState.Modified; } await SaveChangesAsync(cancellationToken); @@ -67,7 +67,7 @@ public virtual async Task UpdateRangeAsync(IEnumerable entities, Cancellation /// public virtual async Task DeleteAsync(T entity, CancellationToken cancellationToken = default) { - _dbContext.Set().Remove(entity); + DbContext.Set().Remove(entity); await SaveChangesAsync(cancellationToken); } @@ -75,7 +75,7 @@ public virtual async Task DeleteAsync(T entity, CancellationToken cancellationTo /// public virtual async Task DeleteRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - _dbContext.Set().RemoveRange(entities); + DbContext.Set().RemoveRange(entities); await SaveChangesAsync(cancellationToken); } @@ -84,7 +84,7 @@ public virtual async Task DeleteRangeAsync(IEnumerable entities, Cancellation public virtual async Task DeleteRangeAsync(ISpecification specification, CancellationToken cancellationToken = default) { var query = ApplySpecification(specification); - _dbContext.Set().RemoveRange(query); + DbContext.Set().RemoveRange(query); await SaveChangesAsync(cancellationToken); } @@ -92,13 +92,13 @@ public virtual async Task DeleteRangeAsync(ISpecification specification, Canc /// public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = default) { - return await _dbContext.SaveChangesAsync(cancellationToken); + return await DbContext.SaveChangesAsync(cancellationToken); } /// public virtual async Task GetByIdAsync(TId id, CancellationToken cancellationToken = default) { - return await _dbContext.Set().FindAsync(cancellationToken: cancellationToken, new object[] { id }); + return await DbContext.Set().FindAsync(cancellationToken: cancellationToken, new object[] { id }); } /// @@ -142,7 +142,7 @@ public virtual async Task SingleOrDefaultAsync(ISingleResultSp /// public virtual async Task> ListAsync(CancellationToken cancellationToken = default) { - return await _dbContext.Set().ToListAsync(cancellationToken); + return await DbContext.Set().ToListAsync(cancellationToken); } /// @@ -170,7 +170,7 @@ public virtual async Task CountAsync(ISpecification specification, Cance /// public virtual async Task CountAsync(CancellationToken cancellationToken = default) { - return await _dbContext.Set().CountAsync(cancellationToken); + return await DbContext.Set().CountAsync(cancellationToken); } /// @@ -182,7 +182,7 @@ public virtual async Task AnyAsync(ISpecification specification, Cancel /// public virtual async Task AnyAsync(CancellationToken cancellationToken = default) { - return await _dbContext.Set().AnyAsync(cancellationToken); + return await DbContext.Set().AnyAsync(cancellationToken); } #if NET6_0_OR_GREATER @@ -201,7 +201,7 @@ public virtual IAsyncEnumerable AsAsyncEnumerable(ISpecification specifica /// The filtered entities as an . protected virtual IQueryable ApplySpecification(ISpecification specification, bool evaluateCriteriaOnly = false) { - return _specificationEvaluator.GetQuery(_dbContext.Set().AsQueryable(), specification, evaluateCriteriaOnly); + return SpecificationEvaluator.GetQuery(DbContext.Set().AsQueryable(), specification, evaluateCriteriaOnly); } /// @@ -216,6 +216,6 @@ protected virtual IQueryable ApplySpecification(ISpecification specificati /// The filtered projected entities as an . protected virtual IQueryable ApplySpecification(ISpecification specification) { - return _specificationEvaluator.GetQuery(_dbContext.Set().AsQueryable(), specification); + return SpecificationEvaluator.GetQuery(DbContext.Set().AsQueryable(), specification); } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/RepositoryOfT.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/RepositoryOfT.cs index 2c7e8c9..ea2832c 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/RepositoryOfT.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/RepositoryOfT.cs @@ -5,7 +5,7 @@ public class Repository : RepositoryBase where T : class { protected readonly TestDbContext dbContext; - public Repository(TestDbContext dbContext) : this(dbContext, SpecificationEvaluator.Default) + public Repository(TestDbContext dbContext) : this(dbContext, EntityFramework6.SpecificationEvaluator.Default) { } @@ -13,4 +13,4 @@ public Repository(TestDbContext dbContext, ISpecificationEvaluator specification { this.dbContext = dbContext; } -} +} diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs index d7aa7a5..bd6010d 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs @@ -5,25 +5,25 @@ namespace Ardalis.Specification.EntityFrameworkCore; /// public abstract class RepositoryBase : IRepositoryBase where T : class { - protected readonly DbContext _dbContext; - protected readonly ISpecificationEvaluator _specificationEvaluator; + protected DbContext DbContext { get; private set; } + protected ISpecificationEvaluator SpecificationEvaluator { get; private set; } public RepositoryBase(DbContext dbContext) - : this(dbContext, SpecificationEvaluator.Default) + : this(dbContext, EntityFrameworkCore.SpecificationEvaluator.Default) { } /// public RepositoryBase(DbContext dbContext, ISpecificationEvaluator specificationEvaluator) { - _dbContext = dbContext; - _specificationEvaluator = specificationEvaluator; + DbContext = dbContext; + SpecificationEvaluator = specificationEvaluator; } /// public virtual async Task AddAsync(T entity, CancellationToken cancellationToken = default) { - _dbContext.Set().Add(entity); + DbContext.Set().Add(entity); await SaveChangesAsync(cancellationToken); @@ -33,7 +33,7 @@ public virtual async Task AddAsync(T entity, CancellationToken cancellationTo /// public virtual async Task> AddRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - _dbContext.Set().AddRange(entities); + DbContext.Set().AddRange(entities); await SaveChangesAsync(cancellationToken); @@ -43,7 +43,7 @@ public virtual async Task> AddRangeAsync(IEnumerable entities, /// public virtual async Task UpdateAsync(T entity, CancellationToken cancellationToken = default) { - _dbContext.Set().Update(entity); + DbContext.Set().Update(entity); await SaveChangesAsync(cancellationToken); } @@ -51,7 +51,7 @@ public virtual async Task UpdateAsync(T entity, CancellationToken cancellationTo /// public virtual async Task UpdateRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - _dbContext.Set().UpdateRange(entities); + DbContext.Set().UpdateRange(entities); await SaveChangesAsync(cancellationToken); } @@ -59,7 +59,7 @@ public virtual async Task UpdateRangeAsync(IEnumerable entities, Cancellation /// public virtual async Task DeleteAsync(T entity, CancellationToken cancellationToken = default) { - _dbContext.Set().Remove(entity); + DbContext.Set().Remove(entity); await SaveChangesAsync(cancellationToken); } @@ -67,7 +67,7 @@ public virtual async Task DeleteAsync(T entity, CancellationToken cancellationTo /// public virtual async Task DeleteRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - _dbContext.Set().RemoveRange(entities); + DbContext.Set().RemoveRange(entities); await SaveChangesAsync(cancellationToken); } @@ -76,7 +76,7 @@ public virtual async Task DeleteRangeAsync(IEnumerable entities, Cancellation public virtual async Task DeleteRangeAsync(ISpecification specification, CancellationToken cancellationToken = default) { var query = ApplySpecification(specification); - _dbContext.Set().RemoveRange(query); + DbContext.Set().RemoveRange(query); await SaveChangesAsync(cancellationToken); } @@ -84,13 +84,13 @@ public virtual async Task DeleteRangeAsync(ISpecification specification, Canc /// public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = default) { - return await _dbContext.SaveChangesAsync(cancellationToken); + return await DbContext.SaveChangesAsync(cancellationToken); } /// public virtual async Task GetByIdAsync(TId id, CancellationToken cancellationToken = default) where TId : notnull { - return await _dbContext.Set().FindAsync(new object[] { id }, cancellationToken: cancellationToken); + return await DbContext.Set().FindAsync(new object[] { id }, cancellationToken: cancellationToken); } /// @@ -134,7 +134,7 @@ public virtual async Task SaveChangesAsync(CancellationToken cancellationTo /// public virtual async Task> ListAsync(CancellationToken cancellationToken = default) { - return await _dbContext.Set().ToListAsync(cancellationToken); + return await DbContext.Set().ToListAsync(cancellationToken); } /// @@ -162,7 +162,7 @@ public virtual async Task CountAsync(ISpecification specification, Cance /// public virtual async Task CountAsync(CancellationToken cancellationToken = default) { - return await _dbContext.Set().CountAsync(cancellationToken); + return await DbContext.Set().CountAsync(cancellationToken); } /// @@ -174,7 +174,7 @@ public virtual async Task AnyAsync(ISpecification specification, Cancel /// public virtual async Task AnyAsync(CancellationToken cancellationToken = default) { - return await _dbContext.Set().AnyAsync(cancellationToken); + return await DbContext.Set().AnyAsync(cancellationToken); } /// @@ -191,7 +191,7 @@ public virtual IAsyncEnumerable AsAsyncEnumerable(ISpecification specifica /// The filtered entities as an . protected virtual IQueryable ApplySpecification(ISpecification specification, bool evaluateCriteriaOnly = false) { - return _specificationEvaluator.GetQuery(_dbContext.Set().AsQueryable(), specification, evaluateCriteriaOnly); + return SpecificationEvaluator.GetQuery(DbContext.Set().AsQueryable(), specification, evaluateCriteriaOnly); } /// @@ -206,6 +206,6 @@ protected virtual IQueryable ApplySpecification(ISpecification specificati /// The filtered projected entities as an . protected virtual IQueryable ApplySpecification(ISpecification specification) { - return _specificationEvaluator.GetQuery(_dbContext.Set().AsQueryable(), specification); + return SpecificationEvaluator.GetQuery(DbContext.Set().AsQueryable(), specification); } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs index 4d000e9..6c81b2a 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs @@ -5,7 +5,7 @@ public class Repository : RepositoryBase where T : class { protected readonly TestDbContext dbContext; - public Repository(TestDbContext dbContext) : this(dbContext, SpecificationEvaluator.Default) + public Repository(TestDbContext dbContext) : this(dbContext, EntityFrameworkCore.SpecificationEvaluator.Default) { } diff --git a/sample/Ardalis.Sample.App3/RepositoryBase.cs b/sample/Ardalis.Sample.App3/RepositoryBase.cs index eac1413..7bcb5fd 100644 --- a/sample/Ardalis.Sample.App3/RepositoryBase.cs +++ b/sample/Ardalis.Sample.App3/RepositoryBase.cs @@ -9,7 +9,7 @@ namespace Ardalis.Sample.App3; public abstract class RepositoryBase : IReadRepository where T : class { - protected readonly DbContext _dbContext; + protected DbContext DbContext { get; private set; } protected readonly AutoMapper.IConfigurationProvider _configurationProvider; protected ISpecificationEvaluator Evaluator { get; } @@ -21,14 +21,14 @@ protected RepositoryBase(DbContext dbContext, IMapper mapper) protected RepositoryBase(DbContext dbContext, ISpecificationEvaluator specificationEvaluator, IMapper mapper) { - _dbContext = dbContext; + DbContext = dbContext; Evaluator = specificationEvaluator; _configurationProvider = mapper.ConfigurationProvider; } public virtual async Task AddAsync(T entity, CancellationToken cancellationToken = default) { - _dbContext.Set().Add(entity); + DbContext.Set().Add(entity); await SaveChangesAsync(cancellationToken); @@ -36,7 +36,7 @@ public virtual async Task AddAsync(T entity, CancellationToken cancellationTo } public virtual async Task> AddRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - _dbContext.Set().AddRange(entities); + DbContext.Set().AddRange(entities); await SaveChangesAsync(cancellationToken); @@ -44,37 +44,37 @@ public virtual async Task> AddRangeAsync(IEnumerable entities, } public virtual async Task UpdateAsync(T entity, CancellationToken cancellationToken = default) { - _dbContext.Set().Update(entity); + DbContext.Set().Update(entity); await SaveChangesAsync(cancellationToken); } public virtual async Task DeleteAsync(T entity, CancellationToken cancellationToken = default) { - _dbContext.Set().Remove(entity); + DbContext.Set().Remove(entity); await SaveChangesAsync(cancellationToken); } public virtual async Task DeleteRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - _dbContext.Set().RemoveRange(entities); + DbContext.Set().RemoveRange(entities); await SaveChangesAsync(cancellationToken); } public virtual async Task DeleteRangeAsync(ISpecification specification, CancellationToken cancellationToken = default) { var query = ApplySpecification(specification); - _dbContext.Set().RemoveRange(query); + DbContext.Set().RemoveRange(query); await SaveChangesAsync(cancellationToken); } public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = default) { - return await _dbContext.SaveChangesAsync(cancellationToken); + return await DbContext.SaveChangesAsync(cancellationToken); } public virtual async Task FindAsync(TId id, CancellationToken cancellationToken = default) where TId : notnull { - return await _dbContext.Set().FindAsync(new object[] { id }, cancellationToken: cancellationToken); + return await DbContext.Set().FindAsync(new object[] { id }, cancellationToken: cancellationToken); } public async Task FirstOrDefaultAsync(ISpecification specification, CancellationToken cancellationToken = default) { @@ -94,7 +94,7 @@ public virtual async Task SaveChangesAsync(CancellationToken cancellationTo } public virtual async Task> ListAsync(CancellationToken cancellationToken = default) { - return await _dbContext.Set().ToListAsync(cancellationToken); + return await DbContext.Set().ToListAsync(cancellationToken); } public virtual async Task> ListAsync(ISpecification specification, CancellationToken cancellationToken = default) { @@ -114,7 +114,7 @@ public virtual async Task CountAsync(ISpecification specification, Cance } public virtual async Task CountAsync(CancellationToken cancellationToken = default) { - return await _dbContext.Set().CountAsync(cancellationToken); + return await DbContext.Set().CountAsync(cancellationToken); } public virtual async Task AnyAsync(ISpecification specification, CancellationToken cancellationToken = default) { @@ -122,7 +122,7 @@ public virtual async Task AnyAsync(ISpecification specification, Cancel } public virtual async Task AnyAsync(CancellationToken cancellationToken = default) { - return await _dbContext.Set().AnyAsync(cancellationToken); + return await DbContext.Set().AnyAsync(cancellationToken); } public async Task ProjectToFirstOrDefaultAsync(ISpecification specification, CancellationToken cancellationToken) @@ -155,10 +155,10 @@ public async Task> ProjectToListAsync(ISpecifica protected virtual IQueryable ApplySpecification(ISpecification specification, bool evaluateCriteriaOnly = false) { - return Evaluator.GetQuery(_dbContext.Set(), specification, evaluateCriteriaOnly); + return Evaluator.GetQuery(DbContext.Set(), specification, evaluateCriteriaOnly); } protected virtual IQueryable ApplySpecification(ISpecification specification) { - return Evaluator.GetQuery(_dbContext.Set(), specification); + return Evaluator.GetQuery(DbContext.Set(), specification); } } From e63126ada4bf2356a77139d11bbd3c0bfd656d2e Mon Sep 17 00:00:00 2001 From: Ruben Nielsen Date: Tue, 14 May 2024 06:07:37 +0200 Subject: [PATCH 4/4] Make the setters protected rather than private. This affords maximum extensibility, while still encapsulating the values within a property --- .../RepositoryBaseOfT.cs | 4 ++-- .../RepositoryBaseOfT.cs | 4 ++-- sample/Ardalis.Sample.App3/RepositoryBase.cs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs index 6af066d..0a2e2b7 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs @@ -10,8 +10,8 @@ namespace Ardalis.Specification.EntityFramework6; /// public abstract class RepositoryBase : IRepositoryBase where T : class { - protected DbContext DbContext { get; private set; } - protected ISpecificationEvaluator SpecificationEvaluator { get; private set; } + protected DbContext DbContext { get; set; } + protected ISpecificationEvaluator SpecificationEvaluator { get; set; } public RepositoryBase(DbContext dbContext) : this(dbContext, EntityFramework6.SpecificationEvaluator.Default) diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs index bd6010d..e9118d4 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs @@ -5,8 +5,8 @@ namespace Ardalis.Specification.EntityFrameworkCore; /// public abstract class RepositoryBase : IRepositoryBase where T : class { - protected DbContext DbContext { get; private set; } - protected ISpecificationEvaluator SpecificationEvaluator { get; private set; } + protected DbContext DbContext { get; set; } + protected ISpecificationEvaluator SpecificationEvaluator { get; set; } public RepositoryBase(DbContext dbContext) : this(dbContext, EntityFrameworkCore.SpecificationEvaluator.Default) diff --git a/sample/Ardalis.Sample.App3/RepositoryBase.cs b/sample/Ardalis.Sample.App3/RepositoryBase.cs index 7bcb5fd..accb419 100644 --- a/sample/Ardalis.Sample.App3/RepositoryBase.cs +++ b/sample/Ardalis.Sample.App3/RepositoryBase.cs @@ -9,7 +9,7 @@ namespace Ardalis.Sample.App3; public abstract class RepositoryBase : IReadRepository where T : class { - protected DbContext DbContext { get; private set; } + protected DbContext DbContext { get; set; } protected readonly AutoMapper.IConfigurationProvider _configurationProvider; protected ISpecificationEvaluator Evaluator { get; }