there is one change that should be implemented and that's disposing of the stream in _getFileData(). if any exception is thrown before you are finished reading the stream will not close. that can be cleaned up easily enough
Code:
List<string> result = new List<string>();
using(StreamReader reader = new StreamReader(this.FileName))
{
while (reader.Peek() >= 0)
{
result.Add(reader.ReadLine());
}
}
return result;
after that it's just preferences.
I try never to expose mutable lists publicly. therefore i would return an IEnumerable<string> rather than IList<string>.
If you are going to set the file name as a property then you should create an explicit exception to throw if the filename is not set. This error will be more meaningful when it's logged.
Code:
if (string.IsNullOrEmpty(FileName))
{
throw new ImportFileNotConfiguredExecption();
}
//return list of strings from the file
Code:
class ImportFileNotConfiguredExecption : Exception
{
public ImportFileNotConfiguredExecption()
:base("No file specified to import. Set the FileName property before calling GetFileData");
}
I would also convert GetFileData to a method. that's strictly a coding preference.
other stylistic recommendations.
* The class has 2 responsiblities 1. access the file 2. return rows. consider separating them into 2 distinct classes. abstracting IO calls (file, socket, database, etc.) makes testing much easier.
* your test requires the artifact [tt]c:\\List_Test.txt[/tt]. I would create the file before the test and delete the file after the test. It's considered bad practice not to clean up after each test. I would also keep it within the executable directory rather than the root of C:
* rename the class to reveal the intent of what it does. Technically this class does not import data. It reads lines from a file. a name like FileLineReader or something would be more appropriate.
* You can pass in any text as a filename. the file may not exist, or it may not be a text file. in which case your code fails. by having the class use a TextReader you ensure the reader can process lines of text.
with the last point about checking the file and if it exists. if you keep this all in one place the class begins to build up too much responsibility for a single object. this also makes it more difficult to test because there are more scenarios you must account for to ensure you code works properly. by moving the creation of the text reader to another object reading lines from a file becomes a very simple test. and the object which creates the filestream is also easier to test because all you need to validate is 1. the filename is not null 2. the file exists and 3.a FileStream is returned.
Jason Meckley
Programmer
Specialty Bakers, Inc.
faq855-7190
faq732-7259