第六章 Validating with the Validation API

CHAPTER 6

Validating with the Validation API

Defining and Triggering Validation: An Overview

你可以使用以下方式设置你的验证规则:

  • 使用Data Annotations 或者 Fluent API。
  • 为属性或者类型自定义验证属性
  • 在所有实现了IValidatableObject 的类的 Validate方法中实现验证规则。
  • 在model中显式定义的关系约束。
  • 另外,你可以将验证规则注入到验证管道中

有以下方式引发DbContext 执行验证:

  • 默认情况下,在调用SaveChanges时,对所有Added或者Modified状态的实体执行验证
  • DbEntityEntry.GetValidationResult 方法将对一个单独的对象执行验证
  • DbEntityEntry 拥有一个途径来验证单独的属性
  • DbContext.GetValidationErrors 方法将遍历所有被DbContext跟踪的状态为Added或者Modified的实体,并对其执行验证

验证的执行顺序

验证的调用顺序

Validating a Single Object on Demand with GetValidationResult

GetValidationResult 方法允许你显式的验证一个单独的实体,它返回一个 ValidationResult 类型的结果,
该结果包含三个重要的成员,目前,我们仅关注IsValid 属性。该属性是一个bool类型的,
当为true时,表示通过了验证,反之没有通过验证。

下面的例子通过该方法对LastName属性进行验证,LastName属性设置了以下规则:

[MaxLength(10)] 
public string LastName { get; set; } 

Example 6-1. Method to test validation of LastName.

private static void ValidateNewPerson()
{
    var person = new Person
    {
        FirstName = "Julie",
        LastName = "Lerman",
        Photo = new PersonPhoto
        {
            Photo = new Byte[] { 0 }
        }
    };
    using (var context = new BreakAwayContext())
    {
        if (context.Entry(person).GetValidationResult().IsValid)
        {
            Console.WriteLine("Person is Valid");
        }
        else
        {
            Console.WriteLine("Person is Invalid");
        }
    }
}

执行结果

Person is Valid

GetValidationResult 方法调用必要的逻辑验证定义在实体属性上的 ValidationAttributes
接着会查找实体是否有CustomValidationAttribute 或者实现了IValidatableObject接口
如果有则调用其Validate方法进行验证。

Specifying Property Rules with ValidationAttribute Data Annotations

下面是可用的验证属性

DataTypeAttribute
 [DataType(DataType enum)]
RangeAttribute
 [Range (low value, high value, error message string)]
RegularExpressionAttribute
 [RegularExpression(@”expression”)]
RequiredAttribute
 [Required]
StringLengthAttribute
 [StringLength(max length value, MinimumLength=min length value)]
CustomValidationAttribute
 This attribute can be applied to a type as well as to a property.

Validating Facets Configured with the Fluent API

Fluent API 拥有和Data Annotations同样的功能,并比它更强大。

Validating Unmapped or “Transient” Properties

按照约定,没有同时包含Getter 和 Setter的属性,将不会被映射到数据库,另外你也可用
NotMapped 或者 Ignore fluent API 来设置属性不被映射到数据库。按照约定
没有被映射的属性将不会被验证。

但是,当你对其应用了ValidationAttribute时。Entity Framework 也会验证它。

Validating Complex Types

Entity Framework 允许使用复杂类型,同时你也可用对其使用数据注解。GetValidationResult方法
将验证这些属性。

Using Data Annotations with an EDMX Model

可用使用 associated metadata class 添加数据注解。

假如你利用DbContext 生成模板从EDMX生成类Person。该类将被声明为分部类

 public partial class Person

如果你想为该类的FirstName属性添加验证属性,你可用创建一个新类,在新类中模拟
属性的声明,并添加验证属性:

class Person_Metadata 
{    
    [MinLength(10)]
    public string FirstName { get; set; }
}

接下来的工作就是让Person类知道要从 Person_Metadata 获取应用的验证属性

[MetadataType(typeof(Person_Metadata))]
public partial class Person

Inspecting Validation Result Details

GetValidationResult 方法在验证失败时,并不是简单的抛出一个异常。而是返回一个
System.Data.Entity.Validation.DbEntityValidationResult 类型的结果,无论验证规则是否
通过。并且会为IsValid属性设置一个恰当的值,为没有通过的规则设置详细信息。

DbEntityValidationResult 还暴露一个ValidationErrors 属性。这个属性是一个包含
DbValidationError 类型的集合对象。另外它还包含一个Entry属性。这看起来有点多余。
因为我们好像可以直接访问entry。但是在一些高级场合。那些无法直接访问Entry的场合,它就显得
比较有用了。

Using CustomValidationAttributes

你可以建立自己的验证逻辑,并利用 Custom ValidationAttribute 应用的需要验证的属性
上。下面的例子展示了一个静态类BusinessValidations。它包含一个单独的验证方法DescriptionRules
被用来验证model的description 属性。该规则验证属性不能包含感叹号和表情符号。

Example 6-3. Static custom validations to be used by different classes

public static class BusinessValidations
{
    public static ValidationResult DescriptionRules(string value)
    {
        var errors = new System.Text.StringBuilder(); if (value != null)
        {
            var description = value as string;
            if (description.Contains("!")) { errors.AppendLine("Description should not contain '!'."); }
            if (description.Contains(":)") || description.Contains(":("))
            {
                errors.AppendLine("Description should not contain emoticons.");
            }
        }
        if (errors.Length > 0)
            return new ValidationResult(errors.ToString());
        else
            return ValidationResult.Success;
    }
}

