接口从抽象class到childclass的继承数据注解?
Inheritance data annotations from abstract class to child class by interface?
这是我的 parent 摘要 class :
public abstract class PersonViewModel:IPersonViewModel
{
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertYourMobileNumber")]
[Display(Name = "MobileNumber")]
//--RegularExpression ========================(1234567890)===!( ValiAsr1)
[RegularExpression(pattern: @"(\d{10})", ErrorMessage = "ErrorNotFindMobileNumber")]
#endregion
public string MobileNumber { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertPassword")]
[Display(Name = "Password")]
[DataType(DataType.Password)]
#endregion
public string Password { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertReTypePassword")]
[Display(Name = "ReTypePassword")]
[DataType(DataType.Password)]
#endregion
public string ReTypePassword { get; set; }
}
这是我的界面:
public interface IPersonViewModel
{
string MobileNumber { get; set; }
string Password { get; set; }
string ReTypePassword { get; set; }
}
和人 Class :
public abstract class Person
{
#region MainProperties
public Guid PersonId { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertYourFirstName")]
[Display(Name = "FirstName")]
[StringLength(15, MinimumLength = 2, ErrorMessage = "ErrorMin2Max15Words")]
[RegularExpression(pattern: @"(^[^-\s\d*]\D*$)", ErrorMessage = "ErrorNotAllowWhitspaceInFirstAndNumbers")]
#endregion
#region EFValidation
[Column(TypeName = "varchar(max)")]
#endregion
public string FirstName { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertYourLastName")]
[Display(Name = "LastName")]
[StringLength(15, MinimumLength = 2, ErrorMessage = "ErrorMin2Max15Words")]
[RegularExpression(pattern: @"(^[^-\s\d*]\D*$)", ErrorMessage = "ErrorNotAllowWhitspaceInFirstAndNumbers")]
#endregion
#region EFValidation
[Column(TypeName = "varchar(max)")]
#endregion
public string LastName { get; set; }
#region DataValidatorModel
[Required]
[Display(Name = "Gender")]
#endregion
#region EFValidation
[Column(TypeName = "varchar(max)")]
#endregion
public string Gender { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertYourProfileImage")]
[Display(Name = "ProfileImage")]
//--RegularExpression ========================(test.jpg)||(folder/test.jpg)
[RegularExpression(pattern: @"([^\s]+(\.(?i)(jpg|png|gif|bmp))$)", ErrorMessage = "ErrorItsNotImageFile")]
#endregion
#region EFValidation
[Column(TypeName = "varchar(max)")]
#endregion
public string ProfileImage { get; set; }
#endregion
public string FullName => $"{FirstName} {LastName}";
}
这是我的 Child Class :
public class UserViewModel:Person,IPersonViewModel
{
#region MainProperties
#region DataValidatorModel
[Display(Name ="Email")]
[DataType(DataType.EmailAddress)]
[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$",ErrorMessage ="ErrorValidEmail")]
#endregion
public string Email { get; set; }
public string MobileNumber { get; set; }
public string Password { get; set; }
public string ReTypePassword { get; set; }
#endregion
}
我的问题是如何通过接口继承数据注解到childclass?
我不能在没有接口的情况下直接这样做,因为它显然是多重继承。
这样做好吗?
还有其他方法吗?
事实上,我不希望有不止一个数据引用 annotations.Is 有什么办法可以做到这一点吗?
从你的基础class继承的属性有属性,没问题。
接口中定义的属性不会传递给实现该接口的 classes。你根本不能这样做:
public interface IPersonViewModel
{
[Required]
string MobileNumber { get; set; }
}
public class PersonViewModel : IPersonViewModel
{
// this will NOT get the [Required] attribute from the interface
public string MobileNumber { get; set; }
}
这是我的 parent 摘要 class :
public abstract class PersonViewModel:IPersonViewModel
{
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertYourMobileNumber")]
[Display(Name = "MobileNumber")]
//--RegularExpression ========================(1234567890)===!( ValiAsr1)
[RegularExpression(pattern: @"(\d{10})", ErrorMessage = "ErrorNotFindMobileNumber")]
#endregion
public string MobileNumber { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertPassword")]
[Display(Name = "Password")]
[DataType(DataType.Password)]
#endregion
public string Password { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertReTypePassword")]
[Display(Name = "ReTypePassword")]
[DataType(DataType.Password)]
#endregion
public string ReTypePassword { get; set; }
}
这是我的界面:
public interface IPersonViewModel
{
string MobileNumber { get; set; }
string Password { get; set; }
string ReTypePassword { get; set; }
}
和人 Class :
public abstract class Person
{
#region MainProperties
public Guid PersonId { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertYourFirstName")]
[Display(Name = "FirstName")]
[StringLength(15, MinimumLength = 2, ErrorMessage = "ErrorMin2Max15Words")]
[RegularExpression(pattern: @"(^[^-\s\d*]\D*$)", ErrorMessage = "ErrorNotAllowWhitspaceInFirstAndNumbers")]
#endregion
#region EFValidation
[Column(TypeName = "varchar(max)")]
#endregion
public string FirstName { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertYourLastName")]
[Display(Name = "LastName")]
[StringLength(15, MinimumLength = 2, ErrorMessage = "ErrorMin2Max15Words")]
[RegularExpression(pattern: @"(^[^-\s\d*]\D*$)", ErrorMessage = "ErrorNotAllowWhitspaceInFirstAndNumbers")]
#endregion
#region EFValidation
[Column(TypeName = "varchar(max)")]
#endregion
public string LastName { get; set; }
#region DataValidatorModel
[Required]
[Display(Name = "Gender")]
#endregion
#region EFValidation
[Column(TypeName = "varchar(max)")]
#endregion
public string Gender { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertYourProfileImage")]
[Display(Name = "ProfileImage")]
//--RegularExpression ========================(test.jpg)||(folder/test.jpg)
[RegularExpression(pattern: @"([^\s]+(\.(?i)(jpg|png|gif|bmp))$)", ErrorMessage = "ErrorItsNotImageFile")]
#endregion
#region EFValidation
[Column(TypeName = "varchar(max)")]
#endregion
public string ProfileImage { get; set; }
#endregion
public string FullName => $"{FirstName} {LastName}";
}
这是我的 Child Class :
public class UserViewModel:Person,IPersonViewModel
{
#region MainProperties
#region DataValidatorModel
[Display(Name ="Email")]
[DataType(DataType.EmailAddress)]
[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$",ErrorMessage ="ErrorValidEmail")]
#endregion
public string Email { get; set; }
public string MobileNumber { get; set; }
public string Password { get; set; }
public string ReTypePassword { get; set; }
#endregion
}
我的问题是如何通过接口继承数据注解到childclass?
我不能在没有接口的情况下直接这样做,因为它显然是多重继承。
这样做好吗?
还有其他方法吗?
事实上,我不希望有不止一个数据引用 annotations.Is 有什么办法可以做到这一点吗?
从你的基础class继承的属性有属性,没问题。
接口中定义的属性不会传递给实现该接口的 classes。你根本不能这样做:
public interface IPersonViewModel
{
[Required]
string MobileNumber { get; set; }
}
public class PersonViewModel : IPersonViewModel
{
// this will NOT get the [Required] attribute from the interface
public string MobileNumber { get; set; }
}