0

I need to have properties in a subclass of the base class for two endpoints in my API to be required one one endpoint and optional in the other.

public class Endpoint1RequestModel : BaseClass
{
    // Other properties specific to this endpoint here
}

public class Endpoint2RequestModel : BaseClass
{
    // Other properties specific to this endpoint here
}

public class BaseClass
{
    // Other shared properties here

    public PropertyInfo Property { get; set; }
}

public class PropertyInfo
{
    public string Address { get; set; }

    public string City { get; set; }

    public string State { get; set; }

    public string Zip { get; set; }
}

I need for the setup for Endpoint1 to be:

public class PropertyInfo
{
    public string Address { get; set; }

    public string City { get; set; }

    [Required]
    public string State { get; set; }

    [Required]
    public string Zip { get; set; }
}

And for Endpoint 2:

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

    [Required]
    public string City { get; set; }

    [Required]
    public string State { get; set; }

    [Required]
    public string Zip { get; set; }
}

I have tried a few things and just couldn't quite get it to function how I want it to.

  • Try the suggestions here: https://stackoverflow.com/questions/6550409/how-to-add-attributes-to-a-base-classs-properties – jlavallet Apr 10 '18 at 19:11
  • A Metadata class would work if the properties were directly in the inherited class, but I need it to update a subclass of the inheritied class, which I can't quite see how to do with this method. – Stephen Halpin Apr 10 '18 at 19:16

1 Answers1

0

why don't you do this:

Have State and Zip in the Base class, both required.

Endpoint1RequestModel has Address and City.

Endpoint2RequestModel has Address and City both Required.

so your classes now look like this:

public class BaseClass
{
    // Other shared properties here
    [Required] 
    public string State { get; set; }

    [Required]
    public string Zip { get; set; }
}

public class Endpoint1RequestModel : BaseClass
{
    public string Address { get; set; }
    public string City { get; set; }
}

public class Endpoint2RequestModel : BaseClass
{
    [Required]
    public string Address { get; set; }

    [Required]
    public string City { get; set; }
}

too much inheritance is bad, complicates everything.

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32
  • There is a lot more data in the models than just these fields, and I was wanting to organize the property information in the json payload being sent to the endpoints into its own sub section, as well as several other subsections of similarly grouped data. For right now I have just moved them into the main base class as abstract properties then I am overriding them in the two main models, but this isn't the ideal solution. I was hoping that there was some other way to override these attribute settings that I was not aware of, but maybe it's just a limitation. – Stephen Halpin Apr 11 '18 at 13:53