I am working on 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 to set private fields in the Person class in its constructor. In addition, the PersonData class introduces redundant code, because now you can see the name and gender in the Person and PersonData classes.
In my defense, this type of design does not scale: now I have a new Age field for reading, I have to add the Age property in two different places.
Is this the right design choice (given that I have a lot of code like this in legacy code)?
How can I reorganize this?
EDIT:
These two classes are a simplified version of real code. Therefore, please forgive using a string instead of an enumeration for gender.
In real code, PersonData has more than 10 fields, so the class is Person.
source share