I have the following problem:
I have a class called MedicalInfo
and I would like its list to have at least one Prescription
. The thing is I thought of fitting in some logic in its constructor, as you can see below, but it has a major con: when I deserialize it (xml) it adds a new one.
I imagine that the Serializer first creates the objects (and therefor adds ALWAYS one instance of Prescription
) and afterwards adds the values of the fields. So, in a nutshell, everytime I de-serialize I end up with one extra instance... How can I avoid this?
Just to put some context, I'm developing a WinForms application that serializes on the app's close and deserializes on its open, in order to have the data avaliable during its use. Furthermore, MedicalInfo
is part of a class called Client
that has many properties, one for instance is a List of Policies. What I serialize and Deserialize is a list of clients (an addressbook). I thought of doing the "check" after deserialization... but that would, in some cases, need a double foreach. I'm not sure if that would be optimal.
public class MedicalInfo
{
public string MedicareNumber { get; set; }
public DateTime PartAEffectiveDate { get; set; }
public DateTime PartBEffectiveDate { get; set; }
public List<Prescription> Prescriptions { get; set; }
public MedicalInfo()
{
PartAEffectiveDate = new DateTime(1900, 01, 01);
PartBEffectiveDate = new DateTime(1900, 01, 01);
Prescriptions = new List<Prescription>().Min(1);
if (Prescriptions.Count == 0)
{
Prescriptions.Add(new Prescription());
Prescriptions.FirstOrDefault().Instructions = "Default Prescription";
}
}
}
public class Client
{
#region Properties
private Guid ID { get; set; }
[XmlIgnore]
public string FullName
{
get
{
return FirstName + " " + MiddleName + " " + LastName;
}
}
[XmlIgnore]
public string DisplayName
{
get
{
return LastName + ", " + FirstName;
}
}
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public bool Male { get; set; }
public DateTime Birthday { get; set; }
public Address Address { get; set; }
public int SSN { get; set; }
public long Phone { get; set; }
public long AlternatePhone { get; set; }
public string Email { get; set; }
public int Height { get; set; }
public int Weight { get; set; }
public string Notes { get; set; }
public BankAccount BankInfo { get; set; }
public MedicalInfo MedInfo { get; set; }
public DateTime RegisteredTime { get; set; }
#endregion Properties
public Client()
{
ID = new Guid();
RegisteredTime = DateTime.Now;
Birthday = new DateTime(1900, 01, 01);
BankInfo = new BankAccount();
MedInfo = new MedicalInfo();
Address = new Address();
}
}
public class InsuranceClient : Client
{
public bool Tobacco { get; set; }
public PolicyCollection Policies { get; set; }
public InsuranceClient() : base()
{
Policies = new PolicyCollection();
if (Policies.Count == 0)
{
Policies.Add(new Policy());
Policies.FirstOrDefault().Plan = "Default Policy";
}
}
}