Last active
August 29, 2019 18:21
-
-
Save panicoenlaxbox/53affe7db4bd308c6547fb3d9d178619 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ConsoleApplication1 | |
{ | |
public class DbCException : Exception | |
{ | |
public DbCException() | |
{ | |
} | |
public DbCException(string message) | |
: base(message) | |
{ | |
} | |
public DbCException(string message, Exception inner) | |
: base(message, inner) | |
{ | |
} | |
} | |
public static class DbC | |
{ | |
public static void Requires(bool condition, string message) | |
{ | |
if (!condition) | |
{ | |
throw new DbCException(message); | |
} | |
} | |
public static void Requires<T>(bool condition, string message) where T : Exception, new() | |
{ | |
if (!condition) | |
{ | |
throw (Exception)Activator.CreateInstance(typeof(T), message); | |
} | |
} | |
public static void Requires(Func<bool> condition, string message) | |
{ | |
if (!condition()) | |
{ | |
throw new DbCException(message); | |
} | |
} | |
public static void Requires(Func<bool> condition, Action t) | |
{ | |
if (!condition()) | |
{ | |
t(); | |
} | |
} | |
public static void Requires(Func<dynamic, bool> condition, dynamic value, string message) | |
{ | |
if (!condition(value)) | |
{ | |
throw new DbCException(message); | |
} | |
} | |
public static void IntegerGreaterThanZero(int value, string name) | |
{ | |
if (value <= 0) | |
{ | |
throw new DbCException($"{name} must be greater than 0"); | |
} | |
} | |
public static void NotEmpty(string value, string name) | |
{ | |
if (string.IsNullOrWhiteSpace(value)) | |
{ | |
throw new DbCException($"{name} cannot be empty"); | |
} | |
} | |
public static void NotEmpty<T>(IEnumerable<T> values, string name) | |
{ | |
if (values == null || !values.Any()) | |
{ | |
throw new DbCException($"{name} cannot be empty"); | |
} | |
} | |
public static void NotEmpty<T, TException>(IEnumerable<T> values, string name) where TException : Exception, new() | |
{ | |
if (values == null || !values.Any()) | |
{ | |
throw (Exception)Activator.CreateInstance(typeof(TException), $"{name} cannot be empty"); | |
} | |
} | |
public static void NotEmpty(Guid value, string name) | |
{ | |
if (value == Guid.Empty) | |
{ | |
throw new DbCException($"{name} cannot be empty"); | |
} | |
} | |
public static void NotNull(object value, string name) | |
{ | |
if (value == null) | |
{ | |
throw new DbCException($"{name} cannot be null"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment