Question

My issue is the following. I have a Model for an Address table as follows.

public partial class ADDRESS
{
    public ADDRESS()
    {
        this.ADDRESS_ID = Guid.NewGuid();
    }

    public Guid ADDRESS_ID { get; set; }
    public string ADDRESS_LINE { get; set; }
    public string CITY { get; set; }
    public Guid? STATE_ID { get; set; }
    public string ZIP { get; set; }
}

This ADDRESS model is used throughout my application and has different validation requirements depending on where it is placed within a form.

I am wondering if there is a way to customize the data annotations. For example, the ADDRESS_LINE and CITY properties are required for OCCUPATION_ADDRESS but not required for WORK_LOCATION_ADDRESS.

public class OCCUPATION_DETAILS
{
    public string COMPANY_NAME { get; set; }
    public string JOB_TITLE { get; set; }
    etc...

    public Guid OCCUPATION_ADDRESS_ID { get; set; }
    public virtual ADDRESS OCCUPATION_ADDRESS { get; set; }

    public WORK_LOCATION_ID { get; set; }
    public virtual ADDRESS WORK_LOCATION_ADDRESS { get; set; }

}

Or it could be that basic validation is the same (zip has to be 5 digits), but I need to adjust the "DisplayName" that is used to match the label of the form it is within.

Trying to minimize the amount of copied models I have set up; otherwise maintenance will be a nightmare.

Was it helpful?

Solution

you can achieve this by using the idea of inheritance how?

public abstract class ADDRESS
{
    public ADDRESS()
    {
        this.ADDRESS_ID = Guid.NewGuid();
    }

    public Guid ADDRESS_ID { get; set; }
    public string ADDRESS_LINE { get; set; }
    public string CITY { get; set; }
    public Guid? STATE_ID { get; set; }
    public string ZIP { get; set; }
}

public parital class OCCUPATION_ADDRESS: ADDRESS,IValidatableObject
{
   public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
   {
       if(string.IsNullOrWhiteSpace(ADDRESS_LINE))
          yield return new ValidationResult("Address line is required!",new string[]{"ADDRESS_LINE"});
   }
}

public parital class WORK_LOCATION_ADDRESS: ADDRESS,IValidatableObject
{
   public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
   {

   }
}

// and your Occupation details class will be like: instead of ADDRESS you will use the corresponding derived classes
public class OCCUPATION_DETAILS
{
    public string COMPANY_NAME { get; set; }
    public string JOB_TITLE { get; set; }
    etc...

    public Guid OCCUPATION_ADDRESS_ID { get; set; }
    public virtual OCCUPATION_ADDRESS OCCUPATION_ADDRESS { get; set; }

    public WORK_LOCATION_ID { get; set; }
    public virtual WORK_LOCATION_ADDRESS WORK_LOCATION_ADDRESS { get; set; }

}

and the inheritance should be Table per Hierarchy (TPH)

for more information about TPH check this Table per hierarchy

hope that this will help you

regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top