Skip to content

Commit b65baa5

Browse files
authored
Update repository methods. (#450)
1 parent a10fb19 commit b65baa5

File tree

6 files changed

+63
-113
lines changed

6 files changed

+63
-113
lines changed

src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,47 +41,52 @@ public virtual async Task<IEnumerable<T>> AddRangeAsync(IEnumerable<T> entities,
4141
}
4242

4343
/// <inheritdoc/>
44-
public virtual async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
44+
public virtual async Task<int> UpdateAsync(T entity, CancellationToken cancellationToken = default)
4545
{
4646
DbContext.Entry(entity).State = EntityState.Modified;
4747

48-
await SaveChangesAsync(cancellationToken);
48+
var result = await SaveChangesAsync(cancellationToken);
49+
return result;
4950
}
5051

5152
/// <inheritdoc/>
52-
public virtual async Task UpdateRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
53+
public virtual async Task<int> UpdateRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
5354
{
5455
foreach (var entity in entities)
5556
{
5657
DbContext.Entry(entity).State = EntityState.Modified;
5758
}
5859

59-
await SaveChangesAsync(cancellationToken);
60+
var result = await SaveChangesAsync(cancellationToken);
61+
return result;
6062
}
6163

6264
/// <inheritdoc/>
63-
public virtual async Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
65+
public virtual async Task<int> DeleteAsync(T entity, CancellationToken cancellationToken = default)
6466
{
6567
DbContext.Set<T>().Remove(entity);
6668

67-
await SaveChangesAsync(cancellationToken);
69+
var result = await SaveChangesAsync(cancellationToken);
70+
return result;
6871
}
6972

7073
/// <inheritdoc/>
71-
public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
74+
public virtual async Task<int> DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
7275
{
7376
DbContext.Set<T>().RemoveRange(entities);
7477

75-
await SaveChangesAsync(cancellationToken);
78+
var result = await SaveChangesAsync(cancellationToken);
79+
return result;
7680
}
7781

7882
/// <inheritdoc/>
79-
public virtual async Task DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
83+
public virtual async Task<int> DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
8084
{
8185
var query = ApplySpecification(specification);
8286
DbContext.Set<T>().RemoveRange(query);
8387

84-
await SaveChangesAsync(cancellationToken);
88+
var result = await SaveChangesAsync(cancellationToken);
89+
return result;
8590
}
8691

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

99-
/// <inheritdoc/>
100-
[Obsolete("Use FirstOrDefaultAsync<T> or SingleOrDefaultAsync<T> instead. The SingleOrDefaultAsync<T> can be applied only to SingleResultSpecification<T> specifications.")]
101-
public virtual async Task<T> GetBySpecAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
102-
{
103-
return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken);
104-
}
105-
106-
/// <inheritdoc/>
107-
[Obsolete("Use FirstOrDefaultAsync<T> or SingleOrDefaultAsync<T> instead. The SingleOrDefaultAsync<T> can be applied only to SingleResultSpecification<T> specifications.")]
108-
public virtual async Task<TResult> GetBySpecAsync<TResult>(ISpecification<T, TResult> specification, CancellationToken cancellationToken = default)
109-
{
110-
return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken);
111-
}
112-
113104
/// <inheritdoc/>
114105
public virtual async Task<T> FirstOrDefaultAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
115106
{

src/Ardalis.Specification.EntityFrameworkCore/ContextFactoryRepositoryBaseOfT.cs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,6 @@ public ContextFactoryRepositoryBaseOfT(IDbContextFactory<TContext> dbContextFact
2626
return await dbContext.Set<TEntity>().FindAsync(new object[] { id }, cancellationToken: cancellationToken);
2727
}
2828

29-
/// <inheritdoc/>
30-
public async Task<TEntity?> GetBySpecAsync(ISpecification<TEntity> specification, CancellationToken cancellationToken = default)
31-
{
32-
await using var dbContext = _dbContextFactory.CreateDbContext();
33-
return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken);
34-
}
35-
36-
/// <inheritdoc/>
37-
public async Task<TResult?> GetBySpecAsync<TResult>(ISpecification<TEntity, TResult> specification, CancellationToken cancellationToken = default)
38-
{
39-
await using var dbContext = _dbContextFactory.CreateDbContext();
40-
return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken);
41-
}
42-
4329
/// <inheritdoc/>
4430
public async Task<TEntity?> FirstOrDefaultAsync(ISpecification<TEntity> specification, CancellationToken cancellationToken = default)
4531
{
@@ -152,49 +138,54 @@ public async Task<IEnumerable<TEntity>> AddRangeAsync(IEnumerable<TEntity> entit
152138
}
153139

154140
/// <inheritdoc/>
155-
public async Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
141+
public async Task<int> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
156142
{
157143
await using var dbContext = _dbContextFactory.CreateDbContext();
158144
dbContext.Set<TEntity>().Update(entity);
159145

160-
await SaveChangesAsync(dbContext, cancellationToken);
146+
var result = await SaveChangesAsync(dbContext, cancellationToken);
147+
return result;
161148
}
162149

163150
/// <inheritdoc/>
164-
public async Task UpdateRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
151+
public async Task<int> UpdateRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
165152
{
166153
await using var dbContext = _dbContextFactory.CreateDbContext();
167154
dbContext.Set<TEntity>().UpdateRange(entities);
168155

169-
await SaveChangesAsync(dbContext, cancellationToken);
156+
var result = await SaveChangesAsync(dbContext, cancellationToken);
157+
return result;
170158
}
171159

172160
/// <inheritdoc/>
173-
public async Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default)
161+
public async Task<int> DeleteAsync(TEntity entity, CancellationToken cancellationToken = default)
174162
{
175163
await using var dbContext = _dbContextFactory.CreateDbContext();
176164
dbContext.Set<TEntity>().Remove(entity);
177165

178-
await SaveChangesAsync(dbContext, cancellationToken);
166+
var result = await SaveChangesAsync(dbContext, cancellationToken);
167+
return result;
179168
}
180169

