.NET Attribute在数据校验上的应用

简介:

.NET Attribute在数据校验上的应用

Attribute(特性)的概念不在此赘述了,相信有点.NET基础的开发人员都明白,用过Attribute的人也不在少数,毕竟很多框架都提供自定义的属性,类似于Newtonsoft.JSON中JsonProperty、JsonIgnore等

自定义特性
.NET 框架允许创建自定义特性,用于存储声明性的信息,且可在运行时被检索。该信息根据设计标准和应用程序需要,可与任何目标元素相关。

创建并使用自定义特性包含四个步骤:

声明自定义特性
构建自定义特性
在目标程序元素上应用自定义特性
通过反射访问特性
声明自定义特性
一个新的自定义特性必须派生自System.Attribute类,例如:

public class FieldDescriptionAttribute : Attribute
{

public string Description { get; private set; }

public FieldDescriptionAttribute(string description)
{
    Description = description;
}

}
public class UserEntity
{

[FieldDescription("用户名称")]
public string Name { get; set; }

}
该如何拿到我们标注的信息呢?这时候需要使用反射获取

  var type = typeof(UserEntity);
  var properties = type.GetProperties();
  foreach (var item in properties)
  {
      if(item.IsDefined(typeof(FieldDescriptionAttribute), true))
      {
          var attribute = item.GetCustomAttribute(typeof(FieldDescriptionAttribute)) as FieldDescriptionAttribute;
          Console.WriteLine(attribute.Description);
      }
  }

执行结果如下:

从执行结果上看,我们拿到了我们想要的数据,那么这个特性在实际使用过程中,到底有什么用途呢?

Attribute特性妙用
在实际开发过程中,我们的系统总会提供各种各样的对外接口,其中参数的校验是必不可少的一个环节。然而没有特性时,校验的代码是这样的:

public class UserEntity
{

  /// <summary>
  /// 姓名
  /// </summary>
  [FieldDescription("用户名称")]
  public string Name { get; set; }

  /// <summary>
  /// 年龄
  /// </summary>
  public int Age { get; set; }

  /// <summary>
  /// 地址
  /// </summary>
  public string Address { get; set; }

}

  UserEntity user = new UserEntity();

  if (string.IsNullOrWhiteSpace(user.Name))
  {
      throw new Exception("姓名不能为空");
  }
  if (user.Age <= 0)
  {
      throw new Exception("年龄不合法");
  }
  if (string.IsNullOrWhiteSpace(user.Address))
  {
      throw new Exception("地址不能为空");
  }

字段多了之后这种代码就看着非常繁琐,并且看上去不直观。对于这种繁琐又恶心的代码,有什么方法可以优化呢?
使用特性后的验证写法如下:

首先定义一个基础的校验属性,提供基础的校验方法

public abstract class AbstractCustomAttribute : Attribute
{
    /// <summary>
    /// 校验后的错误信息
    /// </summary>
    public string ErrorMessage { get; set; }

    /// <summary>
    /// 数据校验
    /// </summary>
    /// <param name="value"></param>
    public abstract void Validate(object value);
}

然后可以定义常用的一些对应的校验Attribute,例如RequiredAttribute、StringLengthAttribute

    /// <summary>
    /// 非空校验
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class RequiredAttribute : AbstractCustomAttribute
    {
        public override void Validate(object value)
        {
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
            {
                throw new Exception(string.IsNullOrWhiteSpace(ErrorMessage) ? "字段不能为空" : ErrorMessage);
            }
        }
    }

    /// <summary>
    /// 自定义验证,验证字符长度
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class StringLengthAttribute : AbstractCustomAttribute
    {
        private int _maxLength;
        private int _minLength;

        public StringLengthAttribute(int minLength, int maxLength)
        {
            this._maxLength = maxLength;
            this._minLength = minLength;
        }

        public override void Validate(object value)
        {
            if (value != null && value.ToString().Length >= _minLength && value.ToString().Length <= _maxLength)
            {
                return;
            }

            throw new Exception(string.IsNullOrWhiteSpace(ErrorMessage) ? $"字段长度必须在{_minLength}与{_maxLength}之间" : ErrorMessage);
        }
    }

