By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Best practice for validating data bound business objects?

P: n/a
Hi,

I'm looking for the best practice for creating a generic data validation
implementation for my data bound business objects. I currently have a
business object base class implementing the following interfaces:
IEditableObject, ICloneable, INotifyPropertyChanged, and IDataErrorInfo

More specifically, my IDataErrorInfo implementation is like so:

[NonSerialized]
private Dictionary<string, string> propertyErrors;
[NonSerialized]
private string classError;

/// <summary>
/// Initializes a new instance of the BusinessObjectBase class
/// </summary>
public BusinessObjectBase()
{
propertyErrors = new Dictionary<string, string>();
}

#region IDataErrorInfo Members

/// <summary>
/// Gets or Sets the error message for the business object
/// </summary>
public string Error
{
get { return classError; }
set { classError = value; }
}

/// <summary>
/// Gets the current error message associated with the specified
property name
/// </summary>
/// <param name="propertyName">The property name</param>
/// <returns>An error message, or string.Empty</returns>
public string this[string propertyName]
{
get
{
if (propertyErrors.ContainsKey(propertyName))
{
return propertyErrors[propertyName];
}
else
{
return string.Empty;
}
}
}

/// <summary>
/// Sets the error message for a property
/// </summary>
/// <param name="propertyName">The property name</param>
/// <param name="error">The error message to set</param>
internal void SetPropertyError(string propertyName, string error)
{
if (propertyErrors.ContainsKey(propertyName))
{
propertyErrors[propertyName] = error;
}
else
{
propertyErrors.Add(propertyName, error);
}
}

#endregion

This works fine when editing existing (i.e. required Properties are valid)
business objects - i.e. if I data bind an ErrorProvider it will show up next
to the respective control and display the appropriate error message in a
tooltip.

The problems I have with this implementation are:

1. If a required property (i.e. EmailAddress, Name etc) starts off with a
null value and the user does not in fact change the value in the data-bound
TextBox, the ErrorProvider will not fire. I know why this is - because I'm
performing my validation in the Set method for the property, but obviously
there has to be a better way to do this.

2. When the user clicks an "OK" button on a data entry form, I can't figure
out how to check whether a validation error has occurred before calling
EndEdit on my binding source.

Is there some validation related interface I'm missing?

Any other ideas/suggestions?
Apr 24 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I've worked around the issue for now by creating custom validation attributes
and implementing a validation extender component, but I'd still like to know
what the accepted best practice is for the scenario posted earlier..

I'm baffled as to why the data binding classes and interfaces sin the CLR
fail to address the very common scenario of unchanged null value properties.
Apr 28 '06 #2

P: n/a
Hi. This is quite a useful method. Not really best practises though.

http://www.codeproject.com/csharp/De...essObjects.asp
May 30 '06 #3

P: n/a
"Adam" wrote:
Hi. This is quite a useful method. Not really best practises though.
http://www.codeproject.com/csharp/De...essObjects.asp


Thanks Adam, I've already implemented a similar workaround using custom
attributes, but really wanted to know what the official line on best practice
is.

It seems strange that MS went so far as to out in the IDataErrorInfo
interface, but yet it's so limited in it's usefulness. What I'm hoping is
there's some other interface I've missed, because the way it stands right now
the well-known business object interfaces (IEditableObject,
INotifyPropertyChanged, IDataErrorInfo) just don't cut it.
May 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.