0

I have a class structure that looks like this:

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

class Address
{
public virtual string Street {get;set;}
public virtual string City {get;set;}
public virtual string State {get;set;}
public virtual string ZipCode {get;set;}
public virtual string Country {get;set;}
}

Then I have the idea of a person with Required Address and a person that uses the RequiredAddress... so I have my new "Required" address looking like:

class RequiredAddress
: Address
{
[Required]
public override string Street {...}
[Required]
public override string City {...}
[Required]
public override string State {...}
[Required]
public override string ZipCode {...}
[Required]
public override string Country {...}
}

but when I just newed up an instance of RequiredAddress into the Address field of my RequiredPerson address, MVC couldn't seem to detect the [Required] tags, and I wound up having to do this:

class PersonRequiredAddress
    : Person
{
public new RequiredAddress Address {get;set;}
}

Now, because we had to do this, we wound up having to re-do our DAL to handle the differences in objects... which feels like a miserable code smell to me. Can anyone provide a better solution to this quandary we face?

Richard B
  • 1,581
  • 1
  • 15
  • 31

2 Answers2

1

Could you change Person to be class Person<TAddress> where TAddress : Address? Then class PersonRequiredAdderss : Person<RequiredAddresss>.

cadrell0
  • 17,109
  • 5
  • 51
  • 69
  • I could, but in this instance "Person" is being used as a concrete class, as well as "PersonRequiredAddress"... let me see how that works. – Richard B Feb 03 '12 at 16:56
  • Yes.. Yes.. I think that'll work. Heh.. should have realized that one. thx @cadrell0 – Richard B Feb 03 '12 at 17:05
  • I'm leaving this as the answer, but the issue I run into is in the mapping of the objects. I can't seem to return a generic IList> since PersonRequiredAddress doesn't seem to want to fit in the IList – Richard B Feb 03 '12 at 18:21
  • To Clarify this... I am trying to get a repository to return me either 1) a Person or 2) a List> where TAddress could be either Address -OR- RequiredAddress. – Richard B Feb 03 '12 at 22:03
0

Take a look at dynamicly attaching dataannotations

Someone else had that question got answered here DataAnnotations dynamically attaching attributes

Community
  • 1
  • 1
rfcdejong
  • 2,219
  • 1
  • 25
  • 51