Ejemplo n.º 1
0
        /// <summary>
        /// Validates the specified object instance.
        /// </summary>
        /// <returns>
        /// A value that indicates whether validation was successful or not.
        /// </returns>
        /// <param name='target'>
        /// The target object instance to validate.
        /// </param>
        /// <param name='failures'>
        /// Exposes a collection of the validation failures.  If validation is a success this collection will be empty.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="target"/> is null.
        /// </exception>
        public bool Validate(TTarget target, out ValidationTestList <TTarget> failures)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            failures = new ValidationTestList <TTarget>();

            foreach (IValidationTest <TTarget> test in this.Tests)
            {
                try
                {
                    bool testPassed = test.Execute(target);

                    if (!testPassed)
                    {
                        failures.Add(test);
                    }
                }
                catch (Exception ex)
                {
                    failures = new ValidationTestList <TTarget>();
                    failures.Add(test);
                    throw new ValidationFailureException <TTarget>(failures, ex);
                }
            }

            return(failures.Count == 0);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a subset of the current collection where the failed test(s) have the specified
        /// <paramref name="identifier"/> and are for the specified <paramref name="member"/>.
        /// </summary>
        /// <returns>
        /// A collection of validation failures, possibly empty.
        /// </returns>
        /// <param name='identifier'>
        /// The identifier value to search for.
        /// </param>
        /// <param name='member'>
        /// The member that we are interested in.
        /// </param>
        public ValidationTestList <TTarget> ByIdentifier(object identifier, MemberInfo member)
        {
            ValidationTestList <TTarget> output = new ValidationTestList <TTarget>();

            if (identifier == null && member == null)
            {
                throw new InvalidOperationException("Both identifier and member cannot be null.");
            }

            var subset = (from x in this
                          where
                          (identifier == null || x.Identifier == identifier) &&
                          (member == null || x.Member == member)
                          select x);

            output.AddRange(subset);

            return(output);
        }
 /// <summary>
 /// Initializes a new instance.
 /// </summary>
 /// <param name='failures'>
 /// The collection of validation failures.
 /// </param>
 public ValidationFailureException(ValidationTestList <TTarget> failures) : this(failures, null)
 {
 }
 /// <summary>
 /// Initializes a new instance.
 /// </summary>
 /// <param name='failures'>
 /// The collection of validation failures.
 /// </param>
 /// <param name='inner'>
 /// An <see cref="System.Exception"/> that was encountered whilst performing validation.
 /// </param>
 public ValidationFailureException(ValidationTestList <TTarget> failures, Exception inner) : base(String.Format("Validation failure of a `{0}'",
                                                                                                                typeof(TTarget).FullName),
                                                                                                  inner)
 {
     this.Failures = failures;
 }