Skip to content

Commit dc102dc

Browse files
committed
fix(core): modernize and refactor codebase
This commit modernizes and refactors various parts of the codebase to improve readability, maintainability, and performance. It includes updates to dependencies, code style improvements, and the use of modern C# features. - Updated `husky` tool version in `dotnet-tools.json` from `0.7.1` to `0.7.2`. - Added `using System.Diagnostics.CodeAnalysis;` to multiple files. - Renamed `BadDbTypes` to `_BadDbTypes` in `DbCommandExtensions.cs` and updated its initialization syntax. - Replaced manual null checks with `ArgumentNullException.ThrowIfNull` and `ArgumentNullException.ThrowIfNullOrEmpty`. - Changed variable declarations to use explicit types (`DbParameter`). - Added `[return: NotNullIfNotNull(nameof(defaultValue))]` attribute to methods in `DbCommandExtensions.cs` and `IDataRecordExtensions.cs`. - Improved variable naming consistency in `DbCommandExtensions.cs`. - Simplified null checks using `is not null`. - Removed `GlobalSuppressions.cs` file. - Refactored `ParameterBase<TDataType>` class to use a primary constructor and set default values directly in property initializers. - Simplified `!=` operator and updated `Equals` and `GetHashCode` methods in `ParameterBase<TDataType>` class. - Modernized `Batch` class with a primary constructor, inline property initializers, and `[GeneratedRegex]` attribute. - Updated various methods in `Batch` class to use modern C# features and more descriptive variable names. - Made `Command` and `Connection` classes partial. - Updated `Parameters` property initialization in `Command` class. - Renamed private fields to follow a consistent naming convention. - Replaced regex fields with methods marked with `[GeneratedRegex]` attribute. - Updated `Equals` method to use pattern matching. - Improved null checks and variable naming in `Command` class. - Refactored `StringParameter` class to use a primary constructor. - Updated `Microsoft.Data.SqlClient` package version from `5.2.2` to `6.0.0`. - Made several improvements to `SQLHelper.cs` including using discards, expression-bodied members, and modern C# features.
2 parents b3b4876 + 688b7e8 commit dc102dc

File tree

13 files changed

+618
-1013
lines changed

13 files changed