181170
/// <inheritdoc/>
182-
public async Task DeleteRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
171+
public async Task<int> DeleteRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
183172
{
184173
await using var dbContext = _dbContextFactory.CreateDbContext();
185174
dbContext.Set<TEntity>().RemoveRange(entities);
186175

187-
await SaveChangesAsync(dbContext, cancellationToken);
176+
var result = await SaveChangesAsync(dbContext, cancellationToken);
177+
return result;
188178
}
189179

190180
/// <inheritdoc/>
191-
public async Task DeleteRangeAsync(ISpecification<TEntity> specification, CancellationToken cancellationToken = default)
181+
public async Task<int> DeleteRangeAsync(ISpecification<TEntity> specification, CancellationToken cancellationToken = default)
192182
{
193183
await using var dbContext = _dbContextFactory.CreateDbContext();
194184
var query = ApplySpecification(specification, dbContext);
195185
dbContext.Set<TEntity>().RemoveRange(query);
196186

197-
await SaveChangesAsync(cancellationToken);
187+
var result = await SaveChangesAsync(dbContext, cancellationToken);
188+
return result;
198189
}
199190

200191
/// <inheritdoc/>

src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,44 +39,49 @@ public virtual async Task<IEnumerable<T>> AddRangeAsync(IEnumerable<T> entities,
3939
}
4040

4141
/// <inheritdoc/>
42-
public virtual async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
42+
public virtual async Task<int> UpdateAsync(T entity, CancellationToken cancellationToken = default)
4343
{
4444
DbContext.Set<T>().Update(entity);
4545

46-
await SaveChangesAsync(cancellationToken);
46+
var result = await SaveChangesAsync(cancellationToken);
47+
return result;
4748
}
4849

4950
/// <inheritdoc/>
50-
public virtual async Task UpdateRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
51+
public virtual async Task<int> UpdateRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
5152
{
5253
DbContext.Set<T>().UpdateRange(entities);
5354

54-
await SaveChangesAsync(cancellationToken);
55+
var result = await SaveChangesAsync(cancellationToken);
56+
return result;
5557
}
5658

5759
/// <inheritdoc/>
58-
public virtual async Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
60+
public virtual async Task<int> DeleteAsync(T entity, CancellationToken cancellationToken = default)
5961
{
6062
DbContext.Set<T>().Remove(entity);
6163

62-
await SaveChangesAsync(cancellationToken);
64+
var result = await SaveChangesAsync(cancellationToken);
65+
return result;
6366
}
6467

