简体   繁体   English

如何在没有任何框架的情况下使用自定义注释验证器POCO(没有asp.net,没有mvc,没有ORM)

[英]How to use a custom annotations validator POCO without any framework (no asp.net, no mvc, no ORM)

I have a custom validation class 我有一个自定义验证类

using System;
using System.Collections.Generic;
using System.Reflection;

 internal class RequiredAttribute1 : Attribute
{
    public RequiredAttribute1()
    {
    }

    public void Validate(object o, MemberInfo info, List<string> errors)
    {
        object value;

        switch (info.MemberType)
        {
            case MemberTypes.Field:
                value = ((FieldInfo)info).GetValue(o);
                break;

            case MemberTypes.Property:
                value = ((PropertyInfo)info).GetValue(o, null);
                break;

            default:
                throw new NotImplementedException();
        }

        if (value is string && string.IsNullOrEmpty(value as string))
        {
            string error = string.Format("{0} is required", info.Name);

            errors.Add(error);
        }
    }
}

I am using it on the following object:- 我在以下对象上使用它: -

class Fruit
{
 [RequiredAttribute1]
 public string Name {get; set;}

 [RequiredAttribute1]
 public string Description {get; set;}
}

Now, I want to run the validation rules on a list of fruits, to print to a string 现在,我想在水果列表上运行验证规则,以打印到字符串
All I can think of is :- 我能想到的是: -

  1. Iterate through fruits 通过水果迭代
  2. For each fruit, iterate through its properties 对于每个水果,迭代其属性
  3. For each property, iterate through custom validators (only 1 here...) 对于每个属性,迭代自定义验证器(这里只有1 ...)
  4. Call the Validate function 调用Validate函数
  5. Collect and print validator errors 收集并打印验证程序错误

Is there something easier than this and more built-in, for reading these annotations, without having to add framework dlls like (ASP.net / MVC /etc...) ? 有没有比这更容易和更内置的东西,用于阅读这些注释,而不必添加像(ASP.net / MVC / etc ...)这样的框架dll?
This is just for a simple Console application. 这仅适用于简单的控制台应用程序。

I managed to get it working using 我设法使用它

using System.ComponentModel.DataAnnotations;

class RequiredAttribute : ValidationAttribute
{ //... above code }

In the main Driver class... 在主要的Driver类......

using System.ComponentModel.DataAnnotations;

class Driver
{

public static void Main(string[] args)
{
            var results = new List<ValidationResult>();
            var vc = new ValidationContext(AreaDataRow, null, null);
            var errorMessages = new List<string>();

            if (!Validator.TryValidateObject(AreaDataRow, vc, results, true))
            {
                foreach (ValidationResult result in results)
                {
                    if (!string.IsNullOrEmpty(result.ErrorMessage))
                    {
                        errorMessages.Add(result.ErrorMessage);
                    }
                }

                isError = true;
            }
}
}

No frameworks or ORMS required, just the DataAnnotations library. 不需要框架或ORMS,只需要DataAnnotations库。
It can work with multiple [Attributes] 它可以使用多个[属性]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM