Usage of Repository Pattern

design-patternsrepository-pattern

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:

  1. read in once (from flat-file)
  2. accessed randomly from the repository
  3. 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

Best Answer

The whole point of the repository pattern is to allow you to abstract away the implementation behind a generic interface. You can use whatever method works best for your situation, and if you want to change it later, it shouldn't affect the rest of your application.

Use whatever implementation works best (and is the easiest to understand and maintain).