zoukankan      html  css  js  c++  java
  • Silverlight WCF RIA服务(十三)数据 3

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

    • DataTypeAttribute
    • RangeAttribute
    • RegularExpressionAttribute
    • RequiredAttribute
    • StringLengthAttribute

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

    添加一个验证属性

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

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

    添加自定义验证属性

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

       
    4. 对象要验证的实体或成员属性,添加[CustomValidationAttribute]批注属性,并传递验证对象和执行验证的方法的名字。下面的示例显示了对一个实体应用[CustomValidation]属性,验证对象的类型是 ProductValidator,验证方法的名字是 IsProductValid。
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      [CustomValidation(typeof(RIAServicesExample.Web.SharedCode.ProductValidator),"IsProductValid")]
      [MetadataTypeAttribute(typeof(Product.ProductMetadata))]
      public partial class Product
      {
       
          internal sealed class ProductMetadata
          {
       
              // Metadata classes are not meant to be instantiated.
              private ProductMetadata()
              {
              }
       
              public string Color;
       
              public Nullable<DATETIME> DiscontinuedDate;
       
              public decimal ListPrice;
       
              public DateTime ModifiedDate;
       
              public string Name;
       
              public Nullable<INT> ProductCategoryID;
       
              public int ProductID;
       
              public Nullable<INT> ProductModelID;
       
              public string ProductNumber;
       
              public Guid rowguid;
       
              public Nullable<DATETIME> SellEndDate;
       
              public DateTime SellStartDate;
       
              [Required()]
              [StringLength(20)]
              public string Size;
       
              public decimal StandardCost;
       
              public byte[] ThumbNailPhoto;
       
              public string ThumbnailPhotoFileName;
       
              public Nullable<DECIMAL> Weight;
          }
      }

       
    5. 生成解决方案。
    6. 在Silverlight客户端,打开Generated_Code文件夹下的文件。注意到在共享代码中CustomValidationAttribute应用到了实体上。
    Powered By D&J (URL:http://www.cnblogs.com/Areas/)
  • 相关阅读:
    duilib中各控件响应的消息类型
    CoInitialize()、CoInitializeEx()和AfxOleInit()区别联系
    Linux下反斜杠号""引发的思考
    人生规划和GTD——“知”、“得”与“合”
    一次失败的面试——IBM电话面试
    Duilib介绍以及各个类的简介
    Linux与Windows中动态链接库的分析与对比
    VC++创建、调用dll的方法步骤
    网络对抗 Exp0 Kali安装 Week1
    2018-2019-1 20165226_20165310_20165315 实验五 通讯协议设计
  • 原文地址:https://www.cnblogs.com/Areas/p/2172169.html
Copyright © 2011-2022 走看看