I'm going to introduce a few programming concepts here so bear with the long answer. This may seem overcomplicated butuntil you're used to it. But all of these patterns willare actually bevery common and very useful in commercial software.
public class DocumentParser : BaseParser
{
protected override List<Parser> GetParsers()
{
return new List<Parser>
{
ParseDateOfBirth,
ParseGender,
ParseRefNum
};
}
protectedprivate bool ParseDateOfBirth(string line, DocumentMetadata dto)
{
if (!line.StartsWith("DOB:")) return false;
dto.DateOfBirth = DateTime.Parse(line.Substring(4));
return true;///Implementation
}
protectedprivate bool ParseGender(string line, DocumentMetadata dto)
{
if (!line.StartsWith("Sex:")) return false;
var s = line.Substring(4,1);
if (s == "M")
{
dto.Gender = Gender.Male;
return true;
}
if (s == "F")
{
dto.Gender = Gender.Female;
return true;
}
///Implementation return false;
}
protected virtualprivate bool ParseRefNum(string line, DocumentMetadata dto)
{
if (!line.StartsWith("RefNum:")) return false;
dto.RefNum = line.Substring(7);
return true;///Implementation
}
}