-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathIReadRepositoryBase.cs
More file actions
162 lines (148 loc) · 8.09 KB
/
IReadRepositoryBase.cs
File metadata and controls
162 lines (148 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
namespace Ardalis.Specification;
/// <summary>
/// <para>
/// A <see cref="IRepositoryBase{T}" /> can be used to query instances of <typeparamref name="T" />.
/// An <see cref="ISpecification{T}"/> (or derived) is used to encapsulate the LINQ queries against the database.
/// </para>
/// </summary>
/// <typeparam name="T">The type of entity being operated on by this repository.</typeparam>
public interface IReadRepositoryBase<T> where T : class
{
/// <summary>
/// Finds an entity with the given primary key value.
/// </summary>
/// <typeparam name="TId">The type of primary key.</typeparam>
/// <param name="id">The value of the primary key for the entity to be found.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains the <typeparamref name="T" />, or <see langword="null"/>.
/// </returns>
Task<T?> GetByIdAsync<TId>(TId id, CancellationToken cancellationToken = default) where TId : notnull;
/// <summary>
/// Returns the first element of a sequence, or a default value if the sequence contains no elements.
/// </summary>
/// <param name="specification">The encapsulated query logic.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains the <typeparamref name="T" />, or <see langword="null"/>.
/// </returns>
Task<T?> FirstOrDefaultAsync(ISpecification<T> specification, CancellationToken cancellationToken = default);
/// <summary>
/// Returns the first element of a sequence, or a default value if the sequence contains no elements.
/// </summary>
/// <param name="specification">The encapsulated query logic.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains the <typeparamref name="TResult" />, or <see langword="null"/>.
/// </returns>
Task<TResult?> FirstOrDefaultAsync<TResult>(ISpecification<T, TResult> specification, CancellationToken cancellationToken = default);
/// <summary>
/// Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.
/// </summary>
/// <param name="specification">The encapsulated query logic.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains the <typeparamref name="T" />, or <see langword="null"/>.
/// </returns>
Task<T?> SingleOrDefaultAsync(ISingleResultSpecification<T> specification, CancellationToken cancellationToken = default);
/// <summary>
/// Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.
/// </summary>
/// <param name="specification">The encapsulated query logic.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains the <typeparamref name="TResult" />, or <see langword="null"/>.
/// </returns>
Task<TResult?> SingleOrDefaultAsync<TResult>(ISingleResultSpecification<T, TResult> specification, CancellationToken cancellationToken = default);
/// <summary>
/// Finds all entities of <typeparamref name="T" /> from the database.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains a <see cref="List{T}" /> that contains elements from the input sequence.
/// </returns>
Task<List<T>> ListAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Finds all entities of <typeparamref name="T" />, that matches the encapsulated query logic of the
/// <paramref name="specification"/>, from the database.
/// </summary>
/// <param name="specification">The encapsulated query logic.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains a <see cref="List{T}" /> that contains elements from the input sequence.
/// </returns>
Task<List<T>> ListAsync(ISpecification<T> specification, CancellationToken cancellationToken = default);
/// <summary>
/// Finds all entities of <typeparamref name="T" />, that matches the encapsulated query logic of the
/// <paramref name="specification"/>, from the database.
/// <para>
/// Projects each entity into a new form, being <typeparamref name="TResult" />.
/// </para>
/// </summary>
/// <typeparam name="TResult">The type of the value returned by the projection.</typeparam>
/// <param name="specification">The encapsulated query logic.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains a <see cref="List{TResult}" /> that contains elements from the input sequence.
/// </returns>
Task<List<TResult>> ListAsync<TResult>(ISpecification<T, TResult> specification, CancellationToken cancellationToken = default);
/// <summary>
/// Returns a number that represents how many entities satisfy the encapsulated query logic
/// of the <paramref name="specification"/>.
/// </summary>
/// <param name="specification">The encapsulated query logic.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A task that represents the asynchronous operation. The task result contains the
/// number of elements in the input sequence.
/// </returns>
Task<int> CountAsync(ISpecification<T> specification, CancellationToken cancellationToken = default);
/// <summary>
/// Returns the total number of records.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A task that represents the asynchronous operation. The task result contains the
/// number of elements in the input sequence.
/// </returns>
Task<int> CountAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Returns a boolean that represents whether any entity satisfy the encapsulated query logic
/// of the <paramref name="specification"/> or not.
/// </summary>
/// <param name="specification">The encapsulated query logic.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A task that represents the asynchronous operation. The task result contains true if the
/// source sequence contains any elements; otherwise, false.
/// </returns>
Task<bool> AnyAsync(ISpecification<T> specification, CancellationToken cancellationToken = default);
/// <summary>
/// Returns a boolean whether any entity exists or not.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A task that represents the asynchronous operation. The task result contains true if the
/// source sequence contains any elements; otherwise, false.
/// </returns>
Task<bool> AnyAsync(CancellationToken cancellationToken = default);
#if NET8_0_OR_GREATER
/// <summary>
/// Finds all entities of <typeparamref name="T" />, that matches the encapsulated query logic of the
/// <paramref name="specification"/>, from the database.
/// </summary>
/// <param name="specification">The encapsulated query logic.</param>
/// <returns>
/// Returns an IAsyncEnumerable which can be enumerated asynchronously.
/// </returns>
IAsyncEnumerable<T> AsAsyncEnumerable(ISpecification<T> specification);
#endif
}