-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataReaderExtensions.cs
131 lines (95 loc) · 3.53 KB
/
DataReaderExtensions.cs
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
using System;
using System.Data;
using Acolyte.Assertions;
using Acolyte.Common.Monads;
namespace Acolyte.Data
{
/// <summary>
/// Contains extension methods for <see cref="IDataReader" /> class.
/// </summary>
public static class DataReaderExtensions
{
#region Get Classes
public static T GetClass<T>(this IDataReader dataReader, Func<IDataReader, T> reader)
{
dataReader.ThrowIfNull(nameof(dataReader));
reader.ThrowIfNull(nameof(reader));
return reader(dataReader);
}
public static T? GetClass<T>(this IDataReader dataReader, string name)
where T : class?
{
dataReader.ThrowIfNull(nameof(dataReader));
name.ThrowIfNull(nameof(name));
object value = dataReader[name];
if (value == Convert.DBNull) return null;
return value.To<T?>();
}
public static T? GetClass<T>(this IDataReader dataReader, int index)
where T : class?
{
dataReader.ThrowIfNull(nameof(dataReader));
object value = dataReader[index];
if (value == Convert.DBNull) return null;
return value.To<T?>();
}
#endregion
#region Get Nullable Value Types
public static T? GetNullable<T>(this IDataReader dataReader, string name)
where T : struct
{
return dataReader.GetNullable<T>(name, defaultValue: null);
}
public static T? GetNullable<T>(this IDataReader dataReader, string name, T? defaultValue)
where T : struct
{
dataReader.ThrowIfNull(nameof(dataReader));
name.ThrowIfNull(nameof(name));
object value = dataReader[name];
if (value == Convert.DBNull) return defaultValue;
return value.To<T?>();
}
public static T GetNullable<T>(this IDataReader dataReader, string name, T defaultValue)
where T : struct
{
dataReader.ThrowIfNull(nameof(dataReader));
name.ThrowIfNull(nameof(name));
object value = dataReader[name];
if (value == Convert.DBNull) return defaultValue;
return value.To<T>();
}
#endregion
#region Get Value Types
public static T GetValue<T>(this IDataReader dataReader, string name)
where T : struct
{
dataReader.ThrowIfNull(nameof(dataReader));
name.ThrowIfNull(nameof(name));
object value = dataReader[name];
if (value == Convert.DBNull)
{
throw new ArgumentException($"Unexpected DBNull value. Name: '{name}'.");
}
return value.To<T>();
}
public static T GetValue<T>(this IDataReader dataReader, string name, T defaultValue)
where T : struct
{
dataReader.ThrowIfNull(nameof(dataReader));
name.ThrowIfNull(nameof(name));
object value = dataReader[name];
if (value == Convert.DBNull) return defaultValue;
return value.To<T>();
}
#endregion
#region Get Date Time
public static DateTime GetDateTime(this IDataReader dataReader, string name,
DateTimeKind kind)
{
dataReader.ThrowIfNull(nameof(dataReader));
name.ThrowIfNull(nameof(name));
return DateTime.SpecifyKind(GetValue<DateTime>(dataReader, name), kind);
}
#endregion
}
}