Skip to content

Commit 688b7e8

Browse files
committed
refactor(core): improve code readability and maintainability
This commit refactors several files to enhance code readability, maintainability, and consistency. It includes updates to namespaces, variable naming conventions, and the use of modern C# features. - Add `System.Diagnostics.CodeAnalysis` namespace to multiple files. - Rename variables and fields to follow consistent naming conventions. - Replace `ArgumentNullException` checks with `ArgumentNullException.ThrowIfNull`. - Use `[GeneratedRegex]` attribute for regex patterns. - Convert classes to partial and use primary constructors. - Refactor methods to use expression-bodied members and discard operator (`_ =`). - Update `Microsoft.Data.SqlClient` package version to `6.0.0`. - Remove `GlobalSuppressions.cs` file. These changes improve the overall structure and clarity of the codebase, making it easier to maintain and extend in the future.
1 parent 9c6c5f9 commit 688b7e8

File tree

12 files changed

+617
-1012
lines changed

12 files changed

+617
-1012
lines changed

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.

src/SQLHelper.DB/HelperClasses/BaseClasses/ParameterBase.cs

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@ namespace SQLHelperDB.HelperClasses.BaseClasses
2828
/// Parameter base class
2929
/// </summary>
3030
/// <typeparam name="TDataType">Data type of the parameter</typeparam>
31-
public abstract class ParameterBase<TDataType> : IParameter<TDataType>
31+
/// <remarks>Constructor</remarks>
32+
/// <param name="id">ID of the parameter</param>
33+
/// <param name="type">Database type</param>
34+
/// <param name="value">Value of the parameter</param>
35+
/// <param name="direction">Direction of the parameter</param>
36+
/// <param name="parameterStarter">
37+
/// What the database expects as the parameter starting string ("@" for SQL Server, ":" for
38+
/// Oracle, etc.)
39+
/// </param>
40+
public abstract class ParameterBase<TDataType>(string id, DbType type, object? value = null, ParameterDirection direction = ParameterDirection.Input, string parameterStarter = "@") : IParameter<TDataType>
3241
{
3342
/// <summary>
3443
/// Constructor
@@ -70,41 +79,20 @@ protected ParameterBase(string id, SqlDbType type, object? value = null, Paramet
7079
{
7180
}
7281

73-
/// <summary>
74-
/// Constructor
75-
/// </summary>
76-
/// <param name="id">ID of the parameter</param>
77-
/// <param name="type">Database type</param>
78-
/// <param name="value">Value of the parameter</param>
79-
/// <param name="direction">Direction of the parameter</param>
80-
/// <param name="parameterStarter">
81-
/// What the database expects as the parameter starting string ("@" for SQL Server, ":" for
82-
/// Oracle, etc.)
83-
/// </param>
84-
protected ParameterBase(string id, DbType type, object? value = null, ParameterDirection direction = ParameterDirection.Input, string parameterStarter = "@")
85-
{
86-
ID = id;
87-
Value = (TDataType)value!;
88-
DatabaseType = type;
89-
Direction = direction;
90-
BatchID = id;
91-
ParameterStarter = parameterStarter;
92-
}
93-
9482
/// <summary>
9583
/// Database type
9684
/// </summary>
97-
public DbType DatabaseType { get; set; }
85+
public DbType DatabaseType { get; set; } = type;
9886

9987
/// <summary>
10088
/// Direction of the parameter
10189
/// </summary>
102-
public ParameterDirection Direction { get; set; }
90+
public ParameterDirection Direction { get; set; } = direction;
10391

10492
/// <summary>
10593
/// The Name that the parameter goes by
10694
/// </summary>
107-
public string ID { get; set; }
95+
public string ID { get; set; } = id;
10896

10997
/// <summary>
11098
/// Gets the internal value.
@@ -115,28 +103,25 @@ protected ParameterBase(string id, DbType type, object? value = null, ParameterD
115103
/// <summary>
116104
/// Starting string of the parameter
117105
/// </summary>
118-
public string ParameterStarter { get; set; }
106+
public string ParameterStarter { get; set; } = parameterStarter;
119107

120108
/// <summary>
121109
/// Parameter value
122110
/// </summary>
123-
public TDataType Value { get; set; }
111+
public TDataType Value { get; set; } = (TDataType)value!;
124112

125113
/// <summary>
126114
/// Batch ID
127115
/// </summary>
128-
protected string BatchID { get; set; }
116+
protected string BatchID { get; set; } = id;
129117

130118
/// <summary>
131119
/// != operator
132120
/// </summary>
133121
/// <param name="first">First item</param>
134122
/// <param name="second">Second item</param>
135123
/// <returns>returns true if they are not equal, false otherwise</returns>
136-
public static bool operator !=(ParameterBase<TDataType> first, ParameterBase<TDataType> second)
137-
{
138-
return !(first == second);
139-
}
124+
public static bool operator !=(ParameterBase<TDataType> first, ParameterBase<TDataType> second) => !(first == second);
140125

141126
/// <summary>
142127
/// The == operator
@@ -147,8 +132,8 @@ protected ParameterBase(string id, DbType type, object? value = null, ParameterD
147132
public static bool operator ==(ParameterBase<TDataType> first, ParameterBase<TDataType> second)
148133
{
149134
return ReferenceEquals(first, second)
150-
|| (!(first is null)
151-
&& !(second is null)
135+
|| (first is not null
136+
&& second is not null
152137
&& first.GetHashCode() == second.GetHashCode());
153138
}
154139

@@ -183,7 +168,7 @@ public string AddParameter(string command)
183168
/// </summary>
184169
/// <param name="obj">Object to compare to</param>
185170
/// <returns>True if they are equal, false otherwise</returns>
186-
public override bool Equals(object obj)
171+
public override bool Equals(object? obj)
187172
{
188173
return (obj is ParameterBase<TDataType> OtherParameter)
189174
&& OtherParameter.DatabaseType == DatabaseType
@@ -201,10 +186,10 @@ public override bool Equals(object obj)
201186
/// </returns>
202187
public override int GetHashCode()
203188
{
204-
var hashCode = 2030399226;
205-
hashCode = (hashCode * -1521134295) + DatabaseType.GetHashCode();
206-
hashCode = (hashCode * -1521134295) + EqualityComparer<string>.Default.GetHashCode(ID);
207-
return (hashCode * -1521134295) + EqualityComparer<TDataType>.Default.GetHashCode(Value);
189+
var HashCode = 2030399226;
190+
HashCode = (HashCode * -1521134295) + DatabaseType.GetHashCode();
191+
HashCode = (HashCode * -1521134295) + EqualityComparer<string>.Default.GetHashCode(ID);
192+
return (HashCode * -1521134295) + EqualityComparer<TDataType>.Default.GetHashCode(Value!);
208193
}
209194

210195
/// <summary>

0 commit comments

Comments
 (0)