Skip to content

Commit 570ec85

Browse files
authored
Add CountAsync method in the generic repositories. (ardalis#141)
* Added CountAsync method in the generic repositories. * Fixed xml comment.
1 parent 7e83560 commit 570ec85

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

ArdalisSpecification/src/Ardalis.Specification/IReadRepositoryBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,14 @@ public interface IReadRepositoryBase<T> where T : class
9090
/// number of elements in the input sequence.
9191
/// </returns>
9292
Task<int> CountAsync(ISpecification<T> specification, CancellationToken cancellationToken = default);
93+
94+
/// <summary>
95+
/// Returns the total number of records.
96+
/// </summary>
97+
/// <returns>
98+
/// A task that represents the asynchronous operation. The task result contains the
99+
/// number of elements in the input sequence.
100+
/// </returns>
101+
Task<int> CountAsync(CancellationToken cancellationToken = default);
93102
}
94103
}

ArdalisSpecificationEF/src/Ardalis.Specification.EF/RepositoryBaseOfT.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ public virtual async Task<int> CountAsync(ISpecification<T> specification, Cance
103103
return await ApplySpecification(specification, true).CountAsync(cancellationToken);
104104
}
105105

106+
/// <inheritdoc/>
107+
public virtual async Task<int> CountAsync(CancellationToken cancellationToken = default)
108+
{
109+
return await dbContext.Set<T>().CountAsync(cancellationToken);
110+
}
111+
106112
/// <summary>
107113
/// Filters the entities of <typeparamref name="T"/>, to those that match the encapsulated query logic of the
108114
/// <paramref name="specification"/>.

ArdalisSpecificationEF/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ public virtual async Task<int> CountAsync(ISpecification<T> specification, Cance
103103
return await ApplySpecification(specification, true).CountAsync(cancellationToken);
104104
}
105105

106+
/// <inheritdoc/>
107+
public virtual async Task<int> CountAsync(CancellationToken cancellationToken = default)
108+
{
109+
return await dbContext.Set<T>().CountAsync(cancellationToken);
110+
}
111+
106112
/// <summary>
107113
/// Filters the entities of <typeparamref name="T"/>, to those that match the encapsulated query logic of the
108114
/// <paramref name="specification"/>.

sample/Ardalis.SampleApp.Infrastructure/Data/CachedCustomerRepository.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public Task<int> CountAsync(Specification.ISpecification<T> specification, Cance
3636
return _sourceRepository.CountAsync(specification, cancellationToken);
3737
}
3838

39+
/// <inheritdoc/>
40+
public Task<int> CountAsync(CancellationToken cancellationToken = default)
41+
{
42+
// TODO: Add Caching
43+
return _sourceRepository.CountAsync(cancellationToken);
44+
}
45+
3946
/// <inheritdoc/>
4047
public Task<T> GetByIdAsync(int id, CancellationToken cancellationToken = default)
4148
{

0 commit comments

Comments
 (0)