Skip to content

Commit

Permalink
Update repository methods. (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
fiseni authored Mar 9, 2025
1 parent a10fb19 commit b65baa5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 113 deletions.
39 changes: 15 additions & 24 deletions src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,47 +41,52 @@ public virtual async Task<IEnumerable<T>> AddRangeAsync(IEnumerable<T> entities,
}

/// <inheritdoc/>
public virtual async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
public virtual async Task<int> UpdateAsync(T entity, CancellationToken cancellationToken = default)
{
DbContext.Entry(entity).State = EntityState.Modified;

await SaveChangesAsync(cancellationToken);
var result = await SaveChangesAsync(cancellationToken);
return result;
}

/// <inheritdoc/>
public virtual async Task UpdateRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
public virtual async Task<int> UpdateRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
{
foreach (var entity in entities)
{
DbContext.Entry(entity).State = EntityState.Modified;
}

await SaveChangesAsync(cancellationToken);
var result = await SaveChangesAsync(cancellationToken);
return result;
}

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

await SaveChangesAsync(cancellationToken);
var result = await SaveChangesAsync(cancellationToken);
return result;
}

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

await SaveChangesAsync(cancellationToken);
var result = await SaveChangesAsync(cancellationToken);
return result;
}

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

await SaveChangesAsync(cancellationToken);
var result = await SaveChangesAsync(cancellationToken);
return result;
}

/// <inheritdoc/>
Expand All @@ -96,20 +101,6 @@ public virtual async Task<T> GetByIdAsync<TId>(TId id, CancellationToken cancell
return await DbContext.Set<T>().FindAsync(cancellationToken: cancellationToken, new object[] { id });
}

/// <inheritdoc/>
[Obsolete("Use FirstOrDefaultAsync<T> or SingleOrDefaultAsync<T> instead. The SingleOrDefaultAsync<T> can be applied only to SingleResultSpecification<T> specifications.")]
public virtual async Task<T> GetBySpecAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
{
return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken);
}

/// <inheritdoc/>
[Obsolete("Use FirstOrDefaultAsync<T> or SingleOrDefaultAsync<T> instead. The SingleOrDefaultAsync<T> can be applied only to SingleResultSpecification<T> specifications.")]
public virtual async Task<TResult> GetBySpecAsync<TResult>(ISpecification<T, TResult> specification, CancellationToken cancellationToken = default)
{
return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken);
}

/// <inheritdoc/>
public virtual async Task<T> FirstOrDefaultAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@ public ContextFactoryRepositoryBaseOfT(IDbContextFactory<TContext> dbContextFact
return await dbContext.Set<TEntity>().FindAsync(new object[] { id }, cancellationToken: cancellationToken);
}

/// <inheritdoc/>
public async Task<TEntity?> GetBySpecAsync(ISpecification<TEntity> specification, CancellationToken cancellationToken = default)
{
await using var dbContext = _dbContextFactory.CreateDbContext();
return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken);
}

/// <inheritdoc/>
public async Task<TResult?> GetBySpecAsync<TResult>(ISpecification<TEntity, TResult> specification, CancellationToken cancellationToken = default)
{
await using var dbContext = _dbContextFactory.CreateDbContext();
return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken);
}

/// <inheritdoc/>
public async Task<TEntity?> FirstOrDefaultAsync(ISpecification<TEntity> specification, CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -152,49 +138,54 @@ public async Task<IEnumerable<TEntity>> AddRangeAsync(IEnumerable<TEntity> entit
}

/// <inheritdoc/>
public async Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
public async Task<int> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
{
await using var dbContext = _dbContextFactory.CreateDbContext();
dbContext.Set<TEntity>().Update(entity);

await SaveChangesAsync(dbContext, cancellationToken);
var result = await SaveChangesAsync(dbContext, cancellationToken);
return result;
}

/// <inheritdoc/>
public async Task UpdateRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
public async Task<int> UpdateRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
{
await using var dbContext = _dbContextFactory.CreateDbContext();
dbContext.Set<TEntity>().UpdateRange(entities);

await SaveChangesAsync(dbContext, cancellationToken);
var result = await SaveChangesAsync(dbContext, cancellationToken);
return result;
}

/// <inheritdoc/>
public async Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default)
public async Task<int> DeleteAsync(TEntity entity, CancellationToken cancellationToken = default)
{
await using var dbContext = _dbContextFactory.CreateDbContext();
dbContext.Set<TEntity>().Remove(entity);

await SaveChangesAsync(dbContext, cancellationToken);
var result = await SaveChangesAsync(dbContext, cancellationToken);
return result;
}

/// <inheritdoc/>
public async Task DeleteRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
public async Task<int> DeleteRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
{
await using var dbContext = _dbContextFactory.CreateDbContext();
dbContext.Set<TEntity>().RemoveRange(entities);

await SaveChangesAsync(dbContext, cancellationToken);
var result = await SaveChangesAsync(dbContext, cancellationToken);
return result;
}

/// <inheritdoc/>
public async Task DeleteRangeAsync(ISpecification<TEntity> specification, CancellationToken cancellationToken = default)
public async Task<int> DeleteRangeAsync(ISpecification<TEntity> specification, CancellationToken cancellationToken = default)
{
await using var dbContext = _dbContextFactory.CreateDbContext();
var query = ApplySpecification(specification, dbContext);
dbContext.Set<TEntity>().RemoveRange(query);

await SaveChangesAsync(cancellationToken);
var result = await SaveChangesAsync(dbContext, cancellationToken);
return result;
}

