首页 > 解决方案 > C# 覆盖继承的验证

问题描述

我有以下类,我的所有实体都继承自:

using System.ComponentModel.DataAnnotations;
#nullable enable

namespace AutomationNavigator.Model.Core
{
    public abstract class NamedEntity : Entity, INamedEntity
    {
        public NamedEntity() : base()
        {
        }

        [MaxLength(100,ErrorMessage ="Name must be 100 characters or less.")]
        [Required]
        [MinLength(3, ErrorMessage = "Name must be at least 3 characters.")]
        [RegularExpression("^[A-Za-z0-9_. ]{3,100}$")] // Alphanumeric with Underscore and Dot only
        [Display(Name= "Name")]
        public string? Name { get; set; }
    }
}

Name字段的验证可以普遍应用,除非在我刚刚创建的特定场景中,我需要允许特殊字符是我的应用程序中的名称字段。我可以以某种方式覆盖/删除正则表达式验证吗?我要完成的课程是:

namespace AutomationNavigator.Model.ProcessAssessment
{
    public class ProcessFile: NamedEntity
    {
        [Display(Name = "OrganizationId")]
        public Guid? OrganizationId { get; set; }

        [ForeignKey("OrganizationId")]
        [Display(Name = "Organization")]
        public Organization? Organization { get; set; }

        [Display(Name = "BusinessProcessId")]
        public Guid? BusinessProcessId { get; set; }

        [ForeignKey("BusinessProcessId")]
        [Display(Name = "BusinessProcess")]
        public BusinessProcess? BusinessProcess { get; set; }

        [Display(Name = "ProcessDocumentId")]
        public Guid? ProcessDocumentId { get; set; }

        [ForeignKey("ProcessDocumentId")]
        [Display(Name = "ProcessDocument")]
        public ProcessDocument? ProcessDocument { get; set; }

        [Display(Name = "ProcessFileStatusLookupId")]
        public Guid? ProcessFileStatusLookupId { get; set; }

        [ForeignKey("ProcessFileStatusLookupId")]
        [Display(Name = "LookupValue")]
        public LookupValue? LookupValue { get; set; }

        [Display(Name = "BlobId")]
        public Guid? BlobId { get; set; }

        [ForeignKey("BlobId")]
        [Display(Name = "Blob")]
        public Blob? Blob { get; set; }

        [Display(Name = "SizeInBytes")]
        public double? SizeInBytes { get; set; }

        [Display(Name = "Version")]
        public double? Version { get; set; }

        [MaxLength(500, ErrorMessage = "Description must be 500 characters or less.")]
        [Display(Name = "Description")]
        public string? Description { get; set; }
    }
}

标签: c#.net

解决方案


虚拟属性是允许后代自定义行为的好方法。

我会在抽象类中将 Name 属性设为虚拟,并在需要更改验证的派生特定类中覆盖它。

在抽象类中:

[MaxLength(100,ErrorMessage ="Name must be 100 characters or less.")]
[Required]
[MinLength(3, ErrorMessage = "Name must be at least 3 characters.")]
[RegularExpression("^[A-Za-z0-9_. ]{3,100}$")]
[Display(Name= "Name")]
public virtual string? Name { get; set; }

在派生类中:

public class ProcessFile: NamedEntity
{    
   public override string? Name { get; set;} 
}

您还可以使派生类直接使用基类的名称并对其进行验证。就像是:

public abstract class NamedEntity
{
    protected string name;

    public virtual string Name
    {
        get { return name; }
        set
        {
            //validation, if any
        }
    }

    public NamedEntity()
    {
        name = "";
    }

}

public class ProcessFile: NamedEntity
{

    public override string Name
    {
        get { return base.Name; }

        set { base.Name = // new validation; }
    }

    public ProcessFile()
        : base()
    {
        base.Name = "";        
    }
}

我希望它有所帮助。

干杯。


推荐阅读