+618
-1013
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"isRoot": true,
44
"tools": {
55
"husky": {
6-
"version": "0.7.1",
6+
"version": "0.7.2",
77
"commands": [
88
"husky"
99
],

src/SQLHelper.DB/ExtensionMethods/DbCommandExtensions.cs

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020
using System;
2121
using System.Data;
2222
using System.Data.Common;
23+
using System.Diagnostics.CodeAnalysis;
2324
using System.Linq;
2425
using System.Threading.Tasks;
2526

@@ -33,13 +34,13 @@ public static class DbCommandExtensions
3334
/// <summary>
3435
/// The bad database types
3536
/// </summary>
36-
private static readonly DbType[] BadDbTypes = {
37+
private static readonly DbType[] _BadDbTypes = [
3738
DbType.Time,
3839
DbType.SByte,
3940
DbType.UInt16,
4041
DbType.UInt32,
4142
DbType.UInt64
42-
};
43+
];
4344

4445
/// <summary>
4546
/// Adds a parameter to the call (for strings only)
@@ -55,10 +56,8 @@ public static DbCommand AddParameter(
5556
string value = "",
5657
ParameterDirection direction = ParameterDirection.Input)
5758
{
58-
if (command is null)
59-
throw new ArgumentNullException(nameof(command));
60-
if (string.IsNullOrEmpty(id))
61-
throw new ArgumentNullException(nameof(id));
59+
ArgumentNullException.ThrowIfNull(command, nameof(command));
60+
ArgumentNullException.ThrowIfNullOrEmpty(id, nameof(id));
6261
var Length = string.IsNullOrEmpty(value) ? 1 : value.Length;
6362
if (direction == ParameterDirection.Output
6463
|| direction == ParameterDirection.InputOutput
@@ -68,8 +67,8 @@ public static DbCommand AddParameter(
6867
Length = -1;
6968
}
7069

71-
var Parameter = command.GetOrCreateParameter(id);
72-
Parameter.Value = string.IsNullOrEmpty(value) ? DBNull.Value : (object)value;
70+
DbParameter Parameter = command.GetOrCreateParameter(id);
71+
Parameter.Value = string.IsNullOrEmpty(value) ? DBNull.Value : value;
7372
Parameter.IsNullable = string.IsNullOrEmpty(value);
7473
Parameter.DbType = DbType.String;
7574
Parameter.Direction = direction;
@@ -94,10 +93,8 @@ public static DbCommand AddParameter(
9493
object? value = null,
9594
ParameterDirection direction = ParameterDirection.Input)
9695
{
97-
if (command is null)
98-
throw new ArgumentNullException(nameof(command));
99-
if (string.IsNullOrEmpty(id))
100-
throw new ArgumentNullException(nameof(id));
96+
ArgumentNullException.ThrowIfNull(command, nameof(command));
97+
ArgumentNullException.ThrowIfNullOrEmpty(id, nameof(id));
10198
return command.AddParameter(id, type.To<DbType>(), value, direction);
10299
}
103100

@@ -114,16 +111,14 @@ public static DbCommand AddParameter(
114111
public static DbCommand AddParameter<TDataType>(
115112
this DbCommand command,
116113
string id,
117-
TDataType value = default,
114+
TDataType? value = default,
118115
ParameterDirection direction = ParameterDirection.Input)
119116
{
120-
if (command is null)
121-
throw new ArgumentNullException(nameof(command));
122-
if (string.IsNullOrEmpty(id))
123-
throw new ArgumentNullException(nameof(id));
117+
ArgumentNullException.ThrowIfNull(command, nameof(command));
118+
ArgumentNullException.ThrowIfNullOrEmpty(id, nameof(id));
124119
return command.AddParameter(
125120
id,
126-
GenericEqualityComparer<TDataType>.Comparer.Equals(value, default!) ? typeof(TDataType).To<DbType>() : value?.GetType().To<DbType>() ?? DbType.Int32,
121+
GenericEqualityComparer<TDataType>.Comparer.Equals(value!, default!) ? typeof(TDataType).To<DbType>() : value?.GetType().To<DbType>() ?? DbType.Int32,
127122
value,
128123
direction);
129124
}
@@ -145,11 +140,9 @@ public static DbCommand AddParameter(
145140
object? value = null,
146141
ParameterDirection direction = ParameterDirection.Input)
147142
{
148-
if (command is null)
149-
throw new ArgumentNullException(nameof(command));
150-
if (string.IsNullOrEmpty(id))
151-
throw new ArgumentNullException(nameof(id));
152-
var Parameter = command.GetOrCreateParameter(id);
143+
ArgumentNullException.ThrowIfNull(command, nameof(command));
144+
ArgumentNullException.ThrowIfNullOrEmpty(id, nameof(id));
145+
DbParameter Parameter = command.GetOrCreateParameter(id);
153146
Parameter.IsNullable = value is null || DBNull.Value == value;
154147
if (Parameter.IsNullable)
155148
{
@@ -163,7 +156,7 @@ public static DbCommand AddParameter(
163156
{
164157
Parameter.Value = value;
165158
}
166-
if (type != default && !BadDbTypes.Contains(type))
159+
if (type != default && !_BadDbTypes.Contains(type))
167160
Parameter.DbType = type;
168161
Parameter.Direction = direction;
169162
return command;
@@ -179,7 +172,7 @@ public static DbCommand AddParameter(
179172
{
180173
if (command?.Connection is null)
181174
return null;
182-
command.Open(retries);
175+
_ = command.Open(retries);
183176
command.Transaction = command.Connection.BeginTransaction();
184177
return command.Transaction;
185178
}
@@ -202,7 +195,7 @@ public static DbCommand AddParameter(
202195
/// <returns>The DBCommand object</returns>
203196
public static DbCommand? Close(this DbCommand command)
204197
{
205-
if (!(command?.Connection is null)
198+
if (command?.Connection is not null
206199
&& command.Connection.State != ConnectionState.Closed)
207200
{
208201
command.Connection.Close();
@@ -230,11 +223,12 @@ public static DbCommand AddParameter(
230223
/// <param name="defaultValue">Default value if there is an issue</param>
231224
/// <param name="retries">The retries.</param>
232225
/// <returns>The object of the first row and first column</returns>
233-
public static TDataType ExecuteScalar<TDataType>(this DbCommand command, TDataType defaultValue = default, int retries = 0)
226+
[return: NotNullIfNotNull(nameof(defaultValue))]
227+
public static TDataType? ExecuteScalar<TDataType>(this DbCommand command, TDataType? defaultValue = default, int retries = 0)
234228
{
235229
if (command is null)
236230
return defaultValue;
237-
command.Open(retries);
231+
_ = command.Open(retries);
238232
return command.ExecuteScalar().To(defaultValue);
239233
}
240234

@@ -246,11 +240,12 @@ public static TDataType ExecuteScalar<TDataType>(this DbCommand command, TDataTy
246240
/// <param name="defaultValue">Default value if there is an issue</param>
247241
/// <param name="retries">The retries.</param>
248242
/// <returns>The object of the first row and first column</returns>
249-
public static async Task<TDataType> ExecuteScalarAsync<TDataType>(this DbCommand command, TDataType defaultValue = default, int retries = 0)
243+
[return: NotNullIfNotNull(nameof(defaultValue))]
244+
public static async Task<TDataType?> ExecuteScalarAsync<TDataType>(this DbCommand command, TDataType? defaultValue = default, int retries = 0)
250245
{
251246
if (command is null)
252247
return defaultValue;
253-
command.Open(retries);
248+
_ = command.Open(retries);
254249
var ReturnValue = await command.ExecuteScalarAsync().ConfigureAwait(false);
255250
return ReturnValue.To(defaultValue);
256251
}
@@ -263,15 +258,14 @@ public static async Task<TDataType> ExecuteScalarAsync<TDataType>(this DbCommand
263258
/// <returns>The DbParameter associated with the ID</returns>
264259
public static DbParameter GetOrCreateParameter(this DbCommand command, string id)
265260
{
266-
if (command is null)
267-
throw new ArgumentNullException(nameof(command));
261+
ArgumentNullException.ThrowIfNull(command, nameof(command));
268262
if (command.Parameters.Contains(id))
269263
{
270264
return command.Parameters[id];
271265
}
272-
var Parameter = command.CreateParameter();
266+
DbParameter Parameter = command.CreateParameter();
273267
Parameter.ParameterName = id;
274-
command.Parameters.Add(Parameter);
268+
_ = command.Parameters.Add(Parameter);
275269
return Parameter;
276270
}
277271

@@ -286,9 +280,9 @@ public static DbParameter GetOrCreateParameter(this DbCommand command, string id
286280
/// if the parameter exists (and isn't null or empty), it returns the parameter's value.
287281
/// Otherwise the default value is returned.
288282
/// </returns>
289-
public static TDataType GetOutputParameter<TDataType>(this DbCommand command, string id, TDataType defaultValue = default)
283+
public static TDataType? GetOutputParameter<TDataType>(this DbCommand command, string id, TDataType? defaultValue = default)
290284
{
291-
return !(command?.Parameters[id] is null) ?
285+
return command?.Parameters[id] is not null ?
292286
command.Parameters[id].Value.To(defaultValue) :
293287
defaultValue;
294288
}
@@ -301,26 +295,28 @@ public static TDataType GetOutputParameter<TDataType>(this DbCommand command, st
301295
/// <returns>The DBCommand object</returns>
302296
public static DbCommand? Open(this DbCommand command, int retries = 0)
303297
{
304-
Exception? holder = null;
298+
Exception? Holder = null;
305299
while (retries >= 0)
306300
{
307301
try
308302
{
309-
if (!(command?.Connection is null)
303+
if (command?.Connection is not null
310304
&& command.Connection.State != ConnectionState.Open)
311305
{
312306
command.Connection.Open();
313307
}
314308

315309
return command;
316310
}
317-
catch (Exception e)
311+
catch (Exception E)
318312
{
319-
holder = e;
313+
Holder = E;
320314
}
321315
--retries;
322316
}
323-
throw holder!;
317+
if (Holder is not null)
318+
throw Holder;
319+
return command;
324320
}
325321

326322
/// <summary>

src/SQLHelper.DB/ExtensionMethods/IDataRecordExtensions.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
using ObjectCartographer;
1919
using System;
2020
using System.Data;
21+
using System.Diagnostics.CodeAnalysis;
2122

2223
namespace SQLHelperDB.ExtensionMethods
2324
{
@@ -37,14 +38,15 @@ public static class IDataRecordExtensions
3738
/// if the parameter exists (and isn't null or empty), it returns the parameter's value.
3839
/// Otherwise the default value is returned.
3940
/// </returns>
40-
public static TDataType GetParameter<TDataType>(this IDataRecord reader, string id, TDataType defaultValue = default)
41+
[return: NotNullIfNotNull(nameof(defaultValue))]
42+
public static TDataType? GetParameter<TDataType>(this IDataRecord reader, string id, TDataType? defaultValue = default)
4143
{
4244
if (reader is null)
4345
return defaultValue;
44-
for (var x = 0; x < reader.FieldCount; ++x)
46+
for (var X = 0; X < reader.FieldCount; ++X)
4547
{
46-
if (reader.GetName(x) == id)
47-
return reader.GetParameter(x, defaultValue);
48+
if (reader.GetName(X) == id)
49+
return reader.GetParameter(X, defaultValue);
4850
}
4951
return defaultValue;
5052
}
@@ -60,7 +62,8 @@ public static TDataType GetParameter<TDataType>(this IDataRecord reader, string
6062
/// if the parameter exists (and isn't null or empty), it returns the parameter's value.
6163
/// Otherwise the default value is returned.
6264
/// </returns>
63-
public static TDataType GetParameter<TDataType>(this IDataRecord reader, int position, TDataType defaultValue = default)
65+
[return: NotNullIfNotNull(nameof(defaultValue))]
66+
public static TDataType? GetParameter<TDataType>(this IDataRecord reader, int position, TDataType? defaultValue = default)
6467
{
6568
if (reader is null)
6669
return defaultValue;

src/SQLHelper.DB/GlobalSuppressions.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)