Permalink
Browse files

Fix coding style to comply with style guidelines in CONTRIBUTING.md

  • Loading branch information...
jlaanstra committed Mar 30, 2013
1 parent 9afa7b8 commit 12ffe7741d47af0e9eb1269de0da704b9db5ab28
@@ -20,23 +20,17 @@ public static void ValidateViaAttributes(this ReactiveValidatedObject This)
.Select(p => Tuple.Create(p, p.GetCustomAttributes(typeof(ValidationAttribute), true).Cast<ValidationAttribute>() ?? new ValidationAttribute[0]))
.Where(x => x.Item2.Any());
- //
- foreach (var property in properties)
- {
+ //for each property add validation for every attribute
+ foreach (var property in properties) {
var propertyName = property.Item1.Name;
var validationAttributes = property.Item2;
- This.Validate<object,object>(propertyName, ioc =>
- {
- foreach (var v in validationAttributes)
- {
- try
- {
+ This.Validate<object,object>(propertyName, ioc => {
+ foreach (var v in validationAttributes) {
+ try {
var ctx = new ValidationContext(This, null, null) { MemberName = propertyName };
v.Validate(ioc.Value, ctx);
- }
- catch (Exception ex)
- {
+ } catch (Exception ex) {
This.Log().Info("{0:X}.{1} failed validation: {2}", This.GetHashCode(), propertyName, ex.Message);
return ex.Message;
}
@@ -63,27 +63,20 @@ public ReactiveValidatedObject()
{
_validationObservable = new Subject<IObservedChange<object, bool>>();
- this.Changed.Subscribe(x =>
- {
+ this.Changed.Subscribe(x => {
if (x.Sender != this)
{
return;
}
- this.CheckPropertyForValidationErrors(x);
+ this.checkPropertyForValidationErrors(x);
});
}
/// <summary>
/// Gets a value that indicates whether the entity has validation errors.
/// </summary>
/// <returns>true if the entity currently has validation errors; otherwise, false.</returns>
- public bool HasErrors
- {
- get
- {
- return this._validationErrors.Any();
- }
- }
+ public bool HasErrors { get { return this._validationErrors.Any(); } }
/// <summary>
/// Gets the validation errors for a specified property or for the entire entity.
@@ -93,17 +86,13 @@ public bool HasErrors
/// <returns>
/// The validation errors for the property or entity.
/// </returns>
- public IEnumerable GetErrors(string propertyName)
+ public IEnumerable GetErrors(string propertyName)
{
- if (string.IsNullOrEmpty(propertyName))
- {
+ if (string.IsNullOrEmpty(propertyName)) {
return _validationErrors.Values;
- }
- else
- {
+ } else {
string error;
- if (_validationErrors.TryGetValue(propertyName, out error))
- {
+ if (_validationErrors.TryGetValue(propertyName, out error)) {
return new string[] { error };
}
return Enumerable.Empty<string>();
@@ -122,7 +111,7 @@ public IEnumerable GetErrors(string propertyName)
///
/// </summary>
/// <param name="change"></param>
- private void CheckPropertyForValidationErrors(IObservedChange<object, object> change)
+ private void checkPropertyForValidationErrors(IObservedChange<object, object> change)
{
string prevResult;
_validationErrors.TryGetValue(change.PropertyName, out prevResult);
@@ -131,17 +120,13 @@ private void CheckPropertyForValidationErrors(IObservedChange<object, object> ch
string result = getPropertyValidationError(change);
this.Log().Debug("Validation result: {0}", result);
- if (result == null)
- {
+ if (result == null) {
_validationErrors.Remove(change.PropertyName);
- }
- else
- {
+ } else {
_validationErrors[change.PropertyName] = result;
}
- if (result != prevResult)
- {
+ if (result != prevResult) {
this.OnErrorsChanged(new DataErrorsChangedEventArgs(change.PropertyName));
}
}
@@ -153,26 +138,21 @@ private void CheckPropertyForValidationErrors(IObservedChange<object, object> ch
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="change">The property change.</param>
/// <returns>An error message or null.</returns>
- private string getPropertyValidationError<TSender, TValue>(IObservedChange<TSender, TValue> change)
+ private string getPropertyValidationError(IObservedChange<object, object> change)
{
- IList<Func<IObservedChange<object, object>, string>> list;
+ IList<Func<IObservedChange<object, object>, string>> validationFunctions;
lock (_validatedProperties) {
- if (!_validatedProperties.TryGetValue(change.PropertyName, out list)) {
+ if (!_validatedProperties.TryGetValue(change.PropertyName, out validationFunctions)) {
return null;
}
}
-
- IList<Func<IObservedChange<TSender, TValue>, string>> validationFunctions = list as IList<Func<IObservedChange<TSender, TValue>, string>>;
-
//make sure we have a value
change.fillInValue();
- foreach (var v in validationFunctions)
- {
+ foreach (var v in validationFunctions) {
string result = v(change);
- if (result != null)
- {
+ if (result != null) {
this.Log().Info("{0:X}.{1} failed validation: {2}", this.GetHashCode(), change.PropertyName, result);
return result;
}
@@ -188,26 +168,22 @@ private void CheckPropertyForValidationErrors(IObservedChange<object, object> ch
/// <typeparam name="TProperty">The type of the property.</typeparam>
/// <param name="property">The expression containing the property name.</param>
/// <param name="result">The function to be called when a change occurs.</param>
- public void Validate<TSender, TProperty>(string propertyName, Func<IObservedChange<TSender, TProperty>,string> result)
+ public void Validate<TSender, TProperty>(string propertyName, Func<IObservedChange<TSender, TProperty>,string> result)
{
Contract.Requires(propertyName != null);
Contract.Requires(result != null);
IList<Func<IObservedChange<object, object>, string>> validationFunctions;
- if (_validatedProperties.TryGetValue(propertyName, out validationFunctions))
- {
+ if (_validatedProperties.TryGetValue(propertyName, out validationFunctions)) {
validationFunctions.Add((Func<IObservedChange<object, object>,string>)result);
- }
- else
- {
+ } else {
validationFunctions = new List<Func<IObservedChange<object, object>, string>>();
validationFunctions.Add((Func<IObservedChange<object, object>, string>)result);
_validatedProperties[propertyName] = validationFunctions;
}
//initial check
- this.CheckPropertyForValidationErrors(new ObservedChange<object, object>()
- {
+ this.checkPropertyForValidationErrors(new ObservedChange<object, object>() {
PropertyName = propertyName,
Sender = this,
// value will be filled in later

0 comments on commit 12ffe77

Please sign in to comment.