Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

397 make fields and methods protected #398

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace Ardalis.Specification.EntityFramework6;
/// <inheritdoc/>
public abstract class RepositoryBase<T> : IRepositoryBase<T> where T : class
{
private readonly DbContext _dbContext;
private readonly ISpecificationEvaluator _specificationEvaluator;
protected readonly DbContext _dbContext;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a property

protected DbContext { get; private set; }

combined with

public RepositoryBase(DbContext dbContext)
{
    DbContext = dbContext;
}

is more appropriate then a protected member.

The same for SpecificationEvaluator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't want to change too much, but I see your point. If we let subclasses access these "fields" it will be safer to do "props" instead from a maintenance perspective. I've made that change and pushed

protected readonly ISpecificationEvaluator _specificationEvaluator;

public RepositoryBase(DbContext dbContext)
: this(dbContext, SpecificationEvaluator.Default)
Expand Down Expand Up @@ -78,14 +78,14 @@ public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, Cancellation
_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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace Ardalis.Specification.EntityFrameworkCore;
/// <inheritdoc/>
public abstract class RepositoryBase<T> : IRepositoryBase<T> 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)
Expand Down Expand Up @@ -62,22 +62,22 @@ public virtual async Task DeleteAsync(T entity, CancellationToken cancellationTo
_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);

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);
}

Expand Down
14 changes: 7 additions & 7 deletions sample/Ardalis.Sample.App3/RepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Ardalis.Sample.App3;

public abstract class RepositoryBase<T> : IReadRepository<T> 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
Expand Down Expand Up @@ -59,12 +59,12 @@ public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, Cancellation
_dbContext.Set<T>().RemoveRange(entities);

await SaveChangesAsync(cancellationToken);
}
}
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);
}
public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
Expand Down