In my current system I've got two EF objects with near-identical sets of properties but which can't for various reasons, be unified or joined to share some of the same structure. It's effectively personal details, so it looks a bit like this:
public partial class Person
{
public int PersonID { get; set; }
public string Title { get; set; }
public string Surname { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
// .. etc
}
public partial class Member
{
public int MemberID { get; set; }
public string Title { get; set; }
public string Surname { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
// .. etc
}
Eventually a mixture of these two object types have to be written to the same file, like this:
foreach (Person p in People)
{
StringBuilder lineBuilder = new StringBuilder();
lineBuilder.Append("\"");
lineBuilder.Append(p.PersonID.ToString());
lineBuilder.Append("\",\"");
lineBuilder.Append(p.Title);
lineBuilder.Append("\",\"");
lineBuilder.Append(p.Forename);
lineBuilder.Append("\",\"");
// etc...
}
foreach (Member m in Members)
{
StringBuilder lineBuilder = new StringBuilder();
lineBuilder.Append("\"");
lineBuilder.Append(m.MemberID.ToString());
lineBuilder.Append("\",\"");
lineBuilder.Append(m.Title);
lineBuilder.Append("\",\"");
lineBuilder.Append(m.Forename);
lineBuilder.Append("\",\"");
// etc...
}
This is obviously A Bad Thing - not only does it repeat code, but it's reliant on both loops maintaining the same number of columns in the output which might easily be overlooked if the code is changed.
Short of creating an intermediary object and mapping the fields on to it - which is really just shifting the problem elsewhere - is there a way to approach this that allows me to get this down to a single loop?