Interchangeable data access layers == stealing from your client
Posted at 07:00 on 24 July 2014
This is one of those so-called “best practices” that crops up a lot in the .net world:
You need to keep the different layers of your application loosely coupled with a clean separation of concerns so that you can swap out your data access layer for a different technology if you need to.
It all sounds right, doesn’t it? Separation of concerns, loose coupling…very clean, SOLID, and Uncle Bob-compliant, right?
Just. A. Minute.
The separation of concerns you are proposing is high-maintenance, high-friction, usually unnecessary, obstructive to important performance optimisations and other requirements, and, as this post by Oren Eini aka Ayende Rahien points out, usually doesn't work anyway.
In what universe is it a best practice to allocate development time and resources, for which your client is paying, towards implementing a high-maintenance, high-friction, broken, unnecessary, non-functional requirement that they are not asking for, at the expense of business value that they are?
In the universe where I live, that is called "stealing from your client."
Nobody is saying here that separation of concerns is bad per se. What is bad, however, is inappropriate separation of concerns -- an attempt to decouple parts of your system that don't lend themselves to being decoupled. Kent Beck has a pretty good guideline as to when separation of concerns is appropriate and when it isn't: you should be dealing with two parts of your system which you can reason about independently.
You can not reason about your business layer, your presentation layer, and your data access layer independently. User stories that require related changes right across all your layers are very, very common.
Every project that I've ever seen that has attempted this kind of abstraction has been riddled with severe SELECT n+1
problems that could not be resolved without breaking encapsulation.
(Nitpickers' corner: I'm not talking about test mocks here. That's different. It's relatively easy to make your test mocks behave like Entity Framework. It's orders of magnitude harder to make NHibernate or RavenDB behave like Entity Framework.)
If you can present a valid business case for making your persistence mechanism interchangeable, then it's a different matter, of course. But in that case, you need to implement both (or all) the different options up-front right from the start, and to bear in mind that the necessary separation of concerns almost certainly won't cleanly follow the boundary between your business layer and your DAL. You should also warn your client of the extra costs involved, otherwise you won't be delivering good value for money.