6568
/// <inheritdoc/>
66-
public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
69+
public virtual async Task<int> DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
6770
{
6871
DbContext.Set<T>().RemoveRange(entities);
6972

70-
await SaveChangesAsync(cancellationToken);
73+
var result = await SaveChangesAsync(cancellationToken);
74+
return result;
7175
}
7276

7377
/// <inheritdoc/>
74-
public virtual async Task DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
78+
public virtual async Task<int> DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
7579
{
7680
var query = ApplySpecification(specification);
7781
DbContext.Set<T>().RemoveRange(query);
7882

79-
await SaveChangesAsync(cancellationToken);
83+
var result = await SaveChangesAsync(cancellationToken);
84+
return result;
8085
}
8186

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

94-
/// <inheritdoc/>
95-
[Obsolete("Use FirstOrDefaultAsync<T> or SingleOrDefaultAsync<T> instead. The SingleOrDefaultAsync<T> can be applied only to SingleResultSpecification<T> specifications.")]
96-
public virtual async Task<T?> GetBySpecAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
97-
{
98-
return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken);
99-
}
100-
101-
/// <inheritdoc/>
102-
[Obsolete("Use FirstOrDefaultAsync<T> or SingleOrDefaultAsync<T> instead. The SingleOrDefaultAsync<T> can be applied only to SingleResultSpecification<T> specifications.")]
103-
public virtual async Task<TResult?> GetBySpecAsync<TResult>(ISpecification<T, TResult> specification, CancellationToken cancellationToken = default)
104-
{
105-
return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken);
106-
}
107-
10899
/// <inheritdoc/>
109100
public virtual async Task<T?> FirstOrDefaultAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
110101
{

src/Ardalis.Specification/IReadRepositoryBase.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,6 @@ public interface IReadRepositoryBase<T> where T : class
2121
/// </returns>
2222
Task<T?> GetByIdAsync<TId>(TId id, CancellationToken cancellationToken = default) where TId : notnull;
2323

24-
/// <summary>
25-
/// Finds an entity that matches the encapsulated query logic of the <paramref name="specification"/>.
26-
/// </summary>
27-
/// <param name="specification">The encapsulated query logic.</param>
28-
/// <returns>
29-
/// A task that represents the asynchronous operation.
30-
/// The task result contains the <typeparamref name="T" />, or <see langword="null"/>.
31-
/// </returns>
32-
[Obsolete("Use FirstOrDefaultAsync<T> or SingleOrDefaultAsync<T> instead. The SingleOrDefaultAsync<T> can be applied only to SingleResultSpecification<T> specifications.")]
33-
Task<T?> GetBySpecAsync(ISpecification<T> specification, CancellationToken cancellationToken = default);
34-
35-
/// <summary>
36-
/// Finds an entity that matches the encapsulated query logic of the <paramref name="specification"/>.
37-
/// </summary>
38-
/// <typeparam name="TResult">The type of the result.</typeparam>
39-
/// <param name="specification">The encapsulated query logic.</param>
40-
/// <returns>
41-
/// A task that represents the asynchronous operation.
42-
/// The task result contains the <typeparamref name="TResult" />.
43-
/// </returns>
44-
[Obsolete("Use FirstOrDefaultAsync<T> or SingleOrDefaultAsync<T> instead. The SingleOrDefaultAsync<T> can be applied only to SingleResultSpecification<T> specifications.")]
45-
Task<TResult?> GetBySpecAsync<TResult>(ISpecification<T, TResult> specification, CancellationToken cancellationToken = default);
46-
4724
/// <summary>
4825
/// Returns the first element of a sequence, or a default value if the sequence contains no elements.
4926
/// </summary>

src/Ardalis.Specification/IRepositoryBase.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,38 @@ public interface IRepositoryBase<T> : IReadRepositoryBase<T> where T : class
3535
/// Updates an entity in the database
3636
/// </summary>
3737
/// <param name="entity">The entity to update.</param>
38-
/// <returns>A task that represents the asynchronous operation.</returns>
39-
Task UpdateAsync(T entity, CancellationToken cancellationToken = default);
38+
/// <returns>A task that represents the asynchronous operation. The task result contains the number of state entries written to the database.</returns>
39+
Task<int> UpdateAsync(T entity, CancellationToken cancellationToken = default);
4040

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

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

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

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

7171
/// <summary>
7272
/// Persists changes to the database.

0 commit comments

Comments
 (0)