Skip to content

Commit 7207538

Browse files
Added a new variant method to the ICanFind trait
1 parent 5245879 commit 7207538

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

src/DotNetToolkit.Repository/RepositoryAsyncBase.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,21 @@ protected RepositoryAsyncBase(ILogger logger) : base(logger)
663663
return GetEntitiesAsync(criteria, options, cancellationToken);
664664
}
665665

666+
/// <summary>
667+
/// Asynchronously finds the collection of projected entity results in the repository.
668+
/// </summary>
669+
/// <param name="selector">A function to project each entity into a new form.</param>
670+
/// <param name="options">The options to apply to the query.</param>
671+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
672+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing the collection of projected entity results in the repository.</returns>
673+
public Task<IEnumerable<TResult>> FindAllAsync<TResult>(Expression<Func<TEntity, TResult>> selector, IQueryOptions<TEntity> options = null, CancellationToken cancellationToken = new CancellationToken())
674+
{
675+
if (selector == null)
676+
throw new ArgumentNullException(nameof(selector));
677+
678+
return GetEntitiesAsync((ISpecification<TEntity>)null, options, selector, cancellationToken);
679+
}
680+
666681
/// <summary>
667682
/// Asynchronously finds the collection of projected entity results in the repository that satisfied the criteria specified by the <paramref name="predicate" />.
668683
/// </summary>

