I’m working through a CSV importing approach that I want to teach to other people I work with where I’m concerned with the following:
- Validating each record before attempting to process (e.g. persist).
- Failure on any record prevents processing.
- Providing status during validation and processing.
What I’m looking for is thoughts on if the following classes and usage example appear clear enough to meet the intent described.
There are three classes.
Importer: manages the overall algorithm, keeps track of validation errors, provides the algorithm to convert each CSV line to data construct that can be validated or processed.
Importer(CSVFile)
getProcessor(): Processor
getValidator(): Validator
getErrors(): array
isOKToProcess(): boolean
lineToData(String data): Object
Validator: validates entire file and each row to ensure it’s OK to process
close(): void
currentRow(): int
hasNext(): boolean
doNext(): void
Processor: persists each row
close(): void
currentRow(): int
hasNext(): boolean
doNext(): void
As you can see the last two roughly adhere to a Java style iterator. The client interaction is thus (psuedocode – assume output is flushed immediately to the buffer):
importer = new Importer("/path/to/file.csv");
validator = importer.getValidator();
writeOutput("validating<br/>");
while (validator.hasNext()) {
validator.doNext();
writeOutput(validator.currentRow() & "<br/>");
}
validator.close();
if (!importer.isOKToProcess()) {
writeOutput("errors<br/>");
writeOutput(importer.getErrors());
return; // short circuit
}
processor = importer.getProcessor();
writeOutput("processing<br/>");
while(processor.hasNext()) {
processor.doNext();
writeOutput(processor.currentRow() & "<br/>");
}
processor.close();
Some specific questions, but please feel free to critique otherwise:
- Make sense to use two separate classes (in practice probably inner) for validating and processing? Or perhaps move everything into one Importer class?
getErrors()in Validator instead, because that’s where the errors are raised?currentRow()on Importer instead of Validator and Processor because it’s really the current row of the overall import?
From an OO perspective I think it is right to keep the classes seperate. I do not think it is even necessary to make the
Validatoran inner class. Think of it from this angle. If you implement this generically (maybe so that one day you can pass in a grammer or some kind of specification which tells theValidatorhow to validate), you can use it to validate multiple types ofCSVfiles. In that case, you would not want it contained in an outerImporterclass. Otherwise you would have to write anImporter/Validatorcombination each time.An
Importeris an importer, aValidatoris a validator.I would definitely move the
getErrors()method into theValidator. Otherwise its like someone else taking credit for the work you have done. Since theValidatoris doing the validation, let it tell the world of the errors it found. Don’t give the glory to theImporter, it has not done anything yet.Your third point does not make quite sense. Both the
Validatorand theImporterhas thecurrentRow()method. This seems correct. They are functianally different and both need to keep track of progress. One change you might be able to make is to give theValidatorhave agetTotalNumRows()method. This way, once the file is validated, theImportercan ask theValidatorhow many rows it read during validation. This will enable theImporterto display progress better, ie. as a percentage rather than only showing on which line it is.Other than that, I think your design is good.