/// <summary>
        /// Confirms that the string parameter value is not null or empty, otherwise throws an exception.
        /// </summary>
        /// <param name="parameter">The string parameter.</param>
        /// <param name="exceptionMessage">The exception message.</param>
        /// <returns>
        /// The input string parameter.
        /// </returns>
        /// <exception cref="System.ArgumentException">Thrown if the string parameter is empty.</exception>
        /// <exception cref="System.ArgumentNullException">Thrown if the string parameter is null.</exception>
        public static Parameter <string> IsNotNullOrEmpty(this Parameter <string> parameter, string exceptionMessage)
        {
            parameter = parameter.IsNotNull(exceptionMessage);

            if (!object.Equals(parameter.Value, string.Empty))
            {
                return(parameter);
            }

            if (string.IsNullOrWhiteSpace(exceptionMessage))
            {
                exceptionMessage = "String is empty.";
            }

            throw new ArgumentException(exceptionMessage, parameter.ParameterName);
        }
Beispiel #2
0
        /// <summary>
        /// Confirms that the string parameter value is not null, empty or white space, otherwise throws an exception.
        /// </summary>
        /// <param name="parameter">The string parameter.</param>
        /// <param name="exceptionMessage">The exception message.</param>
        /// <returns>
        /// The input string parameter.
        /// </returns>
        /// <exception cref="System.ArgumentException">Thrown if the string parameter is empty or white space.</exception>
        /// <exception cref="System.ArgumentNullException">Thrown if the string parameter is null.</exception>
        public static Parameter <string> IsNotNullOrWhiteSpace(this Parameter <string> parameter, string exceptionMessage)
        {
            parameter = parameter.IsNotNull(exceptionMessage);

            if (!string.IsNullOrWhiteSpace(parameter.Value))
            {
                return(parameter);
            }

            if (string.IsNullOrWhiteSpace(exceptionMessage))
            {
                exceptionMessage = string.Format("String is empty or whitespace. Value:<{0}>.", parameter.Value);
            }

            throw new ArgumentException(exceptionMessage, parameter.ParameterName);
        }
