Beispiel #1
0
        public virtual IEnumerable <DbValidationError> Validate(
            EntityValidationContext entityValidationContext,
            InternalMemberEntry property)
        {
            ValidationContext validationContext = entityValidationContext.ExternalValidationContext;

            validationContext.SetDisplayName(property, this._displayAttribute);
            object           obj = property == null ? entityValidationContext.InternalEntity.Entity : property.CurrentValue;
            ValidationResult validationResult;

            try
            {
                validationResult = this._validationAttribute.GetValidationResult(obj, validationContext);
            }
            catch (Exception ex)
            {
                throw new DbUnexpectedValidationException(Strings.DbUnexpectedValidationException_ValidationAttribute((object)validationContext.DisplayName, (object)this._validationAttribute.GetType()), ex);
            }
            if (validationResult == ValidationResult.Success)
            {
                return(Enumerable.Empty <DbValidationError>());
            }
            return(DbHelpers.SplitValidationResults(validationContext.MemberName, (IEnumerable <ValidationResult>) new ValidationResult[1]
            {
                validationResult
            }));
        }
Beispiel #2
0
        // <summary>
        // Validates a property or an entity.
        // </summary>
        // <param name="entityValidationContext"> Validation context. Never null. </param>
        // <param name="property"> Property to validate. Null for entity validation. Not null for property validation. </param>
        // <returns>
        // Validation errors as <see cref="IEnumerable{DbValidationError}" /> . Empty if no errors, never null.
        // </returns>
        public virtual IEnumerable <DbValidationError> Validate(
            EntityValidationContext entityValidationContext, InternalMemberEntry property)
        {
            DebugCheck.NotNull(entityValidationContext);

            var validationContext = entityValidationContext.ExternalValidationContext;

            validationContext.SetDisplayName(property, _displayAttribute);

            var objectToValidate = property == null
                                       ? entityValidationContext.InternalEntity.Entity
                                       : property.CurrentValue;

            ValidationResult validationResult = null;

            try
            {
                validationResult = _validationAttribute.GetValidationResult(objectToValidate, validationContext);
            }
            catch (Exception ex)
            {
                throw new DbUnexpectedValidationException(
                          Strings.DbUnexpectedValidationException_ValidationAttribute(
                              validationContext.DisplayName, _validationAttribute.GetType()),
                          ex);
            }

            return(validationResult != ValidationResult.Success
                       ? DbHelpers.SplitValidationResults(validationContext.MemberName, new[] { validationResult })
                       : Enumerable.Empty <DbValidationError>());
        }
Beispiel #3
0
        // <summary>
        // Validates an entity or a complex type implementing IValidatableObject interface.
        // This method is virtual to allow mocking.
        // </summary>
        // <param name="entityValidationContext"> Validation context. Never null. </param>
        // <param name="property"> Property to validate. Null if this is the entity that will be validated. Never null if this is the complex type that will be validated. </param>
        // <returns>
        // Validation error as <see cref="IEnumerable{DbValidationError}" /> . Empty if no errors. Never null.
        // </returns>
        // <remarks>
        // Note that <paramref name="property" /> is used to figure out what needs to be validated. If it not null the complex
        // type will be validated otherwise the entity will be validated.
        // Also if this is an IValidatableObject complex type but the instance (.CurrentValue) is null we won't validate
        // anything and will not return any errors. The reason for this is that Validation is supposed to validate using
        // information the user provided and not some additional implicit rules. (ObjectContext will throw for operations
        // that involve null complex properties).
        // </remarks>
        public virtual IEnumerable <DbValidationError> Validate(
            EntityValidationContext entityValidationContext, InternalMemberEntry property)
        {
            DebugCheck.NotNull(entityValidationContext);

            Debug.Assert(
                (property == null && entityValidationContext.InternalEntity.Entity is IValidatableObject) ||
                (property != null && (property.CurrentValue == null || property.CurrentValue is IValidatableObject)),
                "Neither entity nor complex type implements IValidatableObject.");

            if (property != null &&
                property.CurrentValue == null)
            {
                return(Enumerable.Empty <DbValidationError>());
            }

            var validationContext = entityValidationContext.ExternalValidationContext;

            validationContext.SetDisplayName(property, _displayAttribute);

            var validatableObject = (IValidatableObject)(property == null
                                                             ? entityValidationContext.InternalEntity.Entity
                                                             : property.CurrentValue);

            IEnumerable <ValidationResult> validationResults = null;

            try
            {
                validationResults = validatableObject.Validate(validationContext);
            }
            catch (Exception ex)
            {
                throw new DbUnexpectedValidationException(
                          Strings.DbUnexpectedValidationException_IValidatableObject(
                              validationContext.DisplayName, ObjectContextTypeCache.GetObjectType(validatableObject.GetType())),
                          ex);
            }

            return(DbHelpers.SplitValidationResults(
                       validationContext.MemberName,
                       validationResults ?? Enumerable.Empty <ValidationResult>()));
        }
Beispiel #4
0
        public virtual IEnumerable <DbValidationError> Validate(
            EntityValidationContext entityValidationContext,
            InternalMemberEntry property)
        {
            if (property != null && property.CurrentValue == null)
            {
                return(Enumerable.Empty <DbValidationError>());
            }
            ValidationContext validationContext = entityValidationContext.ExternalValidationContext;

            validationContext.SetDisplayName(property, this._displayAttribute);
            IValidatableObject             validatableObject = property == null ? (IValidatableObject)entityValidationContext.InternalEntity.Entity : (IValidatableObject)property.CurrentValue;
            IEnumerable <ValidationResult> validationResults;

            try
            {
                validationResults = validatableObject.Validate(validationContext);
            }
            catch (Exception ex)
            {
                throw new DbUnexpectedValidationException(Strings.DbUnexpectedValidationException_IValidatableObject((object)validationContext.DisplayName, (object)ObjectContextTypeCache.GetObjectType(validatableObject.GetType())), ex);
            }
            return(DbHelpers.SplitValidationResults(validationContext.MemberName, validationResults ?? Enumerable.Empty <ValidationResult>()));
        }