【问题标题】:ASP.NET Core Reuse Code for Client-Side and Server-Side Validation用于客户端和服务器端验证的 ASP.NET Core 重用代码
【发布时间】:2020-03-05 05:24:00
【问题描述】:

我有一个使用 Razor 页面的 ASP.NET Core 项目。我正在使用实体框架,但使用我自己的自定义实体配置,如下所示:

namespace Customer.Data.Relational.Configuration {
   public sealed class CustomerConfiguration {
      public static void Configure(EntityTypeBuilder<CustomerEntity> entity) {
         entity.ToTable("customers")
            .HasKey(k => k.Id)
            .HasName("pk_customers");

         entity.Property(p => p.Id)
            .HasColumnName("customer_id")
            .HasColumnType("bigint")
            .ValueGeneratedOnAdd()
            .IsRequired();

         entity.Property(p => p.Code)
            .HasColumnName("customer_code")
            .HasColumnType("nvarchar(15)")
            .IsRequired();

         entity.Property(p => p.FirstName)
            .HasColumnName("first_name")
            .HasColumnType("nvarchar(100)")
            .IsRequired();

         entity.Property(p => p.LastName)
            .HasColumnName("last_name")
            .HasColumnType("nvarchar(100)")
            .IsRequired();

         entity.Property(p => p.EmailAddress)
            .HasColumnName("email_address")
            .HasColumnType("nvarchar(100)")
            .IsRequired();

         entity.Property(p => p.IsPremierCustomer)
            .HasColumnName("premier_customer")
            .HasColumnType("bit")
            .IsRequired();

         entity.HasIndex(k => k.Code)
            .HasName("uq_customers")
            .IsUnique();

         entity.HasOne(o => o.CustomerType)
            .WithMany()
            .HasForeignKey(f => f.CustomerTypeId)
            .OnDelete(DeleteBehavior.Restrict)
            .HasConstraintName("fk_customers_customer_types");
      }
   }
}

这允许我的实体类没有任何形式的注释(让它们只是 POCO)。

namespace Customer.Entities {
   public class CustomerEntity : IAuditableEntity {
      public long Id { get; set; } = default;

      public string Code {
         get; set;
      }

      public string FirstName {
         get; set;
      }

      public string LastName {
         get; set;
      }

      public string EmailAddress {
         get; set;
      }

      public bool IsPremierCustomer {
         get; set;
      }
   }
}

我使用FluentValidations 来验证用户输入。我创建了自定义验证器类。在客户端,我启用了 FluentValidation 与 ASP.NET Core 的集成。在服务器端,我通过调用 FluentValidation API 手动验证。

问题 - 这种方法正确吗?有没有其他方法可以在客户端和服务器上重复使用验证?任何指针或参考表示赞赏。

谢谢。

【问题讨论】:

    标签: c# entity-framework validation asp.net-core


    【解决方案1】:

    在高层次上,这听起来与我目前使用它的方式非常相似。进行客户端和服务器端验证都没有错,有时根据规则的复杂性/依赖性而定。

    一些可以导致验证器/规则重用的技巧:

    • 可以将通用规则提取到property validators 或分离到functional concerns,有助于重用/可维护性

    • Include(validator) 可以让您在某种程度上镜像您的 POCO 继承图,这意味着您可以重用来自父类验证器的规则

    • 考虑实现客户端/服务器端 rule sets 以允许您避免在客户端和服务器端重复处理/调用相同的规则,或者允许您的服务处理不同的验证场景(例如,对于使用相同服务层的 Web 应用和单独的无头服务,验证策略可能不同)

    • 设置自定义语言管理器以覆盖默认消息以避免过度使用 WithMessage 扩展

    在用户体验方面,我尝试将尽可能多的渠道集中到客户端。有时这意味着编写您自己的客户端适配器。如果您需要走这条路,This 是一个好的开始。

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 2013-01-10
      • 2016-10-16
      • 1970-01-01
      相关资源
      最近更新 更多