添加一个用于校验的ValidateExtensions

public static class ValidateExtensions
{

  /// <summary>
  /// 校验
  /// </summary>
  /// <typeparam name="T"></typeparam>
  /// <returns></returns>
  public static void Validate<T>(this T entity) where T : class
  {
      Type type = entity.GetType();

      foreach (var item in type.GetProperties())
      {
          //需要对Property的字段类型做区分处理针对Object List 数组需要做区分处理
          if (item.PropertyType.IsPrimitive || item.PropertyType.IsEnum || item.PropertyType.IsValueType || item.PropertyType == typeof(string))
          {
              //如果是基元类型、枚举类型、值类型或者字符串 直接进行校验
              CheckProperty(entity, item);
          }
          else
          {
              //如果是引用类型
              var value = item.GetValue(entity, null);
              CheckProperty(entity, item);
              if (value != null)
              {
                  if ((item.PropertyType.IsGenericType && Array.Exists(item.PropertyType.GetInterfaces(), t => t.GetGenericTypeDefinition() == typeof(IList<>))) || item.PropertyType.IsArray)
                  {
                      //判断IEnumerable
                      var enumeratorMI = item.PropertyType.GetMethod("GetEnumerator");
                      var enumerator = enumeratorMI.Invoke(value, null);
                      var moveNextMI = enumerator.GetType().GetMethod("MoveNext");
                      var currentMI = enumerator.GetType().GetProperty("Current");
                      int index = 0;
                      while (Convert.ToBoolean(moveNextMI.Invoke(enumerator, null)))
                      {
                          var currentElement = currentMI.GetValue(enumerator, null);
                          if (currentElement != null)
                          {
                              currentElement.Validate();
                          }
                          index++;
                      }
                  }
                  else
                  {
                      value.Validate();
                  }
              }
          }
      }
  }

  private static void CheckProperty(object entity, PropertyInfo property)
  {
      if (property.IsDefined(typeof(AbstractCustomAttribute), true))//此处是重点
      {
          //此处是重点
          foreach (AbstractCustomAttribute attribute in property.GetCustomAttributes(typeof(AbstractCustomAttribute), true))
          {
              if (attribute == null)
              {
                  throw new Exception("AbstractCustomAttribute not instantiate");
              }

              attribute.Validate(property.GetValue(entity, null));
          }
      }
  }

}
新的实体类

public class UserEntity
{

  /// <summary>
  /// 姓名
  /// </summary>
  [Required]
  public string Name { get; set; }

  /// <summary>
  /// 年龄
  /// </summary>
  public int Age { get; set; }

  /// <summary>
  /// 地址
  /// </summary>
  [Required]
  public string Address { get; set; }

  [StringLength(11, 11)]
  public string PhoneNum { get; set; }

}
调用方式

UserEntity user = new UserEntity();
user.Validate();
上面的校验逻辑写的比较复杂,主要是考虑到对象中包含复杂对象的情况,如果都是简单对象,可以不用考虑,只需针对单个属性做字段校验
现有的方式是在校验不通过的时候抛出异常,此处大家也可以自定义异常来表示校验的问题,也可以返回自定义的校验结果实体来记录当前是哪个字段出的问题,留待大家自己实现
如果您有更好的建议和想法欢迎提出,共同进步

以上代码均为原创分享,若大家认为有不妥的地方,烦请留言指出,在下感激不尽

本文作者:hexuwsbg
出处:https://www.cnblogs.com/hexu0512/p/12879671.html

