Implement the INotifyDataErrorInfo

Topics: Prism v4 - Silverlight 4
Aug 17, 2011 at 1:36 AM
Edited Aug 17, 2011 at 1:37 AM

Dear All,

I was looking for a way to validate properties on my viewmodel untill I found a solution that did the job very well.

I then read a few posts about the PRISM implementation, since I am using PRISM 4 I am intrested to know what are the differences between  the two approachs (if any) here is implementation for the INotifyDataErrorInfo:

		#region Implementation of INotifyDataErrorInfo
		public IEnumerable GetErrors(string propertyName)
		{
			if (_errors.ContainsKey(propertyName))
			{
				IList<string> propertyErrors = _errors[propertyName]; 
				foreach (string propertyError in propertyErrors)
				{
					yield return propertyError;
				}
			} yield break;
		} 
		public bool HasErrors
		{
			get { return _errors.Count > 0; }
		}
		public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
		
		private void NotifyErrorsChanged(string propertyName)
		{
			if (ErrorsChanged != null) 
				ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
		}
		protected void ValidateProperty(string propertyName, object value)
		{
			DailyUnPostedDetailsViewModel objectToValidate = this; 
			var results = new List<ValidationResult>(); 
			bool isValid = Validator.TryValidateProperty(value, new ValidationContext(objectToValidate, null, null) { MemberName = propertyName }, results); 
			if (isValid)             
				RemoveErrorsForProperty(propertyName); 
			else            
				AddErrorsForProperty(propertyName, results); 
			NotifyErrorsChanged(propertyName);
		}
		public bool ValidateObject()
		{
			DailyUnPostedDetailsViewModel objectToValidate = this;
			_errors.Clear();
			Type objectType = objectToValidate.GetType();
			PropertyInfo[] properties = objectType.GetProperties();
			foreach (PropertyInfo property in properties)
			{
				if (property.GetCustomAttributes(typeof(ValidationAttribute), true).Any())
				{
					object value = property.GetValue(objectToValidate, null);
					ValidateProperty(property.Name, value);
				}
			} return !HasErrors;
		}
		private void AddErrorsForProperty(string propertyName, IEnumerable<ValidationResult> validationResults)
		{
			RemoveErrorsForProperty(propertyName); 
			_errors.Add(propertyName, validationResults.Select(vr => vr.ErrorMessage).ToList());
		}
		private void RemoveErrorsForProperty(string propertyName)
		{
			if (_errors.ContainsKey(propertyName))             
				_errors.Remove(propertyName);
		}
		#endregion

I can call ValidateObject()  to check the whole VM before submitting data to the server, also I can call ValidateProperty("properityName", value ) in the setter of each properity decorated with CustomValidation method.

Is this still valid under PRISM 4 implementation.

Please advice

Regards

Waleed

Developer
Aug 17, 2011 at 6:42 PM

Hi Waleed,

Prism provides proven patterns and practices, however, you are free to implement those patterns and practices as you see fit for your scenario.

So far, based on my understanding of your implementation, it seems to be a valid possibility to implement the INotifyDataErrorInfo interface as you describe above.

Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini