I'm using the Repository pattern in my application right now. It is a console-based batch processing tool. Essentially I need the repository to be able to randomly access the data. The trouble I am having is that my data needs to be:
- read in once (from flat-file)
- accessed randomly from the repository
- written out once.
I handle this in the code by using a guard clause to see if the records have been accessed yet, and if not then read them in. A simple example below.
public IUnit GetById(int id)
{
if (!ReadIn)
{
ReadAllUnitsFromFile();
}
return _listOfUnits[id];
}
My question is this: Would it be a bastardization of the Repository pattern if I had a module that ran before the rest of my program to read the data into the repository, and another module that ran afterward to write data out of the repository? Is this an anti-pattern? Is there a better pattern to suit my situation?
Thanks