相关文章
|
5月前
|
算法 Java 调度
|
7天前
|
C# Windows
一款.NET开源、简洁易用的Windows桌面小说阅读应用
一款.NET开源、简洁易用的Windows桌面小说阅读应用
|
8天前
|
开发框架 物联网 测试技术
【专栏】.NET 开发:打造领先应用的基石
【4月更文挑战第29天】本文探讨了.NET开发框架为何成为构建领先应用的首选。高性能与稳定性是.NET的核心优势,它采用先进的技术和优化策略,如.NET Core的轻量级设计和JIT/AOT编译模式。跨平台兼容性让开发者能用相同代码库在不同操作系统上构建应用。现代化的开发体验,如C#语言的创新特性和Visual Studio的强大工具,提升了开发者生产力。丰富的生态系统和广泛支持,包括庞大的开发者社区和微软的持续投入,为.NET提供了坚实后盾。
|
8天前
|
机器学习/深度学习 人工智能 Cloud Native
【专栏】洞察.NET 技术的前沿应用
【4月更文挑战第29天】本文探讨了.NET技术的前沿应用,包括.NET Core的跨平台崛起、云原生及AI/机器学习领域的整合。.NET Core支持多平台运行,开源社区的参与促进了其快速发展和性能优化。Xamarin与.NET MAUI助力跨平台移动应用和统一界面开发,而云原生应用借助.NET Core与Azure云服务得以轻松构建和部署。此外,ML.NET和TensorFlow.NET为.NET开发者提供了机器学习和深度学习工具,推动智能应用和边缘计算的创新。.NET技术正持续演进,引领软件开发新趋势。
|
8天前
|
人工智能 物联网 开发者
【专栏】探究.NET 技术的创新应用
【4月更文挑战第29天】本文探讨了.NET技术的最新进展和创新应用,包括.NET 5及后续版本的统一平台、性能提升、跨平台支持、云集成优化和开源社区的贡献。在创新应用场景中,重点介绍了微服务架构、物联网、AI、游戏开发和移动应用。未来,.NET将持续优化性能,深化云原生应用,集成新兴技术,扩大社区生态,并促进相关教育和培训。开发者应把握.NET技术的潜力,积极参与其发展,创造更多创新软件产品。
|
8天前
|
安全 Linux API
【专栏】.NET 开发:打造卓越应用的秘诀
【4月更文挑战第29天】本文介绍了.NET技术的起源、核心特性和应用场景,揭示了其打造卓越应用的秘诀。自2002年推出,.NET历经发展,现支持跨平台,包括.NET Core和.NET 5。其核心特性包括:跨平台兼容性、面向对象编程、内置安全性和高效性能。丰富的类库、强大的开发工具、简洁的语言语法以及活跃的社区支持,使.NET成为构建高效、安全应用的理想选择。随着技术进步,.NET将持续赋能开发者创造更多可能性。
|
8天前
|
人工智能 安全 API
【专栏】理解 .NET 技术,打造优质应用
【4月更文挑战第29天】本文探讨了如何利用.NET技术构建高质量应用程序,介绍了.NET从2002年发展至今的历程,强调其跨平台能力、高效开发、丰富的类库和API、开源生态及安全性等优势。随着.NET 6的规划,平台将更加统一和跨平台,适应云计算、AI等新兴技术。.NET凭借其特性,成为开发者和企业创新的有力工具,未来将继续扮演重要角色。
|
8天前
|
机器学习/深度学习 自然语言处理 安全
【专栏】.NET 开发:构建智能应用的关键
【4月更文挑战第29天】本文探讨了.NET开发在构建智能应用中的关键作用,强调了其强大的框架、工具集、高效性能和跨平台支持。通过实例展示了.NET在人工智能、物联网及企业级应用中的应用。同时,指出了.NET开发面临的挑战,如技术更新的学习成本、性能优化、资源管理和安全隐私保护,并提出了应对策略。随着技术进步,.NET将在智能应用领域发挥更大作用,推动创新与便利。
|
22天前
|
开发框架 前端开发 JavaScript
采用C#.Net +JavaScript 开发的云LIS系统源码 二级医院应用案例有演示
技术架构:Asp.NET CORE 3.1 MVC + SQLserver + Redis等 开发语言:C# 6.0、JavaScript 前端框架:JQuery、EasyUI、Bootstrap 后端框架:MVC、SQLSugar等 数 据 库:SQLserver 2012
21 0