473,394 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Best practice for validating data bound business objects?

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
3 4946
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
Hi. This is quite a useful method. Not really best practises though.

http://www.codeproject.com/csharp/De...essObjects.asp
May 30 '06 #3
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
1
by: Sam Martin | last post by:
Hi all, I've sorted out binding to and from my business entities, however.... Pulling data from the business entities is easy using the <control>.DataBindings.Add(..) method. These bindings take...
9
by: Alfred Taylor | last post by:
I'm testing the waters of n-tier development and I ran into a scenario that I'm not sure what the best solution would be. I have a Company object which contains a collection of contacts retrieved...
18
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
3
by: Marc Gravell | last post by:
Kind of an open question on best-practice for smart-client design. I'd really appreciate anyones views (preferably with reasoning, but I'll take what I get...). Or if anybody has any useful links...
3
by: cbrown | last post by:
I am rebuilding an existing application that relies on an SQL DB. The app is a scheduling/employee management program. My question pertains to best practices in dotnet and database. I use a 3...
13
by: Alan Silver | last post by:
Hello, MSDN (amongst other places) is full of helpful advice on ways to do data access, but they all seem geared to wards enterprise applications. Maybe I'm in a minority, but I don't have those...
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
2
by: Gabriel | last post by:
Hello, I'm looking for documentation with "Best Practice" for ASP.NET application In which case use Connected or Disconnected mode Typed dataset or not ? I didn'd find anything pertinent...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.