/// <summary> /// This code gets called on the first time this Object type is constructed /// </summary> void AddSharedBusinessRules() { lock (_LockObject) { if (!(SharedValidationRules.RulesExistFor(this.GetType()))) { ValidationRulesManager mgrValidation = SharedValidationRules.GetManager(this.GetType()); CharacterCasingRulesManager mgrCharacterCasing = SharedCharacterCasingRules.GetManager(this.GetType()); foreach (PropertyInfo prop in this.GetType().GetProperties()) { foreach (BaseValidatorAttribute atr in prop.GetCustomAttributes(typeof(BaseValidatorAttribute), false)) { mgrValidation.AddRule(atr.Create(prop.Name), prop.Name); } foreach (CharacterCasingFormattingAttribute atr in prop.GetCustomAttributes(typeof(CharacterCasingFormattingAttribute), false)) { mgrCharacterCasing.AddRule(prop.Name, atr.CharacterCasing); } } AddSharedBusinessValidationRules(mgrValidation); AddSharedCharacterCasingFormattingRules(mgrCharacterCasing); } } }
/// <summary> /// This code gets called on the first time this Object type is constructed /// </summary> void AddSharedBusinessRules() { lock (_LockObject) { var entityType = this.GetType(); if (!SharedValidationRules.RulesExistFor(entityType)) { ValidationRulesManager mgrValidation = SharedValidationRules.GetManager(entityType); if (mgrValidation.RulesLoaded) { return; } mgrValidation.SetRulesLoaded(); CharacterFormattingRulesManager mgrCharacterCasing = SharedCharacterFormattingRules.GetManager(entityType); foreach (PropertyInfo prop in entityType.GetProperties()) { foreach (BaseValidatorAttribute atr in prop.GetCustomAttributes(typeof(BaseValidatorAttribute), false)) { mgrValidation.AddRule(atr, prop.Name); } foreach (CharacterFormattingAttribute atr in prop.GetCustomAttributes(typeof(CharacterFormattingAttribute), false)) { mgrCharacterCasing.AddRule(prop.Name, atr.CharacterCasing, atr.RemoveSpace, atr.PhoneExtension); } } AddSharedBusinessValidationRules(mgrValidation); AddSharedCharacterCasingFormattingRules(mgrCharacterCasing); } } }
/// <summary> /// This code gets called on the first time this Object type is constructed /// </summary> void AddSharedBusinessRules() { lock (_LockObject) { if (!SharedValidationRules.RulesExistFor(this.GetType())) { ValidationRulesManager mgrValidation = SharedValidationRules.GetManager(this.GetType()); CharacterFormattingRulesManager mgrCharacterCasing = SharedCharacterFormattingRules.GetManager(this.GetType()); foreach (var propInfo in PclReflection.GetPropertiesInfo(this)) { foreach (var atr in propInfo.GetCustomAttributes <BaseValidatorAttribute>(false)) { mgrValidation.AddRule(atr.Create(propInfo.Name), propInfo.Name); } foreach (var atr in propInfo.GetCustomAttributes <CharacterFormattingAttribute>(false)) { mgrCharacterCasing.AddRule(propInfo.Name, atr.CharacterCasing, atr.RemoveSpace); } } AddSharedBusinessValidationRules(mgrValidation); AddSharedCharacterCasingFormattingRules(mgrCharacterCasing); } } }
/// <summary> /// Validates the entity against all shared and instance rules. /// </summary> public void CheckAllRules() { Boolean raiseErrorPropertyChanged = false; ValidationRulesManager mgr = SharedValidationRules.GetManager(this.GetType()); foreach (ValidationRulesList vrl in mgr.RulesDictionary.Values) { foreach (IValidationRuleMethod obj in vrl.List) { if (RuleSetMatches(obj.RuleBase.RuleSet)) { //remove broken rule if it exists, if not does nothing if (this.ValidatationErrors.Remove(obj.RuleName)) { raiseErrorPropertyChanged = true; } if (obj.Invoke(this) == false) { raiseErrorPropertyChanged = true; this.ValidatationErrors.Add(obj.RuleName, new ValidationError(obj)); InternalRaisePropertyChanged(obj.RuleBase.PropertyName); } } } } if (_instanceValidationRulesManager != null) { foreach (ValidationRulesList vrl in this.InstanceValidationRulesManager.RulesDictionary.Values) { foreach (IValidationRuleMethod obj in vrl.List) { if (RuleSetMatches(obj.RuleBase.RuleSet)) { //remove broken rule if it exists, if not does nothing if (this.ValidatationErrors.Remove(obj.RuleName)) { raiseErrorPropertyChanged = true; } if (obj.Invoke(this) == false) { raiseErrorPropertyChanged = true; this.ValidatationErrors.Add(obj.RuleName, new ValidationError(obj)); InternalRaisePropertyChanged(obj.RuleBase.PropertyName); } } } } } if (raiseErrorPropertyChanged) { InternalRaisePropertyChanged(_STRING_ERROR); InternalRaisePropertyChanged(_STRING_HASERRORS); InternalRaisePropertyChanged(_STRING_HASNOERRORS); } }
/// <summary> /// Validates the property against all shared and instance rules for the property. /// </summary> /// <param name="propertyName">Name of the property.</param> public void CheckRulesForProperty(String propertyName) { Boolean raiseErrorPropertyChanged = false; ValidationRulesManager mgr = SharedValidationRules.GetManager(this.GetType()); foreach (IValidationRuleMethod obj in mgr.GetRulesForProperty(propertyName).List) { if (RuleSetMatches(obj.RuleBase.RuleSet)) { //remove broken rule if it exists, if not does nothing if (this.ValidationErrors.Remove(obj.RuleName)) { raiseErrorPropertyChanged = true; } if (obj.Invoke(this) == false) { raiseErrorPropertyChanged = true; this.ValidationErrors.Add(obj.RuleName, new ValidationError(obj)); } } } if (_instanceValidationRulesManager != null) { foreach (IValidationRuleMethod obj in this.InstanceValidationRulesManager.GetRulesForProperty(propertyName).List) { if (RuleSetMatches(obj.RuleBase.RuleSet)) { //remove broken rule if it exists, if not does nothing if (this.ValidationErrors.Remove(obj.RuleName)) { raiseErrorPropertyChanged = true; } if (obj.Invoke(this) == false) { raiseErrorPropertyChanged = true; this.ValidationErrors.Add(obj.RuleName, new ValidationError(obj)); } } } } if (raiseErrorPropertyChanged) { InternalRaisePropertyChanged(_STRING_ERROR); InternalRaisePropertyChanged(_STRING_ISVALID); InternalRaisePropertyChanged(_STRING_ISNOTVALID); InternalRaisePropertyChanged($"Item[{propertyName}]"); } }
ValidationRulesManager GetValidationRulesManagerForTarget <T>(T target) where T : class { ValidationRulesManager validationRulesManager = SharedValidationRules.GetManager(target.GetType()); if (!validationRulesManager.RulesLoaded) { lock (LockObject) { foreach (PropertyInfo prop in target.GetType().GetProperties()) { foreach (BaseValidatorAttribute atr in prop.GetCustomAttributes(typeof(BaseValidatorAttribute), false)) { validationRulesManager.AddRule(atr, prop.Name); } } } validationRulesManager.SetRulesLoaded(); } return(validationRulesManager); }
/// <summary> /// Validates the entity against all shared and instance rules. /// </summary> public void CheckAllRules() { Boolean raiseErrorPropertyChanged = false; ValidationRulesManager mgr = SharedValidationRules.GetManager(this.GetType()); if (this.ActiveRuleSet == GlobalConstants.Delete) { ClearValidationErrors(); } try { foreach (ValidationRulesList vrl in mgr.RulesDictionary.Values) { foreach (IValidationRuleMethod obj in vrl.List) { if (RuleSetMatches(obj.RuleBase.RuleSet)) { //remove broken rule if it exists, if not does nothing if (this.ValidationErrors.Remove(obj.RuleName)) { raiseErrorPropertyChanged = true; } if (obj.Invoke(this) == false) { raiseErrorPropertyChanged = true; this.ValidationErrors.Add(obj.RuleName, new ValidationError(obj)); InternalRaisePropertyChanged(obj.RuleBase.PropertyName); } } InternalRaisePropertyChanged($"Item[{obj.RuleBase.PropertyName}]"); } } #pragma warning disable 168 } catch (InvalidOperationException) { #pragma warning restore 168 // this is here because one time in 10,000,000,000 I got an invalid operation exception. } if (_instanceValidationRulesManager != null) { foreach (ValidationRulesList vrl in this.InstanceValidationRulesManager.RulesDictionary.Values) { foreach (IValidationRuleMethod obj in vrl.List) { if (RuleSetMatches(obj.RuleBase.RuleSet)) { //remove broken rule if it exists, if not does nothing if (this.ValidationErrors.Remove(obj.RuleName)) { raiseErrorPropertyChanged = true; } if (obj.Invoke(this) == false) { raiseErrorPropertyChanged = true; this.ValidationErrors.Add(obj.RuleName, new ValidationError(obj)); InternalRaisePropertyChanged(obj.RuleBase.PropertyName); } } InternalRaisePropertyChanged($"Item[{obj.RuleBase.PropertyName}]"); } } } if (raiseErrorPropertyChanged) { InternalRaisePropertyChanged(_STRING_ERROR); InternalRaisePropertyChanged(_STRING_ISVALID); InternalRaisePropertyChanged(_STRING_ISNOTVALID); } }