Skip to content

Commit 9dabf16

Browse files
committed
Enhance documentation and null-checking in data classes
Added XML documentation comments to `DataReaderExtensions.cs` and `DynamicDataRecord.cs`. Improved null-checking using `ArgumentNullException.ThrowIfNull` for .NET 6.0 and greater. Updated method implementations for better readability and maintainability while preserving functionality.
1 parent 4e4c1bb commit 9dabf16

2 files changed

Lines changed: 97 additions & 97 deletions

File tree

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,79 @@
1-

2-
namespace SharedCode.Data;
3-
4-
using System;
5-
using System.Collections.Generic;
6-
using System.Data;
1+
using System.Data;
72
using System.Data.Common;
83

4+
namespace SharedCode.Data;
5+
96
/// <summary>
107
/// The data reader extensions class.
118
/// </summary>
129
public static class DataReaderExtensions
1310
{
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();
2623

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+
}
3633

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();
4946

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+
}
5956

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
7175

72-
var converter = new DataRecordConverter<T>(@this);
73-
return converter.ConvertRecordToItem();
74-
}
76+
var converter = new DataRecordConverter<T>(@this);
77+
return converter.ConvertRecordToItem();
78+
}
7579
}
Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
1-

1+
using SharedCode.Data.Exceptions;
22

3-
4-
namespace SharedCode.Data;
5-
6-
using SharedCode.Data.Exceptions;
7-
8-
using System;
93
using System.Data;
104
using System.Dynamic;
115

6+
namespace SharedCode.Data;
7+
128
/// <summary>
139
/// The dynamic data record class.
1410
/// </summary>
15-
public class DynamicDataRecord : DynamicObject
11+
/// <remarks>
12+
/// Initializes a new instance of the <see cref="DynamicDataRecord"/> class.
13+
/// </remarks>
14+
/// <param name="record">The data record to be made dynamic.</param>
15+
public class DynamicDataRecord(IDataRecord record) : DynamicObject
1616
{
17-
/// <summary>
18-
/// The record
19-
/// </summary>
20-
private readonly IDataRecord record;
21-
22-
/// <summary>
23-
/// Initializes a new instance of the <see cref="DynamicDataRecord" /> class.
24-
/// </summary>
25-
/// <param name="record">The record.</param>
26-
public DynamicDataRecord(IDataRecord record) => this.record = record;
27-
28-
/// <inheritdoc/>
29-
public override bool TryGetMember(GetMemberBinder binder, out object? result)
30-
{
31-
ArgumentNullException.ThrowIfNull(binder);
32-
33-
try
34-
{
35-
var rawResult = this.record.GetValue(this.record.GetOrdinal(binder.Name));
36-
result = rawResult == DBNull.Value ? default : rawResult;
37-
return true;
38-
}
39-
catch (IndexOutOfRangeException)
40-
{
41-
throw new ColumnIsNotInResultSetException(binder.Name);
42-
}
43-
}
17+
/// <inheritdoc/>
18+
public override bool TryGetMember(GetMemberBinder binder, out object? result)
19+
{
20+
#if NET6_0_OR_GREATER
21+
ArgumentNullException.ThrowIfNull(binder);
22+
#else
23+
if (binder is null)
24+
{
25+
throw new ArgumentNullException(nameof(binder));
26+
}
27+
#endif
28+
29+
try
30+
{
31+
var rawResult = record.GetValue(record.GetOrdinal(binder.Name));
32+
result = rawResult == DBNull.Value ? default : rawResult;
33+
return true;
34+
}
35+
catch (IndexOutOfRangeException)
36+
{
37+
throw new ColumnIsNotInResultSetException(binder.Name);
38+
}
39+
}
4440
}

0 commit comments

Comments
 (0)