手动使用数据注释验证和对象图 [英] Using Data Annotations Validation Manually and Object Graphs

查看:94
本文介绍了手动使用数据注释验证和对象图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我有两个简单的类:

 公共类CustomerDetails 
{
[必需]
公共字符串地址
{
get;
组;
}
}

公共类客户
{
public Customer()
{
Details = new CustomerDetails();
}

[必需]
公共字符串名称
{
get;
组;
}
public CustomerDetails Details
{
get;
私人套装;
}
}

当我尝试在控制台中手动验证Customer类时

  var customer = new Customer(){Name = Conrad}; 
var context = new ValidationContext(customer,null,null);
var results = new List< ValidationResult>();
Validator.TryValidateObject(customer,context,true);

然后-即使我选择验证客户实例的所有属性-验证程序仅验证Name属性

这是设计使然,还是我在这里遗漏了什么?此外,如果这是设计使然,那么是否存在一种健壮的方法来手动验证装饰有验证属性(包括嵌套类型)的完整对象图,而不是手动对整个对象图使用验证器?



请注意,这是在控制台应用程序而不是ASP.NET MVC应用程序中测试的。



亲切的问候。

解决方案

我有几乎相同的问题,但有嵌套对象的集合。我可以通过在容器类上实现 IValidatableObject 来解决该问题。就您而言,这稍微容易些。这样的事情:

 公共类客户:IValidatableObject 
{
public IEnumerable< ValidationResult> Validate(ValidationContext validateContext)
{
var context = new ValidationContext(this.Details,validationContext.ServiceContainer,validationContext.Items);
var results = new List< ValidationResult>();
Validator.TryValidateObject(this.Details,上下文,结果);
个返回结果;
}
}

希望这会有所帮助。


Let's assume that I have two simple classes:

public class CustomerDetails
{
  [Required]
  public string Address
  {
    get;
    set;
  } 
}

public class Customer
{
   public Customer()
   {
     Details = new CustomerDetails();
   }

   [Required]
   public string Name
   {
     get;
     set;
   }
   public CustomerDetails Details
   {
     get;
     private set;
   } 
}

When I try to manually validate Customer class in a Console application in this way:

var customer = new Customer() { Name = "Conrad" };
var context = new ValidationContext(customer, null, null);
var results = new List<ValidationResult>();
Validator.TryValidateObject(customer, context, true);

Then -even though I chose to validate all properties of the customer instance- Validator just validates the Name property of the customer instance, but not the Address property of the Details.

Is this by design or am I missing something here? Moreover, if this is by design then is there a robust way to manually validate the full object graph decorated with validation attributes, including nested types instead of using validator for the whole object graph manually?

Please note that this is tested within a Console application and not an ASP.NET MVC application.

Kind regards.

解决方案

I had almost the same problem but with the collection of nested objects. I was able to resolve it by implementing IValidatableObject on a container class. In your case it's slightly easier. Something like this:

public class Customer : IValidatableObject
{
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        var context = new ValidationContext(this.Details, validationContext.ServiceContainer, validationContext.Items);
        var results = new List<ValidationResult>();
        Validator.TryValidateObject(this.Details, context, results);
        return results;
    }
}

Hope this helps.

这篇关于手动使用数据注释验证和对象图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