|
1 | | - |
2 | | -namespace SharedCode.Data; |
3 | | - |
4 | | -using System; |
5 | | -using System.Collections.Generic; |
6 | | -using System.Data; |
| 1 | +using System.Data; |
7 | 2 | using System.Data.Common; |
8 | 3 |
|
| 4 | +namespace SharedCode.Data; |
| 5 | + |
9 | 6 | /// <summary> |
10 | 7 | /// The data reader extensions class. |
11 | 8 | /// </summary> |
12 | 9 | public static class DataReaderExtensions |
13 | 10 | { |
14 | | - /// <summary> |
15 | | - /// Enumerates the specified database data reader. |
16 | | - /// </summary> |
17 | | - /// <typeparam name="T">The type of the objects being enumerated.</typeparam> |
18 | | - /// <param name="this">The database data reader.</param> |
19 | | - /// <returns>The enumerable sequence.</returns> |
20 | | - /// <exception cref="ArgumentNullException">dbDataReader</exception> |
21 | | - public static IEnumerable<T> Enumerate<T>(this IDataReader @this) where T : new() |
22 | | - { |
23 | | - return @this is null |
24 | | - ? throw new ArgumentNullException(nameof(@this)) |
25 | | - : EnumerateImpl(); |
| 11 | + /// <summary> |
| 12 | + /// Enumerates the specified database data reader. |
| 13 | + /// </summary> |
| 14 | + /// <typeparam name="T">The type of the objects being enumerated.</typeparam> |
| 15 | + /// <param name="this">The database data reader.</param> |
| 16 | + /// <returns>The enumerable sequence.</returns> |
| 17 | + /// <exception cref="ArgumentNullException">dbDataReader</exception> |
| 18 | + public static IEnumerable<T> Enumerate<T>(this IDataReader @this) where T : new() |
| 19 | + { |
| 20 | + return @this is null |
| 21 | + ? throw new ArgumentNullException(nameof(@this)) |
| 22 | + : EnumerateImpl(); |
26 | 23 |
|
27 | | - IEnumerable<T> EnumerateImpl() |
28 | | - { |
29 | | - var converter = new DataRecordConverter<T>(@this); |
30 | | - while (@this.Read()) |
31 | | - { |
32 | | - yield return converter.ConvertRecordToItem(); |
33 | | - } |
34 | | - } |
35 | | - } |
| 24 | + IEnumerable<T> EnumerateImpl() |
| 25 | + { |
| 26 | + var converter = new DataRecordConverter<T>(@this); |
| 27 | + while (@this.Read()) |
| 28 | + { |
| 29 | + yield return converter.ConvertRecordToItem(); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
36 | 33 |
|
37 | | - /// <summary> |
38 | | - /// Enumerates the specified database data reader as an asynchronous operation. |
39 | | - /// </summary> |
40 | | - /// <typeparam name="T">The type of the objects being enumerated.</typeparam> |
41 | | - /// <param name="this">The database data reader.</param> |
42 | | - /// <returns>The asynchronous enumerable sequence.</returns> |
43 | | - /// <exception cref="ArgumentNullException">dbDataReader</exception> |
44 | | - public static IAsyncEnumerable<T> EnumerateAsync<T>(this DbDataReader @this) where T : new() |
45 | | - { |
46 | | - return @this is null |
47 | | - ? throw new ArgumentNullException(nameof(@this)) |
48 | | - : EnumerateAsyncImpl(); |
| 34 | + /// <summary> |
| 35 | + /// Enumerates the specified database data reader as an asynchronous operation. |
| 36 | + /// </summary> |
| 37 | + /// <typeparam name="T">The type of the objects being enumerated.</typeparam> |
| 38 | + /// <param name="this">The database data reader.</param> |
| 39 | + /// <returns>The asynchronous enumerable sequence.</returns> |
| 40 | + /// <exception cref="ArgumentNullException">dbDataReader</exception> |
| 41 | + public static IAsyncEnumerable<T> EnumerateAsync<T>(this DbDataReader @this) where T : new() |
| 42 | + { |
| 43 | + return @this is null |
| 44 | + ? throw new ArgumentNullException(nameof(@this)) |
| 45 | + : EnumerateAsyncImpl(); |
49 | 46 |
|
50 | | - async IAsyncEnumerable<T> EnumerateAsyncImpl() |
51 | | - { |
52 | | - var converter = new DataRecordConverter<T>(@this); |
53 | | - while (await @this.ReadAsync().ConfigureAwait(true)) |
54 | | - { |
55 | | - yield return converter.ConvertRecordToItem(); |
56 | | - } |
57 | | - } |
58 | | - } |
| 47 | + async IAsyncEnumerable<T> EnumerateAsyncImpl() |
| 48 | + { |
| 49 | + var converter = new DataRecordConverter<T>(@this); |
| 50 | + while (await @this.ReadAsync().ConfigureAwait(true)) |
| 51 | + { |
| 52 | + yield return converter.ConvertRecordToItem(); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
59 | 56 |
|
60 | | - /// <summary> |
61 | | - /// Maps the specified database data record to the specified object type <typeparamref |
62 | | - /// name="T" />. |
63 | | - /// </summary> |
64 | | - /// <typeparam name="T">The type of the object being mapped.</typeparam> |
65 | | - /// <param name="this">The database data record.</param> |
66 | | - /// <returns>The resulting object.</returns> |
67 | | - /// <exception cref="ArgumentNullException">dbDataRecord</exception> |
68 | | - public static T Map<T>(this IDataRecord @this) where T : new() |
69 | | - { |
70 | | - ArgumentNullException.ThrowIfNull(@this); |
| 57 | + /// <summary> |
| 58 | + /// Maps the specified database data record to the specified object type <typeparamref |
| 59 | + /// name="T" />. |
| 60 | + /// </summary> |
| 61 | + /// <typeparam name="T">The type of the object being mapped.</typeparam> |
| 62 | + /// <param name="this">The database data record.</param> |
| 63 | + /// <returns>The resulting object.</returns> |
| 64 | + /// <exception cref="ArgumentNullException">dbDataRecord</exception> |
| 65 | + public static T Map<T>(this IDataRecord @this) where T : new() |
| 66 | + { |
| 67 | +#if NET6_0_OR_GREATER |
| 68 | + ArgumentNullException.ThrowIfNull(@this); |
| 69 | +#else |
| 70 | + if (@this is null) |
| 71 | + { |
| 72 | + throw new ArgumentNullException(nameof(@this)); |
| 73 | + } |
| 74 | +#endif |
71 | 75 |
|
72 | | - var converter = new DataRecordConverter<T>(@this); |
73 | | - return converter.ConvertRecordToItem(); |
74 | | - } |
| 76 | + var converter = new DataRecordConverter<T>(@this); |
| 77 | + return converter.ConvertRecordToItem(); |
| 78 | + } |
75 | 79 | } |
0 commit comments