This is a mirror of official site: http://jasper-net.blogspot.com/

Implementing Data Validation in Silverlight with INotifyDataErrorInfo

| Sunday, June 26, 2011
Introduction

Data validation is an important aspect of any line of business application. It is used to make sure the data stored in the backend is valid and also to guide users in their data input tasks.

Several mechanisms already exist in client technologies to communicate to the application user when a particular piece of data is invalid and needs to be fixed. In some cases, data validation is closely working with data binding which is the ability to automatically push data between the user interface (UI) and the backend. Typically data is then validated as it is initially read to be displayed in a form, and reciprocally as new user input is flushed into the backend.

Both Microsoft Windows Forms (WF) and Windows Presentation Foundation (WPF) client technologies support the IDataErrorInfo interface which allows backend objects to communicate their error information to the UI layer. So far this interface has not been supported in Silverlight. But this is changing with the release of Silverlight 4 Beta. Its binding engine now takes full advantage of sources that implement IDataErrorInfo, just like the WPF binding engine does. Therefore business objects that were created for WF or WPF can now be reused in Silverlight applications without losing the benefits of their IDataErrorInfo implementation.

However IDataErrorInfo was originally designed for pure client environments such as WF and WPF and shows a limitation in a client-server technology like Silverlight: It does not include a means for notifying consumers that an error status has changed.

This is fine for pure client applications where the error status is known as soon as new data is pushed into the business object since the validation rules are applied locally. But for Silverlight applications where user-provided data often needs to be validated on a server, there really needs to be a mechanism for the binding source to advertise new errors asynchronously. Enters the new INotifyDataErrorInfo interface which was designed to fill this gap, in the Silverlight 4 Beta release.
A few words about Silverlight's new support for IDataErrorInfo

As in the .NET Framework 1.1 and subsequent releases, the IDataErrorInfo interface is defined in the System.ComponentModel namespace, in the System.dll assembly. The interface reference documentation can be found here: http://msdn.microsoft.com/en-us/library/system.componentmodel.idataerrorinfo.aspx. The Silverlight binding engine checks if the source of a binding is a property of an object that implements IDataErrorInfo when binding.ValidatesOnDataErrors is set to True. By default, Binding.ValidatesOnDataErrors is set to False. Because in Silverlight there is no concept of BindingGroup as in WPF, the binding engine will never access the IDataErrorInfo.Error property to report top-level validation errors. It only accesses the IDataErrorInfo.Item indexer for property-level errors.
Now to the new kid in town, INotifyDataErrorInfo

Let’s get straight to the interface definition:

namespace System.ComponentModel
{

using System;
using System.Collections;
public interface INotifyDataErrorInfo
{
// Returns True if the object has at least one property-level or top-level error.
bool HasErrors { get; }

// Returns the current set of property-level errors for the provided property name, or
// the current top-level errors if the argument is null or empty.
IEnumerable GetErrors(string propertyName);

// Raised when the set of errors for a particular property has changed, or when the
// top-level errors have changed.
event EventHandler ErrorsChanged;

}

public sealed class DataErrorsChangedEventArgs : EventArgs
{

// Constructor for the DataErrorsChangedEventArgs class. The provided argument can
// be null or empty for top-level object errors.
public DataErrorsChangedEventArgs(string propertyName);

// Returns the name of the property for which the errors have changed, or null/empty string
// when the notification is for the top-level errors.
public string PropertyName { get; }

}
}

Read more: Silverlight .NET

Posted via email from Jasper-net

0 comments: