Conditional subclass check

I have an ASP.NET MVC 3 application with the following class:

public class Person
{
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public Address HomeAddress { get; set; }

  // [Optional("MailingAddrressSameAsHome")] - some custom attribute
  public Address MailingAddress { get; set; }

  public bool MailingAddrressSameAsHome { get; set; }
}


public class Address
{
  [Required]
  public string Street { get; set; }
  ...
}

And now I would like to confirm the mailing address only if MailingAddressSameAsHome is false. Unfortunately, I do not know how to stop checking properties in the Address class.

Do you have any ideas?

+3
source share
2 answers

You thought that if necessary, specify only MailingAddress, thereby allowing the MailingAddressSameAsHome property to be read-only:

    public bool MailingAddrressSameAsHome 
    {
      //Null means no MailingAddress, which means: use HomeAddress
      get { return MailingAddress == null ; } 
    }

If so, you can check MailingAddress whenever it is.

Regards, Morten

0
source

You can write this with a custom attribute

-2
source

Source: https://habr.com/ru/post/1792487/


All Articles