Example 6-4. Applying the new DescriptionRules validation to a property

[MaxLength(500)] 
[CustomValidation(typeof(BusinessValidations), "DescriptionRules")] 
public string Description { get; set; }

CustomValidation用法

CustomValidation(包含验证方法的类型,验证方法字符串名称)

Validating Individual Properties on Demand

除了使用GetValidationResults 方法,DbEntityEntry 允许你深入单独的属性,就像前面章节演示的一样

context.Entry(trip).Property(t => t.Description); 

这返回一个代表Description 的DbPropertyEntry 类型的对象。该对象包含一个GetValidationErrors 方法,
返回一个 ICollection<DbValidationError> 类型的集合对象。

Example 6-6. Validating a property

private static void ValidatePropertyOnDemand()
{
    var trip = new Trip
    {
        EndDate = DateTime.Now,
        StartDate = DateTime.Now,
        CostUSD = 500.00M,
        Description = "Hope you won't be freezing :)"
    };
    using (var context = new BreakAwayContext())
    {
        var errors = context.Entry(trip).Property(t => t.Description).GetValidationErrors();
        Console.WriteLine("# Errors from Description validation: {0}", errors.Count());
    }
}

Specifying Type-Level Validation Rules

有两种方式进行类型级别的验证

  1. 实现IValidatableObject 接口
  2. 为类型添加CustomValidationAttributes
Using IValidatableObject for Type Validation

为类型实现IValidatableObject 接口的 Validate方法

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    if (StartDate.Date >= EndDate.Date)
    {
        yield return new ValidationResult("Start Date must be earlier than End Date", new[] { "StartDate", "EndDate" });
    }
}

你可以在Validate方法中实现多个验证,并用yield关键字返回所有的验证结果

Using CustomValidationAttributes for Type Validation

Example 6-12. Two validation methods to be used as Trip type attributes

public static ValidationResult TripDateValidator(Trip trip, ValidationContext validationContext)
{
    if (trip.StartDate.Date >= trip.EndDate.Date)
    {
        return new ValidationResult("Start Date must be earlier than End Date", new[] { "StartDate", "EndDate" });
    }
    return ValidationResult.Success;
}

public static ValidationResult TripCostInDescriptionValidator(Trip trip, ValidationContext validationContext)
{
    if (trip.CostUSD > 0) {
        if (trip.Description.Contains(Convert.ToInt32(trip.CostUSD).ToString()))
        {
            return new ValidationResult("Description cannot contain trip cost", new[] { "Description" });
        }
    }
    return ValidationResult.Success;
}

Example 6-13. Applying a type-level CustomValidationAttribute

[CustomValidation(typeof(Trip), "TripDateValidator")] 
[CustomValidation(typeof(Trip), "TripCostInDescriptionValidator")] 
public class Trip: IValidatableObject

Understanding How EF Combines Validations

当属性验证未通过时,类型验证不会被触发

验证执行的顺序

  1. Property-level validation on the entity and the base classes. If a property is complex its validation would also include the following:
  2. Property-level validation on the complex type and its base types
  3. Type-level validation on the complex type and its base types, including IValidata bleObject validation on the complex type
  4. Type-level validation on the entity and the base entity types, including IValidata bleObject validation

Validating Multiple Objects

除了为单独的对象调用GetValidationResult方法,你也可以调用DbContext.GetValidationErrors方法对必要的被跟踪对象
进行验证,之所以强调“必要的”,因为默认情况下GetValidationErrors 方法仅验证状态为Added 和 modified 的对象。

GetValidationErrors 不会验证关系约束!SaveChanges方法将验证关系约束。

Validating When Saving Changes

SaveChanges方法将自动执行验证

Reviewing ObjectContext. SaveChanges Workflow

  1. 默认情况下,SaveChanges方法调用 DetectChages方法来更新追踪信息。
  2. SaveChanges 迭代每个被追踪的需要被修改的对象。
  3. 检查每一个实体,确定它们的关系约束是否正常,如果不正常,抛出EntityUpdateException 异常
    并停止进一步处理。
  4. 如果所有的关系通过了验证,EF针对数据库构建并执行必要的SQL命令
  5. 如果SQL命令执行错误,抛出EntityUpdateException并停止进一步工作

因为SaveChanges 使用了DbTransaction (事务处理),如果中间任何一个环节出现了问题,所有成功的操作也将被回滚。

Understanding DbContext.SaveChanges Workflow

当你使用DbContext的SaveChanges方法时,在执行 ObjectContext.Savechanges 的工作流程之前,DbContext.SaveChanges
先调用GetValidationErrors方法,执行验证,如果没有任何错误,则调用ObjectContext.SaveChanges。因为 GetValidationErrors 方法
已经调用了DetectChanges,ObjectContext.SaveChanges将跳过这一步。

Figure 6-5 shows the execution path when your code calls DbContext.SaveChanges.

Disabling Validate Before Save

public class BreakAwayContext : DbContext
{
    public BreakAwayContext()
    {
        Configuration.ValidateOnSaveEnabled = false;
    }  
    ... rest of class logic 
}
原文地址:https://www.cnblogs.com/jameszh/p/6716841.html