Skip to content

Commit e102624

Browse files
Enhance Param validation and refactor AddParam methods
Updated the `Param` struct to include validation for the `name` parameter, ensuring it is not null or whitespace. The `Value` property is now conditionally set based on the `direction` parameter. Refactored multiple overloads of the `AddParam` method to utilize a new `AddParam(Param param)` method, reducing code duplication and improving maintainability. The `AddReturnParam` methods were also updated to leverage this new method for consistency.
1 parent c9239f9 commit e102624

File tree

2 files changed

+31
-75
lines changed

2 files changed

+31
-75
lines changed

Source/Core/ExpressiveCommandBase.Param.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,29 @@ public abstract partial class ExpressiveCommandBase<TConnection, TCommand, TRead
1111
/// A struct that represents the param to be created when the command is executed.
1212
/// TDbType facilitates the difference between DbType and SqlDbType.
1313
/// </summary>
14-
public readonly record struct Param : IEquatable<Param>
14+
public readonly record struct Param
1515

1616
{
1717
/// <summary>
1818
/// Constructs a <see cref="Param"/>.
1919
/// </summary>
20-
public Param(string name, object? value, TDbType? type = null, ParameterDirection direction = ParameterDirection.Input)
20+
public Param(
21+
string name,
22+
object? value,
23+
TDbType? type = null,
24+
ParameterDirection direction = ParameterDirection.Input)
2125
{
26+
if (name is null) throw new ArgumentNullException(nameof(name));
27+
if (string.IsNullOrWhiteSpace(name))
28+
throw new ArgumentException("Parameter names cannot be empty or white space.", nameof(name));
29+
Contract.EndContractBlock();
30+
2231
Name = name;
23-
Value = value;
2432
Type = type;
2533
Direction = direction;
34+
35+
bool isInput = direction is ParameterDirection.Input or ParameterDirection.InputOutput;
36+
Value = isInput ? (value ?? DBNull.Value) : value;
2637
}
2738

2839
/// <summary>

Source/Core/ExpressiveCommandBase.cs

Lines changed: 17 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ public TThis UseCancellationToken(CancellationToken token)
180180

181181
#region AddParam
182182

183+
/// <summary>
184+
/// Shortcut to add a parameter to the params list.
185+
/// </summary>
186+
protected TThis AddParam(Param param)
187+
{
188+
Params.Add(param);
189+
return (TThis)this;
190+
}
191+
183192
/// <summary>
184193
/// Adds a parameter to the params list.
185194
/// </summary>
@@ -191,103 +200,39 @@ public TThis UseCancellationToken(CancellationToken token)
191200
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
192201
/// <exception cref="ArgumentException"><paramref name="name"/> is blank.</exception>
193202
public TThis AddParam(string name, object value, TDbType type, ParameterDirection direction = ParameterDirection.Input)
194-
{
195-
if (name is null) throw new ArgumentNullException(nameof(name));
196-
if (string.IsNullOrWhiteSpace(name))
197-
throw new ArgumentException("Parameter names cannot be empty or white space.", nameof(name));
198-
Contract.EndContractBlock();
199-
200-
Params.Add(new(name, value, type, direction));
201-
return (TThis)this;
202-
}
203+
=> AddParam(new(name, value, type, direction));
203204

204205
/// <inheritdoc cref="AddParam(string, object, TDbType, ParameterDirection)"/>
205206
public TThis AddParam(string name, object? value, ParameterDirection direction = ParameterDirection.Input)
206-
{
207-
if (name is null) throw new ArgumentNullException(nameof(name));
208-
if (string.IsNullOrWhiteSpace(name))
209-
throw new ArgumentException("Parameter names cannot be empty or white space.", nameof(name));
210-
Contract.EndContractBlock();
211-
212-
Params.Add(new(name, value ?? DBNull.Value, null, direction));
213-
return (TThis)this;
214-
}
207+
=> AddParam(new(name, value, null, direction));
215208

216209
/// <inheritdoc cref="AddParam(string, object, TDbType, ParameterDirection)"/>
217210
public TThis AddParam<T>(string name, T? value, TDbType? type = null, ParameterDirection direction = ParameterDirection.Input)
218211
where T : struct
219-
{
220-
if (name is null) throw new ArgumentNullException(nameof(name));
221-
if (string.IsNullOrWhiteSpace(name))
222-
throw new ArgumentException("Parameter names cannot be empty or white space.", nameof(name));
223-
Contract.EndContractBlock();
224-
225-
Params.Add(new(name, value ?? (object)DBNull.Value, type, direction));
226-
return (TThis)this;
227-
}
212+
=> AddParam(new(name, value, type, direction));
228213

229214
/// <inheritdoc cref="AddParam(string, object, TDbType, ParameterDirection)"/>
230215
public TThis AddParam(string name, string? value, TDbType? type = null, ParameterDirection direction = ParameterDirection.Input)
231-
{
232-
if (name is null) throw new ArgumentNullException(nameof(name));
233-
if (string.IsNullOrWhiteSpace(name))
234-
throw new ArgumentException("Parameter names cannot be empty or white space.", nameof(name));
235-
Contract.EndContractBlock();
236-
237-
Params.Add(new(name, value ?? (object)DBNull.Value, type, direction));
238-
return (TThis)this;
239-
}
216+
=> AddParam(new(name, value, type, direction));
240217

241218
/// <inheritdoc cref="AddParam(string, object, TDbType, ParameterDirection)"/>
242219
public TThis AddParam(string name, TDbType type, ParameterDirection direction = ParameterDirection.Input)
243-
{
244-
if (name is null) throw new ArgumentNullException(nameof(name));
245-
if (string.IsNullOrWhiteSpace(name))
246-
throw new ArgumentException("Parameter names cannot be empty or white space.", nameof(name));
247-
Contract.EndContractBlock();
248-
249-
Params.Add(new (name, null, type, direction));
250-
return (TThis)this;
251-
}
220+
=> AddParam(new(name, null, type, direction));
252221

253222
/// <inheritdoc cref="AddParam(string, object, TDbType, ParameterDirection)"/>
254223
public TThis AddParam(string name, ParameterDirection direction = ParameterDirection.Input)
255-
{
256-
if (name is null) throw new ArgumentNullException(nameof(name));
257-
if (string.IsNullOrWhiteSpace(name))
258-
throw new ArgumentException("Parameter names cannot be empty or white space.", nameof(name));
259-
Contract.EndContractBlock();
260-
261-
Params.Add(new(name, null, null, direction));
262-
return (TThis)this;
263-
}
224+
=> AddParam(new(name, null, null, direction));
264225

265226
/// <summary>
266227
/// Adds a return parameter to the params list.
267228
/// </summary>
268229
/// <inheritdoc cref="AddParam(string, object, TDbType, ParameterDirection)"/>
269230
public TThis AddReturnParam(string name, TDbType type)
270-
{
271-
if (name is null) throw new ArgumentNullException(nameof(name));
272-
if (string.IsNullOrWhiteSpace(name))
273-
throw new ArgumentException("Parameter names cannot be empty or white space.", nameof(name));
274-
Contract.EndContractBlock();
275-
276-
Params.Add(new(name, null, type, ParameterDirection.ReturnValue));
277-
return (TThis)this;
278-
}
231+
=> AddParam(new(name, null, type, ParameterDirection.ReturnValue));
279232

280233
/// <inheritdoc cref="AddReturnParam(string, TDbType)"/>
281234
public TThis AddReturnParam(string name)
282-
{
283-
if (name is null) throw new ArgumentNullException(nameof(name));
284-
if (string.IsNullOrWhiteSpace(name))
285-
throw new ArgumentException("Parameter names cannot be empty or white space.", nameof(name));
286-
Contract.EndContractBlock();
287-
288-
Params.Add(new(name, null, null, ParameterDirection.ReturnValue));
289-
return (TThis)this;
290-
}
235+
=> AddParam(new(name, null, null, ParameterDirection.ReturnValue));
291236

292237
/// <summary>
293238
/// Conditionally adds a parameter to the params list.

0 commit comments

Comments
 (0)