laatzen/LaaProductionWeb/LaaProduction.SQL/SQLExtensions.cs
2024-07-17 13:13:41 +02:00

54 lines
1.6 KiB
C#

namespace LaaProduction.SQL
{
using System;
using System.Data.SqlClient;
using System.Runtime.CompilerServices;
public static class SQLExtensions
{
internal static T EnsureNotNull<T>(this T value, [CallerMemberName] String memberName = "")
{
value.ThrowIfNull(memberName);
return value;
}
internal static String EnsureNotNullOrWhiteSpace(this String value, [CallerMemberName] String memberName = "")
{
value.ThrowIfNullOrWhiteSpace(memberName);
return value;
}
internal static void ThrowIfNull<T>(this T value, [CallerMemberName] String memberName = "")
{
if (value == null)
{
throw new ArgumentException($"{memberName} should not be null.", memberName);
}
}
internal static void ThrowIfNullOrWhiteSpace(this String value, [CallerMemberName] String memberName = "")
{
if (String.IsNullOrWhiteSpace(value))
{
throw new ArgumentException($"{memberName} should not be null or white space.", memberName);
}
}
internal static void OpenWithErrorHandling(this SqlConnection sqlConnection, Action<String> errorCallback)
{
sqlConnection.ThrowIfNull(nameof(sqlConnection));
sqlConnection.FireInfoMessageEventOnUserErrors = true;
if (errorCallback != null)
{
sqlConnection.InfoMessage += (_, args) => errorCallback(args.Message);
}
sqlConnection.Open();
}
}
}