Skip to content

Commit b66ab3d

Browse files
Added a new trait for aggregating items from a repository.
1 parent f9cfe3f commit b66ab3d

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

src/DotNetToolkit.Repository/IRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/// </summary>
88
/// <typeparam name="TEntity">The type of the entity.</typeparam>
99
/// <typeparam name="TKey">The type of the primary key.</typeparam>
10-
public interface IRepository<TEntity, in TKey> : ICanAdd<TEntity>, ICanUpdate<TEntity>, ICanDelete<TEntity, TKey>, ICanGet<TEntity, TKey>, ICanFind<TEntity>
10+
public interface IRepository<TEntity, in TKey> : ICanAggregate<TEntity>, ICanAdd<TEntity>, ICanUpdate<TEntity>, ICanDelete<TEntity, TKey>, ICanGet<TEntity, TKey>, ICanFind<TEntity>
1111
where TEntity : class
1212
{
1313
}

src/DotNetToolkit.Repository/RepositoryBase.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected virtual IQueryable<TEntity> GetQuery(Expression<Func<TEntity, bool>> p
8585

8686
return GetQuery(new Specification<TEntity>(predicate));
8787
}
88-
88+
8989
/// <summary>
9090
/// Returns a specification for getting an entity by it's primary key.
9191
/// </summary>
@@ -116,7 +116,40 @@ protected virtual ISpecification<TEntity> ByPrimaryKeySpecification(TKey key, IF
116116
}
117117

118118
#endregion
119-
119+
120+
#region Implementation of ICanAggregate<TEntity>
121+
122+
/// <summary>
123+
/// Returns the number of entities contained in the repository.
124+
/// </summary>
125+
/// <returns>The number of entities contained in the repository.</returns>
126+
public int Count()
127+
{
128+
return Count((ISpecification<TEntity>)null);
129+
}
130+
131+
/// <summary>
132+
/// Returns the number of entities that satisfies the criteria specified by the <paramref name="criteria" /> in the repository.
133+
/// </summary>
134+
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
135+
/// <returns>The number of entities that satisfied the criteria specified by the <paramref name="criteria" /> in the repository.</returns>
136+
public int Count(ISpecification<TEntity> criteria)
137+
{
138+
return GetQuery(criteria).Count();
139+
}
140+
141+
/// <summary>
142+
/// Returns the number of entities that satisfies the criteria specified by the <paramref name="predicate" /> in the repository.
143+
/// </summary>
144+
/// <param name="predicate">A function to filter each entity.</param>
145+
/// <returns>The number of entities that satisfied the criteria specified by the <paramref name="predicate" /> in the repository.</returns>
146+
public int Count(Expression<Func<TEntity, bool>> predicate)
147+
{
148+
return GetQuery(predicate == null ? null : new Specification<TEntity>(predicate)).Count();
149+
}
150+
151+
#endregion
152+
120153
#region Implementation of ICanAdd<in TEntity>
121154

122155
/// <summary>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace DotNetToolkit.Repository.Traits
2+
{
3+
using Specifications;
4+
using System;
5+
using System.Linq.Expressions;
6+
7+
/// <summary>
8+
/// Represents a trait for aggregating items from a repository.
9+
/// </summary>
10+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
11+
public interface ICanAggregate<TEntity> where TEntity : class
12+
{
13+
/// <summary>
14+
/// Returns the number of entities contained in the repository.
15+
/// </summary>
16+
/// <returns>The number of entities contained in the repository.</returns>
17+
int Count();
18+
19+
/// <summary>
20+
/// Returns the number of entities that satisfies the criteria specified by the <paramref name="criteria" /> in the repository.
21+
/// </summary>
22+
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
23+
/// <returns>The number of entities that satisfied the criteria specified by the <paramref name="criteria" /> in the repository.</returns>
24+
int Count(ISpecification<TEntity> criteria);
25+
26+
/// <summary>
27+
/// Returns the number of entities that satisfies the criteria specified by the <paramref name="predicate" /> in the repository.
28+
/// </summary>
29+
/// <param name="predicate">A function to filter each entity.</param>
30+
/// <returns>The number of entities that satisfied the criteria specified by the <paramref name="predicate" /> in the repository.</returns>
31+
int Count(Expression<Func<TEntity, bool>> predicate);
32+
}
33+
}

0 commit comments

Comments
 (0)