c# - 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; }
}
}
解决方案
虚拟属性是允许后代自定义行为的好方法。
我会在抽象类中将 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 = "";
}
}
我希望它有所帮助。
干杯。
推荐阅读
- laravel - Laravel 完整日历
- reactjs - 使 React Material-UI Fab Button 粘在屏幕上的某个位置
- shell - 在linux中使用sed命令在模式之前和之后就地删除一行
- html - 如何在 HTML 中创建下载按钮?
- swift - 为什么数据常量没有初始化?
- javascript - 带有故事块的 Nuxt 不渲染图像
- java - 在 hadoop jar 调用中将参数传递给 log4j.properties
- angular - 使用带有 ngx-leaflet 的 vectorGrid 时的地图滚动性能问题
- node.js - Sequelize 4.42.0:设置实例方法返回“不是函数”错误
- python - 由于 for 循环,打印功能失败