Created
August 2, 2018 16:12
-
-
Save kgiszewski/6535f033e754ed968b99c59e418e846e to your computer and use it in GitHub Desktop.
Mandate
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
public static class Mandate | |
{ | |
public static void That<TException>(bool condition, string message) where TException : Exception | |
{ | |
if (!condition) | |
{ | |
var exceptionInstance = (TException)Activator.CreateInstance(typeof(TException), message); | |
throw exceptionInstance; | |
} | |
} | |
} |
Cons: it will build and allocate the exception message string on each test, even when not throwing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Most useful for business rule enforcement and validation.
For example, we can't send an invoice if the invoice is null, therefore
Mandate
that it is not null before sending out.