generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMultiSetCollArgsT.cs
More file actions
53 lines (47 loc) · 2.36 KB
/
MultiSetCollArgsT.cs
File metadata and controls
53 lines (47 loc) · 2.36 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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using System;
using System.Collections.Generic;
namespace CoreEx.Database
{
/// <summary>
/// Provides the <b>Database</b> multi-set arguments when expecting a collection of items/records.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="mapper">The <see cref="IDatabaseMapper{TItem}"/> for the <see cref="DatabaseRecord"/>.</param>
/// <param name="result">The action that will be invoked with the result of the set.</param>
/// <param name="minRows">The minimum number of rows allowed.</param>
/// <param name="maxRows">The maximum number of rows allowed.</param>
/// <param name="stopOnNull">Indicates whether to stop further query result set processing where the current set has resulted in a null (i.e. no records).</param>
public class MultiSetCollArgs<TColl, TItem>(IDatabaseMapper<TItem> mapper, Action<TColl> result, int minRows = 0, int? maxRows = null, bool stopOnNull = false) : MultiSetCollArgs(minRows, maxRows, stopOnNull), IMultiSetArgs<TItem>
where TItem : class, new()
where TColl : class, ICollection<TItem>, new()
{
private TColl? _coll;
private readonly Action<TColl> _result = result.ThrowIfNull(nameof(result));
/// <summary>
/// Gets the <see cref="IDatabaseMapper{TItem}"/> for the <see cref="DatabaseRecord"/>.
/// </summary>
public IDatabaseMapper<TItem> Mapper { get; private set; } = mapper.ThrowIfNull(nameof(mapper));
/// <summary>
/// The <see cref="DatabaseRecord"/> method invoked for each record for its respective dataset.
/// </summary>
/// <param name="dr">The <see cref="DatabaseRecord"/>.</param>
public override void DatasetRecord(DatabaseRecord dr)
{
dr.ThrowIfNull(nameof(dr));
_coll ??= new TColl();
var item = Mapper.MapFromDb(dr);
if (item != null)
_coll.Add(item);
}
/// <summary>
/// Invokes the corresponding result function.
/// </summary>
public override void InvokeResult()
{
if (_coll != null)
_result(_coll);
}
}
}