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 Attribute | Description |
---|
CustomValidationAttribute | Allows
the developer to identify a custom method in code to validate a
property. This is an alternative to creating a custom validation
attribute. |
DataTypeAttribute | Has a related enumeration named DataType that has pre-configured data types on it such as EmailAddress, Phone, etc. |
EnumDataTypeAttribute | Validates the value configured on a property to ensure it is part of the identified enumeration type. |
RegularExpressionAttribute | Allows the developer to specify a regular expression for validation. |
RequiredAttribute | Identifies that the property is required. |
StringLengthAttribute | Specifies a minimum and maximum character length. |
ValidationAttribute | Abstract 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.