laatzen/LaaProductionWeb/FluentSQL/FluentSQLExtensions.cs
2023-06-07 09:19:08 +02:00

55 lines
1.7 KiB
C#

namespace LaaProductionWeb.Data
{
using System;
using System.Data.SqlClient;
using System.Runtime.CompilerServices;
internal static class FluentSQLExtensions
{
public static T EnsureNotNull<T>(this T value, [CallerMemberName] string memberName = "")
{
value.ThrowIfNull(memberName);
return value;
}
public static string EnsureNotNullOrWhiteSpace(this string value, [CallerMemberName] string memberName = "")
{
value.ThrowIfNullOrWhiteSpace(memberName);
return value;
}
public static void ThrowIfNull<T>(this T value, [CallerMemberName] string memberName = "")
{
if (value == null)
{
throw new ArgumentException($"{memberName} should not be null.", memberName);
}
}
public 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)
{
sqlConnection.ThrowIfNull(nameof(sqlConnection));
sqlConnection.FireInfoMessageEventOnUserErrors = true;
sqlConnection.InfoMessage += SqlConnectionInfoMessage;
sqlConnection.Open();
}
internal static void SqlConnectionInfoMessage(object sender, SqlInfoMessageEventArgs e)
{
// TODO: Log error message to the caller or elsewhere.
}
}
}