|
6 | 6 |
|
7 | 7 | namespace AutSoft.Linq.Queryable; |
8 | 8 |
|
| 9 | +/// <summary> |
| 10 | +/// Extensions methods for <see cref="IQueryable{T}"/> with extended functionality for Single and First methods/> |
| 11 | +/// </summary> |
9 | 12 | public static class FirstAndSingleExtensions |
10 | 13 | { |
11 | | - public static async Task<T> SingleEntityAsync<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate, long entityId) |
| 14 | + /// <summary> |
| 15 | + /// Similar to <see cref="EntityFrameworkQueryableExtensions.SingleOrDefaultAsync{TSource}(IQueryable{TSource}, CancellationToken)"/> |
| 16 | + /// </summary> |
| 17 | + /// <typeparam name="T">The type of the elements of <paramref name="source" />.</typeparam> |
| 18 | + /// <param name="source">An <see cref="IQueryable{T}" /> to return the single element of.</param> |
| 19 | + /// <param name="predicate">A function to test an element for a condition.</param> |
| 20 | + /// <param name="entityId">Identifier of the requested element</param> |
| 21 | + /// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param> |
| 22 | + /// <exception cref="EntityNotFoundException">Throws when no result found</exception> |
| 23 | + public static async Task<T> SingleEntityAsync<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate, long entityId, CancellationToken cancellationToken = default) |
12 | 24 | { |
13 | | - try |
14 | | - { |
15 | | - return await source.SingleAsync(predicate); |
16 | | - } |
17 | | - catch (InvalidOperationException e) |
18 | | - { |
19 | | - // Single throws an InvalidOperationException when no entities are found |
20 | | - throw EntityNotFoundException.CreateForType<T>(e, entityId); |
21 | | - } |
| 25 | + return await source.SingleOrDefaultAsync(predicate, cancellationToken) |
| 26 | + ?? throw EntityNotFoundException.CreateForType<T>(entityId); |
22 | 27 | } |
23 | 28 |
|
24 | | - public static async Task<T> SingleEntityAsync<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate, params object[] queryParameters) |
| 29 | + /// <summary> |
| 30 | + /// Similar to <see cref="EntityFrameworkQueryableExtensions.SingleOrDefaultAsync{TSource}(IQueryable{TSource}, CancellationToken)"/> |
| 31 | + /// </summary> |
| 32 | + /// <typeparam name="T">The type of the elements of <paramref name="source" />.</typeparam> |
| 33 | + /// <param name="source">An <see cref="IQueryable{T}" /> to return the single element of.</param> |
| 34 | + /// <param name="predicate">A function to test an element for a condition.</param> |
| 35 | + /// <param name="queryParameters">Query parameters of the requested element</param> |
| 36 | + /// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param> |
| 37 | + /// <exception cref="EntityNotFoundException">Throws when no result found</exception> |
| 38 | + public static async Task<T> SingleEntityAsync<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate, object[] queryParameters, CancellationToken cancellationToken = default) |
25 | 39 | { |
26 | | - try |
27 | | - { |
28 | | - return await source.SingleAsync(predicate); |
29 | | - } |
30 | | - catch (InvalidOperationException e) |
31 | | - { |
32 | | - // Single throws an InvalidOperationException when no entities are found |
33 | | - throw EntityNotFoundException.CreateForTypeCustomParams<T>(e, queryParameters); |
34 | | - } |
35 | | - } |
36 | | - |
37 | | - public static T SingleEntity<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate, params object[] queryParameters) |
38 | | - { |
39 | | - try |
40 | | - { |
41 | | - return source.Single(predicate); |
42 | | - } |
43 | | - catch (InvalidOperationException e) |
44 | | - { |
45 | | - // Single throws an InvalidOperationException when no entities are found |
46 | | - throw EntityNotFoundException.CreateForTypeCustomParams<T>(e, queryParameters); |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - public static async Task<T> FirstEntityAsync<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate) |
51 | | - { |
52 | | - try |
53 | | - { |
54 | | - return await source.FirstAsync(predicate); |
55 | | - } |
56 | | - catch (InvalidOperationException e) |
57 | | - { |
58 | | - throw EntityNotFoundException.CreateForType<T>(e, default); |
59 | | - } |
| 40 | + return await source.SingleAsync(predicate, cancellationToken) |
| 41 | + ?? throw EntityNotFoundException.CreateForTypeCustomParams<T>(queryParameters); |
60 | 42 | } |
61 | 43 | } |
0 commit comments