神仙?妖怪?谢谢!

Just do it...

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

如何验证数据
我们对实体和成员属性添加验证属性来实施验证规则。WCF RIA Service提供了几个验证属性来执行常用的验证检测,还提供了CustomValidationAttribute属性来执行自定义的验证检测。
在RIA Service中包含了如下的验证属性:

  • DataTypeAttribute
  • RangeAttribute
  • RegularExpressionAttribute
  • RequiredAttribute
  • StringLengthAttribute

我们在服务端添加验证属性,这些属性会传递给生成的客户端实体。在运行时,这些验证规则会应用到客户输入的数据上。我们必须通过添加元数据类来添加这些验证属性。

 

添加一个验证属性

  1. 为实体类添加一个元数据类,可以参考上一节内容。
  2. 对想要验证的实体或成员属性,添加验证属性来执行验证。下面的例子示例如何对一个名字为Address1的成员属性添加验证。
1 [Required]   
2 [StringLength(60)]   
3 public string AddressLine1; 

 

   3.  生成解决方案。
   4.  在silverlight的应用项目中,打开Generated_Code文件下的代码文件,注意到在客户端代码中也应用了验证属性。


添加自定义验证属性

  1. 对实体类添加一个元数据类。
  2. 添加一个共享代码文件,以*.shared.cs命名。这个文件将会包含一个自定义验证对象。
  3. 添加一个方法来判断是否数据有效。这个方法必须是public和static,还必须返回一个ValidationResult来表示验证的结果。下面示例是一个有名为 IsProductValid方法的ProductValidator类,这个方法验证一个Product实体。
代码
 1 public class ProductValidator   
 2 {   
 3     public static ValidationResult IsProductValid(Product productToValidate, ValidationContext context)   
 4     {   
 5         if (productToValidate.ListPrice < ((decimal).8 * productToValidate.StandardCost))   
 6         {   
 7             return new ValidationResult("ListPrice is below 80 percent of StandardCost.");   
 8         }   
 9         else  
10         {   
11             return ValidationResult.Success;   
12         }   
13     }   
14 }

 

   4.  对象要验证的实体或成员属性,添加[CustomValidationAttribute]批注属性,并传递验证对象和执行验证的方法的名字。下面的示例显示了对一个实体应用[CustomValidation]属性,验证对象的类型是 ProductValidator,验证方法的名字是 IsProductValid。

代码
 1 [CustomValidation(typeof(RIAServicesExample.Web.SharedCode.ProductValidator), "IsProductValid")]   
 2 [MetadataTypeAttribute(typeof(Product.ProductMetadata))]   
 3 public partial class Product   
 4 {   
 5     internal sealed class ProductMetadata   
 6     {   
 7         // Metadata classes are not meant to be instantiated.   
 8         private ProductMetadata()   
 9         {   
10         }   
11         public string Color;   
12         public Nullable<datetime> DiscontinuedDate;   
13         public decimal ListPrice;   
14         public DateTime ModifiedDate;   
15         public string Name;   
16         public Nullable<int> ProductCategoryID;   
17         public int ProductID;   
18         public Nullable<int> ProductModelID;   
19         public string ProductNumber;   
20         public Guid rowguid;   
21         public Nullable<datetime> SellEndDate;   
22         public DateTime SellStartDate;   
23         [Required()]   
24         [StringLength(20)]   
25         public string Size;   
26         public decimal StandardCost;   
27         public byte[] ThumbNailPhoto;   
28         public string ThumbnailPhotoFileName;   
29         public Nullable<decimal> Weight;   
30     }   
31 }

 

   5.  生成解决方案。
   6.  在Silverlight客户端,打开Generated_Code文件夹下的文件。注意到在共享代码中 CustomValidationAttribute应用到了实体上。

 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/blackant2/archive/2010/04/08/5461597.aspx

posted on 2010-06-05 01:19  E.Trock  阅读(794)  评论(0编辑  收藏  举报