public ParticipationRecord GetOrCreateParticipationRecord(string experimentName,
Func<string> chooseAnOptionForUser,
string userId)
{
var xml = Load();
var experiment = xml.Root.Elements("Experiment").Single(x => x.Attribute("Name").Value == experimentName);
if (experiment.Element("Participants") == null)
experiment.Add(new XElement("Participants"));
var participants = experiment.Element("Participants");
var existingRecord = participants.Elements("Participant").SingleOrDefault(x => x.Attribute("Id").Value == userId);
if (existingRecord != null)
return new ParticipationRecord(
existingRecord.Attribute("Id").Value,
existingRecord.Value,
existingRecord.Attribute("HasConverted") == null
? false
: bool.Parse(existingRecord.Attribute("HasConverted").Value),
existingRecord.Attribute("DateConverted") == null
? (DateTime?) null
: DateTime.Parse(existingRecord.Attribute("DateConverted").Value)
);
var assignedOption = chooseAnOptionForUser();
experiment.Element("Participants").Add(new XElement("Participant",
new XAttribute("Id", userId),
new XCData(assignedOption)));
Save(xml);
return new ParticipationRecord(userId, assignedOption, false, null);
}