There are many ways to ensure the data is valid before attempting to store it in the database. For instance, in WPF and WinForms applications, a popular approach is having your persistent entities implement IDataErrorInfo. This way you will be able to store the error information in the entity itself and retrieve it easily at any time.

Furthermore, your UI controls will probably support this interface and automatically call the properties validation, using the Error to show the error message. This is the case for example with RadGridView - a part of the RadControls for WinForms suite.You can see a nice explanation how it uses the IDataErrorInfo interface for data validation in this blog post.

The problem is that IDataErrorInfo requires a certain amount of code to be written for each of the entities in order for the validation to be fully functioning. What can be done automatically though is a basic implementation allowing you to extend each of the classes with a minimal effort. Something like:

#region IDataErrorInfo members
         
private string error = string.Empty;
public string Error
{
    get
    {
        return this.error;
    }
}
         
public string this[string propertyName]
{
    get
    {
        this.ValidatePropertyInternal(propertyName, ref this.error);
         
        return this.error;
    }
}
         
protected virtual void ValidatePropertyInternal(string propertyName, ref string error)
{
    this.ValidateProperty(propertyName, ref error);
}
         
// Please implement this method in a partial class in order to provide the error message depending on each of the properties.
partial void ValidateProperty(string propertyName, ref string error);
         
#endregion

This auto-generated code is now coming out of the box in Telerik OpenAccess ORM Q2 2013. 



All you have to do is to implement the partial method ValidateProperty, which would hold the specific business logic for validating each of the properties based on its name and you are good to go. 

While the Code Generation options have been now fully presented in our blog, we are just beginning to tell you about all the interesting features Q2 2013 of OpenAccess ORM will offer. Hint: Bulk Operations are coming, so stay tuned!


About the Author

Ivailo Ivanov

 is Team Lead in Telerik Data Access

Related Posts

Comments