Example #1
0
        /// <summary>
        /// Validates specified field.
        /// </summary>
        /// <remarks>Checks whether the value contained in the given field obeys basic constraints
        /// (nullability, min/max value etc). Returns <see cref="FieldErrorCode"/> that indicates
        /// the type of error encountered.</remarks>
        /// <param name="field">Field mapped in this entity. Throws exception if an invalid field is given.</param>
        /// <returns>Error code that indicates the type of encountered error.</returns>
        public virtual FieldErrorCode ValidateField(IDbColumn field)
        {
            // Note: this generic implementation has a lot of overhead.
            // It is recommended to implement optimized ValidateField method in inherited classes.

            if (_Table.Columns.GetByColumnName(field.ColumnName) == null)
                throw new ArgumentException(Messages.EntityModelBase_SpecFieldDoesntExistInEntity, "field");

            object fieldValue = GetField(field);
            FieldError err = new FieldError(field, fieldValue);
            return err.ErrorCode;
        }
Example #2
0
        /// <summary>
        /// Validates all fields of an entity. Returns all encountered errors.
        /// </summary>
        /// <returns>Array of FieldError objects that describe all encountered errors. Empty array if there are no errors.</returns>
        /// <example>
        /// The following example creates a new empty EmployeesEntity object and validates it.
        /// <code>
        /// public string ValidateEmployee()
        /// {
        /// 	// Create a new empty EmployeesEntity and validate it.
        /// 	// Employees table constraints specify that fields LastName and FirstName are not nullable.
        /// 	// Validation will return an array that contains two errors (LastName and FirstName may not be null).
        /// 	EmployeesEntity emp = new EmployeesEntity();
        /// 	FieldError[] errors = emp.ValidateAllFields();
        ///
        /// 	// Iterate through all errors and create a message for the user that describes the errors ecountered.
        /// 	string message = "";
        /// 	foreach (FieldError error in errors)
        /// 	{
        /// 		if (error.ErrorCode == FieldErrorCode.NullError)
        /// 			message += error.Field.PropertyName + " property may not be null. ";
        /// 		else
        /// 			message += error.Field.PropertyName + " contains an invalid value. ";
        /// 	}
        ///
        /// 	return message;
        /// }
        /// </code>
        /// </example>
        public virtual FieldError[] ValidateAllFields()
        {
            ArrayList errorList = new ArrayList();
            object[] entityValues = ToObjectArray();
            for (int fieldIdx = 0; fieldIdx < _Table.Columns.Count; fieldIdx++)
            {
                FieldError currError = new FieldError(_Table.Columns[fieldIdx], entityValues[fieldIdx]);
                if (currError.ErrorCode != FieldErrorCode.AllOk)
                    errorList.Add(currError);
            }

            FieldError[] errorArray = new FieldError[errorList.Count];
            for (int errIdx = 0; errIdx < errorArray.Length; errIdx++)
                errorArray[errIdx] = (FieldError)errorList[errIdx];

            return errorArray;
        }