示例#1
0
        /// <summary>
        /// Checks that the provided condition evaluated to True.  If not, an <see cref="ObjectDisposedException"/> is thrown.
        /// </summary>
        /// <typeparam name="T">The type of the object being validated.</typeparam>
        /// <param name="validation">The current object validation to check against.</param>
        /// <param name="condition">An expression that must evaluate to true, or it will fail the validation.</param>
        /// <param name="message">An optional message to throw with the exception.</param>
        /// <returns>The current object validation to check against.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="condition"/> is null.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if <paramref name="condition"/> evaluated to false.</exception>
        public static StateValidation <T> IsNotDisposed <T>(this StateValidation <T> validation, Predicate <T> condition, string message)
        {
            if (validation == null)
            {
                throw new ArgumentNullException("validation");
            }
            if (condition == null)
            {
                throw new ArgumentNullException("condition");
            }

            if (!condition(validation.Object))
            {
                var objectName = typeof(T).FullName;

                if (message == null)
                {
                    validation.SetException(new ObjectDisposedException(objectName));
                }
                else
                {
                    validation.SetException(new ObjectDisposedException(objectName, message));
                }
            }

            return(validation);
        }
示例#2
0
        /// <summary>
        /// Checks that the provided condition evaluated to True.  If not, an <see cref="InvalidOperationException"/> is thrown.
        /// </summary>
        /// <typeparam name="T">The type of the object being validated.</typeparam>
        /// <param name="validation">The current object validation to check against.</param>
        /// <param name="condition">An expression that must evaluate to true, or it will fail the validation.</param>
        /// <param name="message">An optional message to throw with the exception.</param>
        /// <returns>The current object validation to check against.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="condition"/> is null.</exception>
        /// <exception cref="InvalidOperationException">Thrown if <paramref name="condition"/> evaluated to false.</exception>
        public static StateValidation <T> Operation <T>(this StateValidation <T> validation, Predicate <T> condition, string message)
        {
            if (validation == null)
            {
                throw new ArgumentNullException("validation");
            }
            if (condition == null)
            {
                throw new ArgumentNullException("condition");
            }

            if (!condition(validation.Object))
            {
                validation.SetException(message == null ? new InvalidOperationException() : new InvalidOperationException(message));
            }

            return(validation);
        }
        internal static StateValidation <T> Borrow(T obj)
        {
            if (_validationPool == null)
            {
                _validationPool = new Queue <StateValidation <T> >();
            }

            StateValidation <T> valObj;

            if (_validationPool.Count > 0)
            {
                valObj = _validationPool.Dequeue();
            }
            else
            {
                valObj = new StateValidation <T>();
            }

            valObj.Object = obj;

            return(valObj);
        }
示例#4
0
 /// <summary>
 /// Begins a new State validation.
 /// </summary>
 /// <typeparam name="T">The type of the object being validated.</typeparam>
 /// <param name="objectToValidate">The object to be validated.</param>
 /// <returns>A new <see cref="StateValidation{T}"/> instance.</returns>
 public static StateValidation <T> State <T>(T objectToValidate)
 {
     return(StateValidation <T> .Borrow(objectToValidate));
 }
示例#5
0
 /// <summary>
 /// Checks that the provided condition evaluated to True.  If not, an <see cref="InvalidOperationException"/> is thrown.
 /// </summary>
 /// <typeparam name="T">The type of the object being validated.</typeparam>
 /// <param name="validation">The current object validation to check against.</param>
 /// <param name="condition">An expression that must evaluate to true, or it will fail the validation.</param>
 /// <param name="format"> A composite format string.</param>
 /// <param name="args">An object array that contains zero or more objects to format.</param>
 /// <returns>The current object validation to check against.</returns>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="condition"/> is null.</exception>
 /// <exception cref="InvalidOperationException">Thrown if <paramref name="condition"/> evaluated to false.</exception>
 public static StateValidation <T> Operation <T>(this StateValidation <T> validation, Predicate <T> condition, string format, params object[] args)
 {
     return(Operation(validation, condition, Format(format, args)));
 }
示例#6
0
 /// <summary>
 /// Checks that the provided condition evaluated to True.  If not, an <see cref="InvalidOperationException"/> is thrown.
 /// </summary>
 /// <typeparam name="T">The type of the object being validated.</typeparam>
 /// <param name="validation">The current object validation to check against.</param>
 /// <param name="condition">An expression that must evaluate to true, or it will fail the validation.</param>
 /// <returns>The current object validation to check against.</returns>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="condition"/> is null.</exception>
 /// <exception cref="InvalidOperationException">Thrown if <paramref name="condition"/> evaluated to false.</exception>
 public static StateValidation <T> Operation <T>(this StateValidation <T> validation, Predicate <T> condition)
 {
     return(Operation(validation, condition, null));
 }
示例#7
0
 internal static void SetException <T>(this StateValidation <T> validation, Exception ex)
 {
     validation.SetExceptionInternal(ex);
 }
示例#8
0
 /// <summary>
 /// Checks that the provided <see cref="IDisposedObservable"/> is not disposed. If so, an <see cref="ObjectDisposedException"/> is thrown.
 /// </summary>
 /// <typeparam name="T">The type of the object being validated. Must implement <see cref="IDisposedObservable"/>.</typeparam>
 /// <param name="validation">The current object validation to check against.</param>
 /// <returns>The current object validation to check against.</returns>
 /// <exception cref="ObjectDisposedException">Thrown if the object is disposed.</exception>
 public static StateValidation <T> IsNotDisposed <T>(this StateValidation <T> validation)
     where T : IDisposedObservable
 {
     return(IsNotDisposed(validation, obj => !obj.IsDisposed, null));
 }
示例#9
0
 /// <summary>
 /// Checks that the provided <see cref="IDisposedObservable"/> is not disposed. If so, an <see cref="ObjectDisposedException"/> is thrown.
 /// </summary>
 /// <typeparam name="T">The type of the object being validated. Must implement <see cref="IDisposedObservable"/>.</typeparam>
 /// <param name="validation">The current object validation to check against.</param>
 /// <param name="message">An optional message to throw with the exception.</param>
 /// <returns>The current object validation to check against.</returns>
 /// <exception cref="ObjectDisposedException">Thrown if the object is disposed.</exception>
 public static StateValidation <T> IsNotDisposed <T>(this StateValidation <T> validation, string message)
     where T : IDisposedObservable
 {
     return(IsNotDisposed(validation, obj => !obj.IsDisposed, message));
 }
示例#10
0
 /// <summary>
 /// Checks that the provided condition evaluated to True.  If not, an <see cref="ObjectDisposedException"/> is thrown.
 /// </summary>
 /// <typeparam name="T">The type of the object being validated.</typeparam>
 /// <param name="validation">The current object validation to check against.</param>
 /// <param name="condition">An expression that must evaluate to true, or it will fail the validation.</param>
 /// <returns>The current object validation to check against.</returns>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="condition"/> is null.</exception>
 /// <exception cref="ObjectDisposedException">Thrown if <paramref name="condition"/> evaluated to false.</exception>
 public static StateValidation <T> IsNotDisposed <T>(this StateValidation <T> validation, Predicate <T> condition)
 {
     return(IsNotDisposed(validation, condition, null));
 }