I'm working on a legacy code and I have seen a lot of code like this:
public class Person
{
public Person(PersonData data)
{
this.Name = data.Name;
this.Gender = data.Gender ;
}
public String Name { get; private set;}
public String Gender { get; private set;}
}
public class PersonData
{
public String Name;
public String Gender;
}
public static Person ReadPerson(Reader reader)
{
PersonData data = new PersonData;
data.Name = reader.ReadString();
data.Gender = reader.ReadString();
Person p = new Person(data);
return p;
}
The PersonData class exists for setting the private fields in Person class in its constructor. Other than that, the PersonData class introduces redundant code, as you can see now you have Name and Sex in both Person and PersonData class.
In my opion, this kind of design doesn't scale: now I have a new field "Age" to read, I have to add the "Age" property in two different places.
Is this a valid design choice (given I have see a lot code like this in the legacy code)?
How can I refactor this?
EDIT:
Those two classes are simplified version of the real code. So please forgive using string instead of enum for gender.
In the real code the PersonData have more than 10 fields so as Person class.
Sexis a boolean value sometimes followed with "please?". TryGender(or some variation of) which is usually constrained based upon domain (Gender and Preference/Association may be further separated) as to what values are accepted: a simpleenummight includeMale, Female, Other(but could be more/different and in a simplified form only considers the first two options).