Beispiel #3
0
        /// <summary>
        /// Confirms that the collection parameter value is not null or empty, otherwise throws an exception.
        /// </summary>
        /// <typeparam name="T">The type of the items in the collection parameter.</typeparam>
        /// <param name="parameter">The collection parameter.</param>
        /// <param name="exceptionMessage">The exception message.</param>
        /// <returns>
        /// The input collection parameter.
        /// </returns>
        /// <exception cref="System.ArgumentException">Thrown if the collection parameter is empty.</exception>
        /// <exception cref="System.ArgumentNullException">Thrown if the collection parameter is null.</exception>
        public static Parameter <IEnumerable <T> > IsNotNullOrEmpty <T>(this Parameter <IEnumerable <T> > parameter, string exceptionMessage)
        {
            parameter = parameter.IsNotNull(exceptionMessage);

            if (parameter.Value.Count() > 0)
            {
                return(parameter);
            }

            if (string.IsNullOrWhiteSpace(exceptionMessage))
            {
                exceptionMessage = "Collection is empty.";
            }

            throw new ArgumentException(exceptionMessage, parameter.ParameterName);
        }
        /// <summary>
        /// Confirms that the parameter value is not an instance of the specified type, otherwise throws an exception.
        /// </summary>
        /// <typeparam name="T">The type of the parameter.</typeparam>
        /// <param name="parameter">The parameter.</param>
        /// <param name="type">The type.</param>
        /// <param name="exceptionMessage">The exception message.</param>
        /// <returns>
        /// The input parameter.
        /// </returns>
        /// <exception cref="System.ArgumentException">Thrown if the parameter is an instance of the specified type.</exception>
        public static Parameter <T> IsNotInstanceOfType <T>(this Parameter <T> parameter, Type type, string exceptionMessage)
        {
            parameter = parameter.IsNotNull(exceptionMessage);

            if (!type.IsInstanceOfType(parameter.Value))
            {
                return(parameter);
            }

            if (string.IsNullOrWhiteSpace(exceptionMessage))
            {
                exceptionMessage = string.Format(
                    "Value is of an unexpected type. Type:<{0}>.",
                    type.FullName);
            }

            throw new ArgumentException(exceptionMessage, parameter.ParameterName);
        }
        /// <summary>
        /// Confirms that the collection parameter value does not contain the unexpected value, otherwise throws an exception.
        /// </summary>
        /// <typeparam name="T">The type of the items in the parameter collection.</typeparam>
        /// <param name="parameter">The collection parameter.</param>
        /// <param name="unexpectedValue">The unexpected value.</param>
        /// <param name="exceptionMessage">The exception message.</param>
        /// <returns>
        /// The input collection parameter.
        /// </returns>
        /// <exception cref="System.ArgumentException">Thrown if the collection parameter value contains the unexpected value.</exception>
        public static Parameter <IEnumerable <T> > DoesNotContain <T>(this Parameter <IEnumerable <T> > parameter, T unexpectedValue, string exceptionMessage)
        {
            parameter = parameter.IsNotNull(exceptionMessage);

            if (!parameter.Value.Contains(unexpectedValue))
            {
                return(parameter);
            }

            if (string.IsNullOrWhiteSpace(exceptionMessage))
            {
                exceptionMessage = string.Format(
                    "Collection contains an unexpected value. Value:<{0}>.",
                    unexpectedValue);
            }

            throw new ArgumentException(exceptionMessage, parameter.ParameterName);
        }
        /// <summary>
        /// Confirms that the collection parameter value contains the expected value, otherwise throws an exception.
        /// </summary>
        /// <typeparam name="T">The type of the items in the parameter collection.</typeparam>
        /// <param name="parameter">The collection parameter.</param>
        /// <param name="expectedValue">The expected value.</param>
        /// <param name="exceptionMessage">The exception message.</param>
        /// <returns>
        /// The input collection parameter.
        /// </returns>
        /// <exception cref="System.ArgumentException">Thrown if the collection parameter value does not contain the expected value.</exception>
        public static Parameter <IEnumerable <T> > Contains <T>(this Parameter <IEnumerable <T> > parameter, T expectedValue, string exceptionMessage)
        {
            parameter = parameter.IsNotNull(exceptionMessage);

            if (parameter.Value.Contains(expectedValue))
            {
                return(parameter);
            }

            if (string.IsNullOrWhiteSpace(exceptionMessage))
            {
                exceptionMessage = string.Format(
                    "Collection does not contain the expected value. Expected:<{0}>.",
                    expectedValue);
            }

            throw new ArgumentException(exceptionMessage, parameter.ParameterName);
        }
        /// <summary>
        /// Confirms that the string parameter value starts with the expected value, otherwise throws an exception.
        /// </summary>
        /// <param name="parameter">The string parameter.</param>
        /// <param name="expectedValue">The expected value.</param>
        /// <param name="exceptionMessage">The exception message.</param>
        /// <returns>
        /// The input string parameter.
        /// </returns>
        /// <exception cref="System.ArgumentException">Thrown if the string parameter value does not start with the expected value.</exception>
        public static Parameter <string> StartsWith(this Parameter <string> parameter, string expectedValue, string exceptionMessage)
        {
            parameter = parameter.IsNotNull(exceptionMessage);

            if (parameter.Value.StartsWith(expectedValue))
            {
                return(parameter);
            }

            if (string.IsNullOrWhiteSpace(exceptionMessage))
            {
                exceptionMessage = string.Format(
                    "String does not start with the expected value. Expected:<{0}> Actual:<{1}>.",
                    expectedValue,
                    parameter.Value);
            }

            throw new ArgumentException(exceptionMessage, parameter.ParameterName);
        }
Beispiel #8
0
        /// <summary>
        /// Confirms that the string parameter value matches the specified regular expression pattern, otherwise throws an exception.
        /// </summary>
        /// <param name="parameter">The parameter.</param>
        /// <param name="pattern">The pattern.</param>
        /// <param name="regexOptions">The regex options.</param>
        /// <param name="exceptionMessage">The exception message.</param>
        /// <returns>
        /// The input parameter.
        /// </returns>
        /// <exception cref="System.ArgumentException">Thrown if the string parameter value does not match the regular expression pattern.</exception>
        public static Parameter <string> IsMatch(this Parameter <string> parameter, string pattern, RegexOptions regexOptions, string exceptionMessage)
        {
            parameter = parameter.IsNotNull(exceptionMessage);

            if (Regex.IsMatch(parameter.Value, pattern, regexOptions))
            {
                return(parameter);
            }

            if (string.IsNullOrWhiteSpace(exceptionMessage))
            {
                exceptionMessage = string.Format(
                    "String does not match the expected pattern. Pattern:<{0}> Actual:<{1}>.",
                    pattern,
                    parameter.Value);
            }

            throw new ArgumentException(exceptionMessage, parameter.ParameterName);
        }