/// <inheritdoc/>
Expand Down
39 changes: 15 additions & 24 deletions src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,44 +39,49 @@ public virtual async Task<IEnumerable<T>> AddRangeAsync(IEnumerable<T> entities,
}

/// <inheritdoc/>
public virtual async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
public virtual async Task<int> UpdateAsync(T entity, CancellationToken cancellationToken = default)
{
DbContext.Set<T>().Update(entity);

await SaveChangesAsync(cancellationToken);
var result = await SaveChangesAsync(cancellationToken);
return result;
}

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

await SaveChangesAsync(cancellationToken);
var result = await SaveChangesAsync(cancellationToken);
return result;
}

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

await SaveChangesAsync(cancellationToken);
var result = await SaveChangesAsync(cancellationToken);
return result;
}

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

await SaveChangesAsync(cancellationToken);
var result = await SaveChangesAsync(cancellationToken);
return result;
}

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

await SaveChangesAsync(cancellationToken);
var result = await SaveChangesAsync(cancellationToken);
return result;
}

/// <inheritdoc/>
Expand All @@ -91,20 +96,6 @@ public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationTo
return await DbContext.Set<T>().FindAsync(new object[] { id }, cancellationToken: cancellationToken);
}

/// <inheritdoc/>
[Obsolete("Use FirstOrDefaultAsync<T> or SingleOrDefaultAsync<T> instead. The SingleOrDefaultAsync<T> can be applied only to SingleResultSpecification<T> specifications.")]
public virtual async Task<T?> GetBySpecAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
{
return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken);
}

/// <inheritdoc/>
[Obsolete("Use FirstOrDefaultAsync<T> or SingleOrDefaultAsync<T> instead. The SingleOrDefaultAsync<T> can be applied only to SingleResultSpecification<T> specifications.")]
public virtual async Task<TResult?> GetBySpecAsync<TResult>(ISpecification<T, TResult> specification, CancellationToken cancellationToken = default)
{
return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken);
}

/// <inheritdoc/>
public virtual async Task<T?> FirstOrDefaultAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
{
Expand Down
23 changes: 0 additions & 23 deletions src/Ardalis.Specification/IReadRepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,6 @@ public interface IReadRepositoryBase<T> where T : class
/// </returns>
Task<T?> GetByIdAsync<TId>(TId id, CancellationToken cancellationToken = default) where TId : notnull;

/// <summary>
/// Finds an entity that matches the encapsulated query logic of the <paramref name="specification"/>.
/// </summary>
/// <param name="specification">The encapsulated query logic.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains the <typeparamref name="T" />, or <see langword="null"/>.
/// </returns>
[Obsolete("Use FirstOrDefaultAsync<T> or SingleOrDefaultAsync<T> instead. The SingleOrDefaultAsync<T> can be applied only to SingleResultSpecification<T> specifications.")]
Task<T?> GetBySpecAsync(ISpecification<T> specification, CancellationToken cancellationToken = default);

/// <summary>
/// Finds an entity that matches the encapsulated query logic of the <paramref name="specification"/>.
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="specification">The encapsulated query logic.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains the <typeparamref name="TResult" />.
/// </returns>
[Obsolete("Use FirstOrDefaultAsync<T> or SingleOrDefaultAsync<T> instead. The SingleOrDefaultAsync<T> can be applied only to SingleResultSpecification<T> specifications.")]
Task<TResult?> GetBySpecAsync<TResult>(ISpecification<T, TResult> specification, CancellationToken cancellationToken = default);

/// <summary>
/// Returns the first element of a sequence, or a default value if the sequence contains no elements.
/// </summary>
Expand Down
20 changes: 10 additions & 10 deletions src/Ardalis.Specification/IRepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,38 @@ public interface IRepositoryBase<T> : IReadRepositoryBase<T> where T : class
/// Updates an entity in the database
/// </summary>
/// <param name="entity">The entity to update.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task UpdateAsync(T entity, CancellationToken cancellationToken = default);
/// <returns>A task that represents the asynchronous operation. The task result contains the number of state entries written to the database.</returns>
Task<int> UpdateAsync(T entity, CancellationToken cancellationToken = default);

/// <summary>
/// Updates the given entities in the database
/// </summary>
/// <param name="entities">The entities to update.</param>
/// <param name="cancellationToken"></param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task UpdateRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default);
/// <returns>A task that represents the asynchronous operation. The task result contains the number of state entries written to the database.</returns>
Task<int> UpdateRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default);

/// <summary>
/// Removes an entity in the database
/// </summary>
/// <param name="entity">The entity to delete.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task DeleteAsync(T entity, CancellationToken cancellationToken = default);
/// <returns>A task that represents the asynchronous operation. The task result contains the number of state entries written to the database.</returns>
Task<int> DeleteAsync(T entity, CancellationToken cancellationToken = default);

/// <summary>
/// Removes the given entities in the database
/// </summary>
/// <param name="entities">The entities to remove.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default);
/// <returns>A task that represents the asynchronous operation. The task result contains the number of state entries written to the database.</returns>
Task<int> DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default);

/// <summary>
/// Removes the all entities of <typeparamref name="T" />, that matches the encapsulated query logic of the
/// <paramref name="specification"/>, from the database.
/// </summary>
/// <param name="specification">The encapsulated query logic.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default);
/// <returns>A task that represents the asynchronous operation. The task result contains the number of state entries written to the database.</returns>
Task<int> DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default);

/// <summary>
/// Persists changes to the database.
Expand Down
Loading

0 comments on commit b65baa5

Please sign in to comment.