Search code examples
c#.netdata-access-layer

What details should be hidden in the Data Access Layer?


I'm creating a Data Access Layer (DAL) to read/modify some tables stored in an XML file. While my application is running, the rows of these tables are updated very often, therefore I thinking about two possible alternatives:

  1. When the application starts, I could load all the data in tables through the DAL and put them in a data structure such as a Dictionary. While the application is running, this dictionary is continuously updated. When the application is closing, I invoke a method of the DAL in order to overwrite the old file with the new data in the dictionary.
  2. It would probably be more correct to hide this dictionary to the upper layers, so I might have a Dictionary private field in the DAL. In this way, the upper layers would call the methods of the DAL to update the rows of the tables.

Is perhaps the second approach better than the first one?


Solution

  • Option 2 here is better for encapsulation though does increase complexity. The dictionary is an implementation detail that your upper levels shouldn't be concerned with. Instead your data access layer should expose classes that represent the actual tables - for example you may have a Person class or an Inventory class. As DAL classes these would provide methods for adding, deleting, updating, retrieving etc. as appropriate.

    Further up you'd have a domain layer which operates on a row retrieved from your DAL classes that would add business methods (e.g. an Order could be made by a Person and impacts Inventory - the act of invoking methods on these domain classes goes down to the DAL to actually touch the underlying data).