private void UpdateValidationSource() { if (this.validationBase != null) { this.validationBase.ErrorsChanged -= this.ValidationBaseOnErrorsChanged; this.validationBase = null; this.validationPropertyName = null; } if (this.DataContext == null || string.IsNullOrEmpty(this.ValidationPropertyName)) { return; } PropertyInfo validationPropertyInfo = this.DataContext.GetType().GetProperty(this.ValidationPropertyName); if (validationPropertyInfo == null) { throw new InvalidOperationException( string.Format( "Validation source type {0} does not contain a property with name {1}", this.DataContext.GetType().Name, this.ValidationPropertyName)); } this.validationBase = this.DataContext as IValidationBase; if (this.validationBase == null) { return; } this.validationBase.ErrorsChanged += this.ValidationBaseOnErrorsChanged; this.ValidationBaseOnErrorsChanged(null, new DataErrorsChangedEventArgs(this.ValidationPropertyName)); }
protected override ValidationResult Evaluate(object value, object context) { IValidationBase validationBase = value as IValidationBase; if (validationBase == null) { return(null); } return(ValidationResult.FromRule(this, !validationBase.HasErrors)); }
/// <summary> /// Method which parse string to DateTime with validation /// </summary> /// <param name="dateString"> /// String with date for parsing /// </param> /// <returns> /// If input can be parsed method returns DateTime, otherwise returns null /// </returns> public DateTime?ParseDate(string dateString) { Validator = new DateParseValidation(dateString); if (!Validator.IsValid) { _customLogger.Error(Validator.Message); return(null); } return(DateTime.Parse(dateString)); }
private bool SetPropertyInternal <T>(string propertyName, ref T property, T newValue) { if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentNullException(nameof(propertyName)); } // ReSharper disable RedundantNameQualifier if (object.Equals(property, default(T)) && object.Equals(newValue, default(T))) { return(false); } if (!object.Equals(property, default(T)) && property.Equals(newValue)) { return(false); } if (this.EvaluateRulesForPropertyChange(propertyName, newValue)) { return(false); } this.RaisePropertyChanging(propertyName); IValidationBase oldVal = property as IValidationBase; if (oldVal != null) { oldVal.ErrorsChanged -= this.ChildElementErrorsChanged; } IValidationBase newVal = newValue as IValidationBase; if (newVal != null) { newVal.ErrorsChanged += this.ChildElementErrorsChanged; } property = newValue; this.RaisePropertyChanged(propertyName); return(true); }
public void Revalidate(bool recursive = true) { foreach (KeyValuePair <string, IList <IPropertyValidationRule> > ruleSet in this.PropertyValidationRules) { PropertyInfo propertyInfo = this.GetType().GetProperty(ruleSet.Key); Debug.Assert(propertyInfo != null, "propertyInfo != null"); object propertyValue = propertyInfo.GetValue(this); if (recursive) { IValidationBase validationBase = propertyValue as IValidationBase; validationBase?.Revalidate(true); } this.EvaluateRulesForPropertyChange(ruleSet.Key, propertyValue); } }
/// <summary> /// Calculate range for input dates /// </summary> /// <param name="startDate"> /// Start range date /// </param> /// <param name="endDate"> /// End range date /// </param> /// <returns> /// Calculated range /// </returns> public string CalculateRange(DateTime startDate, DateTime endDate) { Validator = new DatesOrderValidation(new DateTime[] { startDate, endDate }); if (!Validator.IsValid) { _customILogger.Error(Validator.Message); return(null); } if (startDate.Equals(endDate)) { return(GetRangeForSameDates(startDate)); } PatternsDTO patterns = _patternsUtil.GetPatterns(startDate, endDate); return(GetRangeWithSeparator(startDate, endDate, patterns.StartPattern, patterns.EndPattern)); }
public ServiceBase(IValidationBase <TEntity> validation, IRepositoryBase <TEntity> repository) { Validation = validation; Repository = repository; }