Skip to content

Commit 332360b

Browse files
Merge pull request #26 from johelvisguzman/feature/issue25
Closes #25
2 parents 9b600eb + 5ede2f2 commit 332360b

File tree

9 files changed

+1462
-6
lines changed

9 files changed

+1462
-6
lines changed

src/DotNetToolkit.Repository.EntityFramework/EfRepositoryBase.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ public override void Dispose()
223223
return predicate == null ? GetQuery().AnyAsync(cancellationToken) : GetQuery().AnyAsync(predicate, cancellationToken);
224224
}
225225

226+
/// <summary>
227+
/// A protected asynchronous overridable method for getting a new <see cref="IDictionary{TDictionaryKey, TElement}" /> according to the specified <paramref name="keySelector" />, an element selector.
228+
/// </summary>
229+
protected override Task<Dictionary<TDictionaryKey, TElement>> GetDictionaryAsync<TDictionaryKey, TElement>(ISpecification<TEntity> criteria, Func<TEntity, TDictionaryKey> keySelector, Func<TEntity, TElement> elementSelector, IQueryOptions<TEntity> options, CancellationToken cancellationToken = new CancellationToken())
230+
{
231+
if (keySelector == null)
232+
throw new ArgumentNullException(nameof(keySelector));
233+
234+
return GetQuery(criteria, options).ToDictionaryAsync(keySelector, elementSelector, EqualityComparer<TDictionaryKey>.Default, cancellationToken);
235+
}
236+
226237
#endregion
227238
}
228239
}

src/DotNetToolkit.Repository.EntityFrameworkCore/EfCoreRepositoryBase.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ public override void Dispose()
223223
return predicate == null ? GetQuery().AnyAsync(cancellationToken) : GetQuery().AnyAsync(predicate, cancellationToken);
224224
}
225225

226+
/// <summary>
227+
/// A protected asynchronous overridable method for getting a new <see cref="IDictionary{TDictionaryKey, TElement}" /> according to the specified <paramref name="keySelector" />, an element selector.
228+
/// </summary>
229+
protected override Task<Dictionary<TDictionaryKey, TElement>> GetDictionaryAsync<TDictionaryKey, TElement>(ISpecification<TEntity> criteria, Func<TEntity, TDictionaryKey> keySelector, Func<TEntity, TElement> elementSelector, IQueryOptions<TEntity> options, CancellationToken cancellationToken = new CancellationToken())
230+
{
231+
if (keySelector == null)
232+
throw new ArgumentNullException(nameof(keySelector));
233+
234+
return GetQuery(criteria, options).ToDictionaryAsync(keySelector, elementSelector, EqualityComparer<TDictionaryKey>.Default, cancellationToken);
235+
}
236+
226237
#endregion
227238
}
228239
}

src/DotNetToolkit.Repository/RepositoryAsyncBase.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public abstract class RepositoryAsyncBase<TEntity, TKey> : RepositoryBase<TEntit
7777
/// </summary>
7878
protected abstract Task<bool> GetExistAsync(ISpecification<TEntity> criteria, CancellationToken cancellationToken = new CancellationToken());
7979

80+
/// <summary>
81+
/// A protected asynchronous overridable method for getting a new <see cref="Dictionary{TDictionaryKey, TElement}" /> according to the specified <paramref name="keySelector" />, an element selector.
82+
/// </summary>
83+
protected abstract Task<Dictionary<TDictionaryKey, TElement>> GetDictionaryAsync<TDictionaryKey, TElement>(ISpecification<TEntity> criteria, Func<TEntity, TDictionaryKey> keySelector, Func<TEntity, TElement> elementSelector, IQueryOptions<TEntity> options, CancellationToken cancellationToken = new CancellationToken());
84+
8085
#endregion
8186

8287
#region Implementation of ICanAggregateAsync<TEntity>
@@ -113,6 +118,142 @@ public abstract class RepositoryAsyncBase<TEntity, TKey> : RepositoryBase<TEntit
113118
return CountAsync(new Specification<TEntity>(predicate), cancellationToken);
114119
}
115120

