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 ?

paradise_human
  • 685
  • 2
  • 14
  • 31
  • don't redefine them in the derived class, since they are not abstract in the base class. – juharr Dec 12 '16 at 18:13
  • @juharr It's inherited from interface and I must implement it ! – paradise_human Dec 12 '16 at 18:16
  • Wait, what is the `Preson` class. Shouldn't `UserViewModel` inherit `PersonViewModel`? – juharr Dec 12 '16 at 18:18
  • In fact yes, but the Person is also inherited.Person Class is another Abstract Class that used for EF. – paradise_human Dec 12 '16 at 18:20
  • OK, If `UserViewMdoel` inherits from `PersonViewModel` instead it will get the properties and attributes you've defined to implement the interface no need to re-implement them in the derived class. – juharr Dec 12 '16 at 18:24
  • I know, but can I inherit from two base classes ? I can't. I must use interfaces and I must implement it. – paradise_human Dec 12 '16 at 18:27
  • If you want it to inherit from `Person` and implement `IPersonViewModel` then you will not be able to also inherit from `PersonViewModel`. The real question is why you want to inherit from `Person` in the first place. – juharr Dec 12 '16 at 18:32
  • Because the Person properties is the same to PersonViewModel and I don't want to have multiple references by repeating the properties to PersonViewModel. – paradise_human Dec 12 '16 at 18:40

1 Answers1

2

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; }
}
Will Ray
  • 10,621
  • 3
  • 46
  • 61
  • I think the best way is to separate the Person EF model from PersonViewModel . Is it true ? I mean separating responsibilities is not duplication properties and the properties can be repeated. Is this correct ? – paradise_human Dec 15 '16 at 04:16
  • @paradise_human I'm simply stating that annotations on interfaces do not carry-over to the classes which implement them. I'm not exactly sure what you're looking for beyond that. Duplication of properties happens a lot between layers. Most people use a solution such as AutoMapper to map values between them. – Will Ray Dec 15 '16 at 05:16