public class ChristmasSong : INotifyDataErrorInfo
{ private string title;
private string performedBy;
private TimeSpan duration;
private DateTime published;
private string error;
public Dictionary<string, List> errors =
new Dictionary<string,List>();
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{ if (PropertyChanged != null)
PropertyChanged(
this, new PropertyChangedEventArgs(propertyName)); }
public string Title
{ get
{ return title;
}
set
{ title =
value; ValidateTitleProperty();
}
}
private void ValidateTitleProperty()
{ ChristmasSongError christmasSongError =
new ChristmasSongError()
{ Severity = ErrorLevel.Error, ErrorName =
"TitleRequired", ErrorMessage =
"Title should not be empty" }; if (string.IsNullOrEmpty(title))
{ AddError(
"Title", christmasSongError); }
else
{ RemoveError(
"Title", "TitleRequired"); }
}
public string PerformedBy
{ get
{ return performedBy;
}
set
{ performedBy =
value; ValidatePerformedByProperty();
}
}
private void ValidatePerformedByProperty()
{ ChristmasSongError christmasSongError =
new ChristmasSongError()
{ Severity = ErrorLevel.Error, ErrorName =
"PerformedByRequired", ErrorMessage =
"The artist should not be empty" }; if (string.IsNullOrEmpty(performedBy))
{ AddError(
"PerformedBy", christmasSongError); }
else
{ RemoveError(
"PerformedBy", "PerformedByRequired"); }
}
public TimeSpan Duration
{ get
{ return duration;
}
set
{ duration =
value; ValidateDuration();
}
}
private void ValidateDuration()
{ ChristmasSongError durationNullError =
new ChristmasSongError()
{ Severity = ErrorLevel.CriticalError, ErrorName =
"DurationNull", ErrorMessage =
"The duration should not be empty" }; ChristmasSongError durationTooLongError =
new ChristmasSongError()
{ Severity = ErrorLevel.Error, ErrorName =
"DurationTooLong", ErrorMessage =
"The duration is too long" };
if (duration == TimeSpan.Zero)
{ AddError(
"Duration", durationNullError); }
else
{ RemoveError(
"Duration", "DurationNull"); }
if (duration.TotalSeconds > 500)
{ AddError(
"Duration", durationTooLongError); }
else
{ RemoveError(
"Duration", "DurationTooLong"); }
}
public DateTime Published
{ get
{ return published;
}
set
{ published =
value; VaidatePublished();
}
}
private void VaidatePublished()
{ ChristmasSongError publishedTooLow =
new ChristmasSongError()
{ Severity = ErrorLevel.CriticalError, ErrorName =
"PublishedTooLow", ErrorMessage =
"The date is too small" }; ChristmasSongError publishedTooHigh =
new ChristmasSongError()
{ Severity = ErrorLevel.CriticalError, ErrorName =
"PublishedTooHigh", ErrorMessage =
"The date is too high" };
if (published < new DateTime(1900, 1, 1))
{ AddError(
"Published", publishedTooLow); }
else
{ RemoveError(
"Published", "PublishedTooLow"); }
if (published > new DateTime( 2010, 1, 1))
{ AddError(
"Published", publishedTooHigh); }
else
{ RemoveError(
"Published", "PublishedTooHigh"); }
}
private void AddError(string propertyName, ChristmasSongError error)
{
if (!errors.ContainsKey(propertyName))
{ errors.Add(propertyName,
new List() { error }); }
else// adding the error to the already existing list
{ var list = errors[propertyName];
list.Add(error);
}
if (ErrorsChanged != null)
ErrorsChanged(
this, new DataErrorsChangedEventArgs(propertyName)); }
private void RemoveError(string propertyName, string errorName)
{ if (errors.ContainsKey(propertyName))
{ var christmasSongError = errors[propertyName]
.Where
(e => e.ErrorName == errorName).FirstOrDefault();
var list = errors[propertyName];
list.Remove(christmasSongError);
if (list.Count == 0)//no more errors for this property
{ errors.Remove(propertyName);
}
if (ErrorsChanged != null)
ErrorsChanged(
this, new DataErrorsChangedEventArgs(propertyName)); }
}
public event EventHandler ErrorsChanged;
public System.Collections.IEnumerable GetErrors(string propertyName)
{ if (string.IsNullOrEmpty(propertyName))//retrieve
errors for entire entity { return errors.Values;
}
else
{ if (errors.ContainsKey(propertyName))
return errors[propertyName];
return null;
}
}
public bool HasErrors
{ get
{ if (errors.Count == 0)
return false;
return true;
}
}
}