Telerik blogs

Read the other posts in this series:

In the previous blog posts listed above, I showed how Telerik’s new LINQ implementation works with WCF RIA Services. I showed how to build your own Domain Service as well as build custom query methods. In this post I will show how to build a metadata class. (Note: future versions of the OpenAccess LINQ Implementation will produce the metadata class for you automatically.)

The WCF RIA Services metadata class is a separate class from the DomainService that contains information about the entities. In this class you can write custom validation logic, set attributes of the properties of the entities or indicate whether the property is to be generated on the client or not.

To create this class, create a new class in Visual Studio and name it: YourDomainSeriveClassName.metadata.cs. For example, our DomainService is DomainService1, so the metadata class is: DomainService1.metadata.cs.

Erase everything in the class and then replace it with the following, using the proper namespace in your project:

    namespace SilverlightApplication6.Web
    {
     
 
    using System.ComponentModel.DataAnnotations
 
    //the MetadataTypeAttribute identifies CustomersMetadata as the class
    //that carries additional metadata for the Customers class.
    [MetadataTypeAttribute(typeof(Customers.CustomersMetadata))]
    public partial class Customers
    {
        internal sealed class CustomersMetadata
        {
            //Metadata classes are not meant to be instantiated.
            private CustomersMetadata()
            {
            }
 
            public string Address { get; set; }
  
            public string City { get; set; }
  
            public string CompanyName { get; set; }
  
            public string ContactName { get; set; }
  
            public string ContactTitle { get; set; }
  
            public string Country { get; set; }
  
            public string CustomerID { get; set; }
  
            public string Fax { get; set; }
  
            public string Phone { get; set; }
  
            public string PostalCode { get; set; }
  
            public string Region { get; set; }
        }
    }
}

As you can see this class has each of the properties of your entity (lines 16-26), now you can set them as required, specify a length, or validate with a RegEx pattern. You can also specify that a property should not be sent down to the client. Of course you can specify much more sophisticated rules, you can even write your own methods. >

Let’s do a quick example on the CompanyName property, we will set it to required, set an error message to be displayed if the field is not entered as well as set a length of 32. This is done with two attributes:

[Required(ErrorMessage
          = "CompanyName is Required!!")]
 
[StringLength(32)]
 
public string CompanyName { get; set; }

Now when you perform databinding, RIA Services will enforce these rules for you on the client. For example, if try to edit our data in the application built in Part II, RIA Services automatically adds validation for us and passes on the error message we specified in the attribute. (Note you have to add an UpdateCustomer method to your DomainService1 class to enable editing.)

clip_image002

Enjoy!


About the Author

Steve Forte

 sits on the board of several start-ups including Triton Works. Stephen is also the Microsoft Regional Director for the NY Metro region and speaks regularly at industry conferences around the world. He has written several books on application and database development including Programming SQL Server 2008 (MS Press).

Comments

Comments are disabled in preview mode.