Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

May 4, 2011

Silverlight/WCF RIA Validation: Metadata Class

Filed under: ASP.NET RIA Services,C#,Silverlight @ 1:05 am

Data annotation attributes are a great way to add single property validation to your Silverlight/WCF RIA application. But what if you don’t want to inject attributes all over your business classes? The purpose of a metadata class is to have a place for all of these attributes external from the business class.

NOTE: This post is part of a set of validation posts which starts here and it uses the sample code from this prior post.

If your Silverlight application uses WCF RIA services and your own business objects, then you can set attributes on your business object properties to define single-attribute validation.

In this prior post, the code example demonstrated how to use the single property validation attributes available to validate that a last name is entered and does not exceed 20 characters, the attributes look like this:

[Required(ErrorMessage = "Please enter a last or family name.")]
[StringLength(20, ErrorMessage = "Last name cannot exceed 20 characters.")]
public string LastName { get; set; }

If you don’t want these attributes in your business classes, you can instead create a metadata file and store all of the attributes there.

1) Right-click on your business object’s Class Library project and add a new class (Add | New Item | Class).

In this example, the class was called Student. So the metadata class is named StudentMetadata.cs.

2) In  this new class, add any of the business object properties and associated attributes.

Note that the property names defined in the metadata class must exactly match the property names in the original class.

In C#:

using System.ComponentModel.DataAnnotations;

namespace InStepValidationExample.BL
{
internal sealed class StudentMetadata
{

   /// <summary>
   /// Private to ensure this class is not instantiated.
   /// </summary>
   private StudentMetadata()
   {}

  [Required(ErrorMessage = "Please enter a last or family name.")]
  [StringLength(20, ErrorMessage = "Last name cannot exceed 20 characters.")]
  public string LastName { get; set; }
}
}

In VB:

TBD

3) Associate the metadata class with its original class using a MetadataType attribute on the original class definition. This attribute associates the original business object class with the metadata class.

In C#:

namespace InStepValidationExample.BL
{
    [MetadataType(typeof(StudentMetadata))]
    public class Student
    {
     …
    }
}

In VB:

TBD

When you run the application, the validation should work as before:

image

Use this technique any time you want attributes on your business object properties, but want to define these attributes external from the business object classes.

Enjoy!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2019 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs