/// <summary> /// Validate the given object. Handles integer and Guid type identifiers. /// </summary> /// <param name="businessObject"> /// The business object to validate. /// </param> /// <returns> /// Returns true if validation were succesful. /// </returns> public override bool Validate(ValidatableObjectBase businessObject) { try { // Get identity property from business object var identity = GetPropertyValue(businessObject); // If identity is integer check that it is 0 or positive value if (identity.GetType() == typeof(int)) { int id; if (int.TryParse(identity.ToString(), out id)) { return(id >= 0); } // Parse failed. return(false); } // If identity is GUID check that parsing is successful if (identity.GetType() == typeof(Guid)) { Guid id; if (Guid.TryParse(identity.ToString(), out id)) { // If id is empty return false; return(id != Guid.Empty); } // Parse failed return(false); } // No match on types return false return(false); } catch (Exception exception) { Debug.WriteLine(exception); return(false); } }
/// <summary> /// Validates the object. /// </summary> /// <param name="businessObject"> /// The business object to validate /// </param> /// <returns> /// Returns true if the validation were successful. /// </returns> public override bool Validate(ValidatableObjectBase businessObject) { try { // Get property from the given business object var property = this.GetPropertyValue(businessObject).ToString(); // If regular expression is empty, throw ArgumentException // If expression is null and match is called, it counts as success, but I wanted to cut that out. if (string.IsNullOrEmpty(this.expression)) { throw new ArgumentException("Expression value is not set. Currently RegexValidationRule does not support empty validation expression."); } // Match propertys value to the given expression return(Regex.Match(property, this.expression).Success); } catch (Exception exception) { Debug.WriteLine(exception); return(false); } }
/// <summary> /// Gets value from the business object using simple reflection. /// </summary> /// <param name="businessObject"> /// Business object to inspect /// </param> /// <returns> /// Returns Property's object or null /// </returns> protected object GetPropertyValue(ValidatableObjectBase businessObject) { return(businessObject.GetType().GetProperty(this.PropertyName).GetValue(businessObject, null)); }
/// <summary> /// Validation method that contains validation implmentation. Needs to be implemented on the derived objects. /// </summary> /// <param name="businessObject"> /// Business object to validate. /// </param> /// <returns> /// Returns true if validation was successful. /// </returns> public abstract bool Validate(ValidatableObjectBase businessObject);