src/DotNetToolkit.Repository/RepositoryBase.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,6 @@ protected virtual IEnumerable<TEntity> GetEntities(ISpecification<TEntity> crite
162162
/// </summary>
163163
protected virtual IEnumerable<TResult> GetEntities<TResult>(ISpecification<TEntity> criteria, IQueryOptions<TEntity> options, Expression<Func<TEntity, TResult>> selector)
164164
{
165-
if (criteria == null)
166-
throw new ArgumentNullException(nameof(criteria));
167-
168165
if (selector == null)
169166
throw new ArgumentNullException(nameof(selector));
170167

@@ -808,6 +805,20 @@ public IEnumerable<TEntity> FindAll(ISpecification<TEntity> criteria, IQueryOpti
808805
return GetEntities(criteria, options);
809806
}
810807

808+
/// <summary>
809+
/// Finds the collection of projected entity results in the repository.
810+
/// </summary>
811+
/// <param name="selector">A function to project each entity into a new form.</param>
812+
/// <param name="options">The options to apply to the query.</param>
813+
/// <returns>The collection of projected entity results in the repository.</returns>
814+
public IEnumerable<TResult> FindAll<TResult>(Expression<Func<TEntity, TResult>> selector, IQueryOptions<TEntity> options = null)
815+
{
816+
if (selector == null)
817+
throw new ArgumentNullException(nameof(selector));
818+
819+
return GetEntities((ISpecification<TEntity>)null, options, selector);
820+
}
821+
811822
/// <summary>
812823
/// Finds the collection of projected entity results in the repository that satisfied the criteria specified by the <paramref name="predicate" />.
813824
/// </summary>

src/DotNetToolkit.Repository/Traits/ICanFind.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface ICanFind<TEntity> where TEntity : class
1818
/// <param name="predicate">A function to filter each entity.</param>
1919
/// <param name="options">The options to apply to the query.</param>
2020
/// <returns>The entity that satisfied the criteria specified by the <paramref name="predicate" /> in the repository.</returns>
21-
TEntity Find(Expression<Func<TEntity, bool>> predicate, IQueryOptions<TEntity> options = null);
21+
TEntity Find(Expression<Func<TEntity, bool>> predicate, IQueryOptions<TEntity> options = null);
2222

2323
/// <summary>
2424
/// Finds the first entity in the repository that satisfies the criteria specified by the <paramref name="criteria" /> in the repository.
@@ -68,6 +68,14 @@ public interface ICanFind<TEntity> where TEntity : class
6868
/// <returns>The collection of entities in the repository that satisfied the criteria specified by the <paramref name="criteria" />.</returns>
6969
IEnumerable<TEntity> FindAll(ISpecification<TEntity> criteria, IQueryOptions<TEntity> options = null);
7070

71+
/// <summary>
72+
/// Finds the collection of projected entity results in the repository.
73+
/// </summary>
74+
/// <param name="selector">A function to project each entity into a new form.</param>
75+
/// <param name="options">The options to apply to the query.</param>
76+
/// <returns>The collection of projected entity results in the repository.</returns>
77+
IEnumerable<TResult> FindAll<TResult>(Expression<Func<TEntity, TResult>> selector, IQueryOptions<TEntity> options = null);
78+
7179
/// <summary>
7280
/// Finds the collection of projected entity results in the repository that satisfied the criteria specified by the <paramref name="predicate" />.
7381
/// </summary>

src/DotNetToolkit.Repository/Traits/ICanFindAsync.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ public interface ICanFindAsync<TEntity> where TEntity : class
7676
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing the collection of entities in the repository that satisfied the criteria specified by the <paramref name="criteria" />.</returns>
7777
Task<IEnumerable<TEntity>> FindAllAsync(ISpecification<TEntity> criteria, IQueryOptions<TEntity> options = null, CancellationToken cancellationToken = default(CancellationToken));
7878

79+
/// <summary>
80+
/// Asynchronously finds the collection of projected entity results in the repository.
81+
/// </summary>
82+
/// <param name="selector">A function to project each entity into a new form.</param>
83+
/// <param name="options">The options to apply to the query.</param>
84+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
85+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing the collection of projected entity results in the repository.</returns>
86+
Task<IEnumerable<TResult>> FindAllAsync<TResult>(Expression<Func<TEntity, TResult>> selector, IQueryOptions<TEntity> options = null, CancellationToken cancellationToken = default(CancellationToken));
87+
7988
/// <summary>
8089
/// Asynchronously finds the collection of projected entity results in the repository that satisfied the criteria specified by the <paramref name="predicate" />.
8190
/// </summary>

test/DotNetToolkit.Repository.Integration.Test/RepositoryTraitsTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ private static void TestFindAll(IRepository<Customer, int> repo)
501501
Assert.Empty(repo.FindAll(x => x.Name.Equals(name)));
502502
Assert.Empty(repo.FindAll(spec));
503503
Assert.Empty(repo.FindAll<string>(x => x.Name.Equals(name), x => x.Name));
504+
Assert.Empty(repo.FindAll<string>(x => x.Name));
504505
Assert.Empty(repo.FindAll<string>(spec, x => x.Name));
505506

506507
repo.Add(entity);
@@ -509,6 +510,7 @@ private static void TestFindAll(IRepository<Customer, int> repo)
509510
Assert.Single(repo.FindAll(x => x.Name.Equals(name)));
510511
Assert.Single(repo.FindAll(spec));
511512
Assert.Single(repo.FindAll<string>(x => x.Name.Equals(name), x => x.Name));
513+
Assert.Single(repo.FindAll<string>(x => x.Name));
512514
Assert.Single(repo.FindAll<string>(spec, x => x.Name));
513515
}
514516

@@ -1456,6 +1458,7 @@ private static async Task TestFindAllAsync(IRepositoryAsync<Customer, int> repo)
14561458
Assert.Empty(await repo.FindAllAsync(x => x.Name.Equals(name)));
14571459
Assert.Empty(await repo.FindAllAsync(spec));
14581460
Assert.Empty(await repo.FindAllAsync<string>(x => x.Name.Equals(name), x => x.Name));
1461+
Assert.Empty(await repo.FindAllAsync<string>(x => x.Name));
14591462
Assert.Empty(await repo.FindAllAsync<string>(spec, x => x.Name));
14601463

14611464
await repo.AddAsync(entity);
@@ -1464,6 +1467,7 @@ private static async Task TestFindAllAsync(IRepositoryAsync<Customer, int> repo)
14641467
Assert.Single(await repo.FindAllAsync(x => x.Name.Equals(name)));
14651468
Assert.Single(await repo.FindAllAsync(spec));
14661469
Assert.Single(await repo.FindAllAsync<string>(x => x.Name.Equals(name), x => x.Name));
1470+
Assert.Single(await repo.FindAllAsync<string>(x => x.Name));
14671471
Assert.Single(await repo.FindAllAsync<string>(spec, x => x.Name));
14681472
}
14691473

0 commit comments

Comments
 (0)