C# C重写继承的验证
我的所有实体都从以下类继承:C# C重写继承的验证,c#,.net,C#,.net,我的所有实体都从以下类继承: using System.ComponentModel.DataAnnotations; #nullable enable namespace AutomationNavigator.Model.Core { public abstract class NamedEntity : Entity, INamedEntity { public NamedEntity() : base() { }
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; }
}
}
对名称字段的验证可以普遍应用,但我刚刚创建的特定场景除外,在该场景中,我需要在我的应用程序中允许特殊字符是名称字段。我可以找人覆盖/删除正则表达式验证吗?我试图完成的课程是:
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 = "";
}
}
我希望有帮助
干杯。这可能会对您有所帮助。我希望这会起作用:在抽象类中使名称为虚拟名称,并在例外情况下覆盖它。@MKR这是用于类属性的。我认为这种方法是可能的attributes@Julian您好,佩德罗。我首先要感谢你的详尽回答。我将使道具为虚拟的,然后在派生类中重写它。这是天才,我不知道你可以用属性来做,我认为这只是为方法保留的。每天学习新的东西。然而,我真的不明白你给我的第二个选择是什么。你能详细说明一下你在每节课上使用getter和setter做什么吗?谢谢,不用谢,迪恩。谢谢基类中的getter和setter仅用于本文中的封装检查:。在派生类中,setter可以类似于set{base.Name=value;},如果需要,可以使用新的验证,其中value关键字是客户机定义的值。注意,通过这样做,我们还重写了基类中的值。下面的文章有一个类似的示例,基类不是抽象类,但是: