MULTIMEDIA

Building LOB Applications : Data Validation through Data Annotation

3/19/2011 4:46:03 PM

1. Problem

You need to implement data validation within your Silverlight LOB application.

2. Solution

Take advantage of Data Annotations via attributes in Silverlight 4 to implement data validation.

3. How It Works

When you add a Domain Service to a web project, you have the option of generating a metadata class. The metadata class is a partial class that allows developers to apply validation configuration without having to worry about the classes getting regenerated automatically if the underlying model changes.

The data annotations sit in the System.ComponentModel.DataAnnotations namespace. There are three categories of attributes in this namespace that can be applied to entities, validation attributes, display attributes, and data modeling attributes. Table 1 has a list of the validation attributes that can be applied to entities via the metadata class.

Table 1. Path Context Menu Suboptions
Submen AttributeDescription
CustomValidationAttributeAllows the developer to identify a custom method in code to validate a property. This is an alternative to creating a custom validation attribute.
DataTypeAttributeHas a related enumeration named DataType that has pre-configured data types on it such as EmailAddress, Phone, etc.
EnumDataTypeAttributeValidates the value configured on a property to ensure it is part of the identified enumeration type.
RegularExpressionAttributeAllows the developer to specify a regular expression for validation.
RequiredAttributeIdentifies that the property is required.
StringLengthAttributeSpecifies a minimum and maximum character length.
ValidationAttributeAbstract base class for validation attributes.

The two available extension points for developers are to create a custom validation attribute that inherits from the ValidationAttribute base class or create a custom class method and designate that method using the CustomValidationAttribute class.

4. The Code

For the code, you proceed as before by establishing the WCF RIA Link in the project properties (as in Recipe 7 and 8). You also copy the UI code from Recipe 8 to have a working UI to start out. You generated a metadata class for the Customers Domain Service in Recipe 4 so you will edit Customer domain service metadata class.

The file NorthwindDomainService.metadata.cs contains the metadata classes for the NorthwindDomain Service. If you search in that file, you will find an internal sealed class named CustomerMetadata. You apply the [Required] attribute to a number of properties. You also apply the [DataType(DataType.PhoneNumber)] attribute to the Fax and Phone fields. Listing 1 shows the code.

Listing 1. The Recipe 9 MainPage.Xaml File
internal sealed class CustomerMetadatad
{
// Metadata classes are not meant to be instantiated.
private CustomerMetadata()
{
}

[Required]
public string Address { get; set; }

[Required]
public string City { get; set; }

[Required]
public string CompanyName { get; set; }

[Required]
public string ContactName { get; set; }

[Required]
public string ContactTitle { get; set; }

[Required]
public string Country { get; set; }

public EntityCollection<CustomerDemographic> CustomerDemographics { get; set; }

[Required]
public string CustomerID { get; set; }

[Required,DataType(DataType.PhoneNumber)]
public string Fax { get; set; }

public EntityCollection<Order> Orders { get; set; }

[Required, DataType(DataType.PhoneNumber)]
public string Phone { get; set; }


public string PostalCode { get; set; }


public string Region { get; set; }
}

Figure 1 shows the validation in action with the automatically generated error message at the bottom.

Figure 1. Final UI for Recipe 9

Other  
  •  Building LOB Applications : Implementing CRUD Operations in RIA Services
  •  Microsoft XNA Game Studio 3.0 : Displaying Images - Resources and Content (part 2) - Adding Resources to a Project
  •  Microsoft XNA Game Studio 3.0 : Displaying Images - Resources and Content (part 1)
  •  iPhone 3D Programming : Blending and Augmented Reality - Rendering Anti-Aliased Lines with Textures
  •  Programming with DirectX : Game Math - Bounding Geometry (part 2) - Bounding Spheres & Bounding Hierarchies
  •  Programming with DirectX : Game Math - Bounding Geometry (part 1) - Bounding Boxes
  •  Programming with DirectX : Game Math - Matrices
  •  iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 2) - Jittering
  •  iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 1) - A Super Simple Sample App for Supersampling
  •  Building LOB Applications : Navigating RIA LOB Data
  •  Building LOB Applications : Databinding in XAML
  •  Microsoft XNA Game Studio 3.0 : Program Bugs
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Adding Vibration
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Using the Keyboard
  •  iPhone 3D Programming : Blending and Augmented Reality - Stencil Alternatives for Older iPhones
  •  iPhone 3D Programming : Blending and Augmented Reality - Poor Man’s Reflection with the Stencil Buffer
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Reading a Gamepad
  •  iPhone 3D Programming : Blending and Augmented Reality - Shifting Texture Color with Per-Vertex Color
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Extensions and Their Uses
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Caveats
  •  
    Top 10
    Windows Server 2003 : Domain Name System - Command-Line Utilities
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 2)
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 1)
    Brother MFC-J4510DW - An Innovative All-In-One A3 Printer
    Computer Planet I7 Extreme Gaming PC
    All We Need To Know About Green Computing (Part 4)
    All We Need To Know About Green Computing (Part 3)
    All We Need To Know About Green Computing (Part 2)
    All We Need To Know About Green Computing (Part 1)
    Master Black-White Copying
    Most View
    Microsoft Tries To Flatten Competition With Surface (Part 3) - Dropbox drops Public Folders, SSD Prices Way Down, AMD Adopts Arm for Armor
    SQL Server 2008 R2 : Dropping Indexes, Online Indexing Operations, Indexes on Views
    NZXT Source 210 Elite - Finest Cases For Frugal Gamers
    OLED Me Be the One
    Toshiba Tecra R850
    Windows 8's Unexpected Features (Part 1)
    Computing Yourself Fit (Part 4)
    Crucial Ballistix Tactical LP 16GB Kit
    100 Windows Speed-Up Tips (Part 1) - Clean up your hard drive & Defrag your computer
    Organize Windows With Virtual Desktops
    Developing Applications for the Cloud on the Microsoft Windows Azure Platform : DNS Names, Certificates, and SSL in the Surveys Application
    Improve Your Mac (Part 2) - Add Music To You Movies
    LG Optimus L3 E400 Review (Part 1)
    Roku 2 XS
    Get An Awesome Satnav For Free
    Windows 7 : Syncing with Network Files (part 2) - Dealing with Conflict
    KWA 150 SE – The Most Expensive Amplifier Of ModWright
    Kindle Fire - The Second Coming (Part 1)
    Essential Wedding Kit (Part 3) - Shoot-saving kit
    Web Security : Attacking AJAX - Checking for Cross-Domain Access, Reading Private Data via JSON Hijacking