121+
/// <summary>
122+
/// Asynchronously returns a new <see cref="Dictionary{TDictionaryKey, TEntity}" /> according to the specified <paramref name="keySelector" />.
123+
/// </summary>
124+
/// <typeparam name="TDictionaryKey">The type of the dictionary key.</typeparam>
125+
/// <param name="keySelector">A function to extract a key from each entity.</param>
126+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
127+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a new <see cref="Dictionary{TDictionaryKey, TEntity}" /> that contains keys and values.</returns>
128+
public Task<Dictionary<TDictionaryKey, TEntity>> ToDictionaryAsync<TDictionaryKey>(Func<TEntity, TDictionaryKey> keySelector, CancellationToken cancellationToken = new CancellationToken())
129+
{
130+
if (keySelector == null)
131+
throw new ArgumentNullException(nameof(keySelector));
132+
133+
return ToDictionaryAsync(keySelector, (IQueryOptions<TEntity>)null, cancellationToken);
134+
}
135+
136+
/// <summary>
137+
/// Asynchronously returns a new <see cref="Dictionary{TDictionaryKey, TEntity}" /> according to the specified <paramref name="keySelector" />.
138+
/// </summary>
139+
/// <typeparam name="TDictionaryKey">The type of the dictionary key.</typeparam>
140+
/// <param name="keySelector">A function to extract a key from each entity.</param>
141+
/// <param name="options">The options to apply to the query.</param>
142+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
143+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a new <see cref="Dictionary{TDictionaryKey, TEntity}" /> that contains keys and values.</returns>
144+
public Task<Dictionary<TDictionaryKey, TEntity>> ToDictionaryAsync<TDictionaryKey>(Func<TEntity, TDictionaryKey> keySelector, IQueryOptions<TEntity> options, CancellationToken cancellationToken = new CancellationToken())
145+
{
146+
if (keySelector == null)
147+
throw new ArgumentNullException(nameof(keySelector));
148+
149+
return ToDictionaryAsync((ISpecification<TEntity>)null, keySelector, options, cancellationToken);
150+
}
151+
152+
/// <summary>
153+
/// Asynchronously returns a new <see cref="Dictionary{TDictionaryKey, TElemen}" /> according to the specified <paramref name="keySelector" />, a comparer, and an element selector function..
154+
/// </summary>
155+
/// <typeparam name="TDictionaryKey">The type of the dictionary key.</typeparam>
156+
/// <typeparam name="TElement">The type of the value returned by elementSelector.</typeparam>
157+
/// <param name="keySelector">A function to extract a key from each entity.</param>
158+
/// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
159+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
160+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a new <see cref="Dictionary{TDictionaryKey, TEntity}" /> that contains keys and values.</returns>
161+
public Task<Dictionary<TDictionaryKey, TElement>> ToDictionaryAsync<TDictionaryKey, TElement>(Func<TEntity, TDictionaryKey> keySelector, Func<TEntity, TElement> elementSelector, CancellationToken cancellationToken = new CancellationToken())
162+
{
163+
if (keySelector == null)
164+
throw new ArgumentNullException(nameof(keySelector));
165+
166+
return ToDictionaryAsync(keySelector, elementSelector, (IQueryOptions<TEntity>)null, cancellationToken);
167+
}
168+
169+
/// <summary>
170+
/// Asynchronously returns a new <see cref="Dictionary{TDictionaryKey, TElemen}" /> according to the specified <paramref name="keySelector" />, a comparer, and an element selector function..
171+
/// </summary>
172+
/// <typeparam name="TDictionaryKey">The type of the dictionary key.</typeparam>
173+
/// <typeparam name="TElement">The type of the value returned by elementSelector.</typeparam>
174+
/// <param name="keySelector">A function to extract a key from each entity.</param>
175+
/// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
176+
/// <param name="options">The options to apply to the query.</param>
177+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
178+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a new <see cref="Dictionary{TDictionaryKey, TEntity}" /> that contains keys and values.</returns>
179+
public Task<Dictionary<TDictionaryKey, TElement>> ToDictionaryAsync<TDictionaryKey, TElement>(Func<TEntity, TDictionaryKey> keySelector, Func<TEntity, TElement> elementSelector, IQueryOptions<TEntity> options, CancellationToken cancellationToken = new CancellationToken())
180+
{
181+
if (keySelector == null)
182+
throw new ArgumentNullException(nameof(keySelector));
183+
184+
return ToDictionaryAsync((ISpecification<TEntity>)null, keySelector, elementSelector, options, cancellationToken);
185+
}
186+
187+
/// <summary>
188+
/// Asynchronously returns a new <see cref="Dictionary{TDictionaryKey, TEntity}" /> according to the specified <paramref name="keySelector" />.
189+
/// </summary>
190+
/// <typeparam name="TDictionaryKey">The type of the dictionary key.</typeparam>
191+
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
192+
/// <param name="keySelector">A function to extract a key from each entity.</param>
193+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
194+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a new <see cref="Dictionary{TDictionaryKey, TEntity}" /> that contains keys and values.</returns>
195+
public Task<Dictionary<TDictionaryKey, TEntity>> ToDictionaryAsync<TDictionaryKey>(ISpecification<TEntity> criteria, Func<TEntity, TDictionaryKey> keySelector, CancellationToken cancellationToken = new CancellationToken())
196+
{
197+
if (keySelector == null)
198+
throw new ArgumentNullException(nameof(keySelector));
199+
200+
return ToDictionaryAsync(criteria, keySelector, (IQueryOptions<TEntity>)null, cancellationToken);
201+
}
202+
203+
/// <summary>
204+
/// Asynchronously returns a new <see cref="Dictionary{TDictionaryKey, TEntity}" /> according to the specified <paramref name="keySelector" />.
205+
/// </summary>
206+
/// <typeparam name="TDictionaryKey">The type of the dictionary key.</typeparam>
207+
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
208+
/// <param name="keySelector">A function to extract a key from each entity.</param>
209+
/// <param name="options">The options to apply to the query.</param>
210+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
211+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a new <see cref="Dictionary{TDictionaryKey, TEntity}" /> that contains keys and values.</returns>
212+
public Task<Dictionary<TDictionaryKey, TEntity>> ToDictionaryAsync<TDictionaryKey>(ISpecification<TEntity> criteria, Func<TEntity, TDictionaryKey> keySelector, IQueryOptions<TEntity> options, CancellationToken cancellationToken = new CancellationToken())
213+
{
214+
if (keySelector == null)
215+
throw new ArgumentNullException(nameof(keySelector));
216+
217+
return ToDictionaryAsync(criteria, keySelector, IdentityFunction<TEntity>.Instance, options, cancellationToken);
218+
}
219+
220+
/// <summary>
221+
/// Asynchronously returns a new <see cref="Dictionary{TDictionaryKey, TElemen}" /> according to the specified <paramref name="keySelector" />, a comparer, and an element selector function..
222+
/// </summary>
223+
/// <typeparam name="TDictionaryKey">The type of the dictionary key.</typeparam>
224+
/// <typeparam name="TElement">The type of the value returned by elementSelector.</typeparam>
225+
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
226+
/// <param name="keySelector">A function to extract a key from each entity.</param>
227+
/// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
228+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
229+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a new <see cref="Dictionary{TDictionaryKey, TEntity}" /> that contains keys and values.</returns>
230+
public Task<Dictionary<TDictionaryKey, TElement>> ToDictionaryAsync<TDictionaryKey, TElement>(ISpecification<TEntity> criteria, Func<TEntity, TDictionaryKey> keySelector, Func<TEntity, TElement> elementSelector, CancellationToken cancellationToken = new CancellationToken())
231+
{
232+
if (keySelector == null)
233+
throw new ArgumentNullException(nameof(keySelector));
234+
235+
return ToDictionaryAsync(criteria, keySelector, elementSelector, (IQueryOptions<TEntity>)null, cancellationToken);
236+
}
237+
238+
/// <summary>
239+
/// Asynchronously returns a new <see cref="Dictionary{TDictionaryKey, TElemen}" /> according to the specified <paramref name="keySelector" />, a comparer, and an element selector function..
240+
/// </summary>
241+
/// <typeparam name="TDictionaryKey">The type of the dictionary key.</typeparam>
242+
/// <typeparam name="TElement">The type of the value returned by elementSelector.</typeparam>
243+
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
244+
/// <param name="keySelector">A function to extract a key from each entity.</param>
245+
/// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
246+
/// <param name="options">The options to apply to the query.</param>
247+
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
248+
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a new <see cref="Dictionary{TDictionaryKey, TEntity}" /> that contains keys and values.</returns>
249+
public Task<Dictionary<TDictionaryKey, TElement>> ToDictionaryAsync<TDictionaryKey, TElement>(ISpecification<TEntity> criteria, Func<TEntity, TDictionaryKey> keySelector, Func<TEntity, TElement> elementSelector, IQueryOptions<TEntity> options, CancellationToken cancellationToken = new CancellationToken())
250+
{
251+
if (keySelector == null)
252+
throw new ArgumentNullException(nameof(keySelector));
253+
254+
return GetDictionaryAsync(criteria, keySelector, elementSelector, options, cancellationToken);
255+
}
256+
116257
#endregion
117258

118259
#region Implementation of ICanAddAsync<in TEntity>

0 commit comments

Comments
 (0)