I recently had a conversation with a coworker about where the logic belonged in a piece of code. I felt that his dao* (data access object) layer had too much logic. I suggested that the objects that were communicating with the database should only have the queries needed to get the data from the database, and any business logic should be in a different layer (likely a service layer). My colleague said that he had done that before and he thought that made the dao layer too thin.
I let the comment slide, and let the code slide (though I am regretting that decision). But, that got me to wondering if there is such a thing as a layer being “too thin”.
This, by the way, is probably too thin.
The only example I can think of where I might say that a layer was too thin was if it were just a wrapper around another object and it wasn’t really serving any purpose, it just called through to the object it wraps. However, if the wrapper was serving a purpose (like wrapping a utility object that was all static methods so we can mock it, or wrapping a third party library that we want to encapsulate to make it easy to change out), then I wouldn’t have an issue.
In my mind, I try to think of each layer as a different level of abstraction. Our controllers have the highest level of abstraction. Reading the code there helps you understand the high level tasks we need to do (like, get some data, set some values and return some data).
The next level down is the service layer. This contains business logic It would contain the code that sets values, or does some calculations that we need to return to the user through the controller. This layer is more detailed, but will often use other classes to assist.
The last level is the dao layer. This is where we are getting our data, either from a database or file, or magic 8-ball. This has nitty-gritty details of how to get the data, but contains NO logic for converting the data in any way. It can contain queries that allow us to bring back only the data we are interested in. Those queries can do things for us like sorting our data, etc. But, the dao layer is simply in charge of executing the queries and returning the data to the service layer.
Do you have any examples where you have seen a layer and thought it was too thin?