using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Umbraco.Core { /// /// Helper class for mandating values, for example on method parameters. /// internal static class Mandate { /// /// Mandates that the specified parameter is not null. /// /// The value. /// Name of the param. /// If is null. public static void ParameterNotNull(T value, string paramName) where T : class { That(value != null, () => new ArgumentNullException(paramName)); } /// /// Mandates that the specified parameter is not null. /// /// The value. /// Name of the param. /// If is null or whitespace. public static void ParameterNotNullOrEmpty(string value, string paramName) { That(!string.IsNullOrWhiteSpace(value), () => new ArgumentNullException(paramName)); } /// /// Mandates that the specified sequence is not null and has at least one element. /// /// /// The sequence. /// Name of the param. public static void ParameterNotNullOrEmpty(IEnumerable sequence, string paramName) { ParameterNotNull(sequence, paramName); ParameterCondition(sequence.Any(), paramName); } /// /// Mandates that the specified parameter matches the condition. /// /// The condition to check. /// Name of the param. /// If the condition is false. public static void ParameterCondition(bool condition, string paramName) { ParameterCondition(condition, paramName, (string)null); } /// /// Mandates that the specified parameter matches the condition. /// /// The condition to check. /// Name of the param. /// The message. /// If the condition is false. public static void ParameterCondition(bool condition, string paramName, string message) { // Warning: don't make this method have an optional message parameter (removing the other ParameterCondition overload) as it will // make binaries compiled against previous Framework libs incompatible unneccesarily message = message ?? "A parameter passed into a method was not a valid value"; That(condition, () => new ArgumentException(message, paramName)); } /// /// Mandates that the specified condition is true, otherwise throws an exception specified in . /// /// The type of the exception. /// if set to true, throws exception . /// An exception of type is raised if the condition is false. public static void That(bool condition) where TException : Exception, new() { if (!condition) throw ActivatorHelper.CreateInstance(); } /// /// Mandates that the specified condition is true, otherwise throws an exception specified in . /// /// The type of the exception. /// if set to true, throws exception . /// Deffered expression to call if the exception should be raised. /// An exception of type is raised if the condition is false. public static void That(bool condition, Func defer) where TException : Exception, new() { if (!condition) { throw defer.Invoke(); } // Here is an example of how this method is actually called //object myParam = null; //Mandate.That(myParam != null, // textManager => new ArgumentNullException(textManager.Get("blah", new {User = "blah"}))); } } }