public IEnumerable<Person> ImportPersonsFromCsvFile(string fileName) { if (fileName == null) throw new ArgumentNullException(nameof(fileName)); if (!File.Exists(fileName)) throw new FileNotFoundException(nameof(fileName)); var lines = File.ReadAllLines(fileName, Encoding.Default); if (lines.Length == 0) throw new FormatException("File contains no lines."); for (int i = 0; i < lines.Length; i++) { var personBuilder = new PersonBuilder(); var currentLine = lines[i]; ProcessLineAndSetBuildersProperties(currentLine, CsvSeparator, personBuilder); yield return personBuilder.Build(); } }
private void ParseFieldsAndSetBuildersProperties(string[] personFields, PersonBuilder builder) { if (personFields == null) throw new ArgumentNullException(nameof(personFields)); if (personFields.Length != FieldCount) throw new FormatException($"Incorret field count {personFields.Length}. Expected {FieldCount}."); var personDocument = personFields[DocumentPosition]; if (personDocument == string.Empty) throw new FormatException("Document can't be empty."); // Simple barcode readers can't handle non-digit characters if (personDocument.Any(c => !char.IsDigit(c))) throw new FormatException("Document can't contain non-digit characters."); var personName = personFields[NamePosition]; if (personName == string.Empty) throw new FormatException("Name can't be empty."); var personRole = (PersonRole)Enum.Parse(typeof(PersonRole), personFields[RolePosition]); builder.Id = new GenericDocument(personDocument); builder.Name = personName; builder.Role = personRole; }
private void ProcessLineAndSetBuildersProperties(string line, char separator, PersonBuilder builder) { var separatedFields = SeparateFields(line, separator); ParseFieldsAndSetBuildersProperties(separatedFields, builder); }
public PersonBuilderControl() { InitializeComponent(); builder = new PersonBuilder(); }