Blame view

netcore/src/Infrastructure/NCC/DataValidation/Extensions/DataValidationServiceCollectionExtensions.cs 3.72 KB
de2bd2f9   “wangming”   项目初始化
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  using NCC.DataValidation;
  using NCC.Dependency;
  using Microsoft.AspNetCore.Mvc;
  using System;
  
  namespace Microsoft.Extensions.DependencyInjection
  {
      /// <summary>
      /// 友好异常服务拓展类
      /// </summary>
      [SuppressSniffer]
      public static class DataValidationServiceCollectionExtensions
      {
          /// <summary>
          /// 添加全局数据验证
          /// </summary>
          /// <typeparam name="TValidationMessageTypeProvider">验证类型消息提供器</typeparam>
          /// <param name="mvcBuilder"></param>
          /// <param name="configure"></param>
          /// <returns></returns>
          public static IMvcBuilder AddDataValidation<TValidationMessageTypeProvider>(this IMvcBuilder mvcBuilder, Action<DataValidationServiceOptions> configure = null)
              where TValidationMessageTypeProvider : class, IValidationMessageTypeProvider
          {
              // 添加全局数据验证
              mvcBuilder.Services.AddDataValidation<TValidationMessageTypeProvider>(configure);
  
              return mvcBuilder;
          }
  
          /// <summary>
          /// 添加全局数据验证
          /// </summary>
          /// <typeparam name="TValidationMessageTypeProvider">验证类型消息提供器</typeparam>
          /// <param name="services"></param>
          /// <param name="configure"></param>
          /// <returns></returns>
          public static IServiceCollection AddDataValidation<TValidationMessageTypeProvider>(this IServiceCollection services, Action<DataValidationServiceOptions> configure = null)
              where TValidationMessageTypeProvider : class, IValidationMessageTypeProvider
          {
              // 添加全局数据验证
              services.AddDataValidation(configure);
  
              // 单例注册验证消息提供器
              services.AddSingleton<IValidationMessageTypeProvider, TValidationMessageTypeProvider>();
  
              return services;
          }
  
          /// <summary>
          /// 添加全局数据验证
          /// </summary>
          /// <param name="mvcBuilder"></param>
          /// <param name="configure"></param>
          /// <returns></returns>
          public static IMvcBuilder AddDataValidation(this IMvcBuilder mvcBuilder, Action<DataValidationServiceOptions> configure = null)
          {
              mvcBuilder.Services.AddDataValidation(configure);
  
              return mvcBuilder;
          }
  
          /// <summary>
          /// 添加全局数据验证
          /// </summary>
          /// <param name="services"></param>
          /// <param name="configure"></param>
          /// <returns></returns>
          public static IServiceCollection AddDataValidation(this IServiceCollection services, Action<DataValidationServiceOptions> configure = null)
          {
              // 添加验证配置文件支持
              services.AddConfigurableOptions<ValidationTypeMessageSettingsOptions>();
  
              // 载入服务配置选项
              var configureOptions = new DataValidationServiceOptions();
              configure?.Invoke(configureOptions);
  
              // 判断是否启用全局
              if (configureOptions.EnableGlobalDataValidation)
              {
                  // 添加自定义验证
                  services.Configure<ApiBehaviorOptions>(options =>
                  {
                      options.SuppressModelStateInvalidFilter = true;
                  });
  
                  // 添加全局数据验证
                  services.AddMvcFilter<DataValidationFilter>(options =>
                  {
                      // 关闭空引用对象验证
                      options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = configureOptions.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes;
                  });
              }
  
              return services;
          }
      }
  }