Building a data access layer
One of the really significant changes I’ve made to the web project I’ve been working on for the past six months has been seperating all of the data access code into a data access layer that the other parts of the application can only access through interface classes that I’ve called datasources. I’ve set it up so the datasource classes allow the application communicates with the data access layer using the program’s business objects which means the other parts of the program never need to know any specifics of how the underlying datasources work.
I don’t want it to be easy for the data access code to leak back into the other layers of the application. I want any functionality that requires the interfaces to be bypassed to be something that I (and any future developers) have to think pretty seriously about. The latest thing I’ve done to achieve this is restrict how much of the data access layer the rest of the application could actually by marking the non-interface classes that I didn’t want the rest of the application using internal.
Stopping the code from other layers getting into my data access layer was pretty easy. I put all the data access code in a different dll from the UI code and created a reference from the UI dll to the data access dll, but not the other way around. Now data access code that tries to use objects from the other layers of the application atually gives a compile error.
Building the data access layer been an interesting process. My ideas about the design of the data access are still solidifying. I’m still not completely happy with how the code within the layer actually works and I think the next thing I might look at is OR mapping. I think the work I’ve done on the interface between the layers is going to make the application a lot easier to maintain in the long term, I’m just really hoping the next developer on the project doesn’t absolutely hate me because of the way I’ve structured things. :)