• 5 Subscribers
  • 114 Views
0

this is my parent abstract 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; }
}

And this my interface :

public interface IPersonViewModel
{
    string MobileNumber { get; set; }
    string Password { get; set; }
    string ReTypePassword { get; set; }

}

And Person 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}";
}

And this my 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
}

My Question is how can I inherit data annotations to child class by interface?

I Can't do that directly without interface because it's obviously multiple inheritance.

Is this a good way to do that ?

Is there a any alternative way ?

In fact I don't want to have more than one reference to data annotations.Is there any way to do that ?


1 answers in total

1
Will Ray Posted at 2016-12-14 22:00:27Z

The properties which are inherited from your base class with have the attributes, no problem.

Attributes defined in an interface are not passed to classes which implement that interface. You simply cannot do this:

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; }
}

Answer this questsion