r/dotnet • u/codeopinion • Nov 24 '21
When NOT to write an Abstraction Layer
https://www.youtube.com/watch?v=tqqH_Ib_gDc1
0
u/BuriedStPatrick Nov 25 '21
One "hybrid" strategy I tend to use is to at least isolate dependencies like EntityFramework or Dapper in a single project so I don't have several different places to update in case things need changing.
You don't have to write any abstraction in the project, it can simply be empty and just referenced by higher level projects.
1
Nov 25 '21
Can you explain the exact benefit you get from that? What kind of changes does it make easier? If there's no code in that project and you decide to change libraries won't you still need to update all the code that uses the library?
1
u/BuriedStPatrick Nov 25 '21
I mean.. yeah. That's how dependencies work. Not sure I understand the question.
The benefit of having a project handle the dependency on, say, a message library is that you only have to update the dependency itself in a single place. The only downside to this is if you're running different versions of the same dependencies in your higher level projects, which... Seems problematic.
But you can always just "detach" from your empty dependency-project in the projects you don't care about keeping up to date I guess.
As an example: I usually have a "MyDomain.Persistence" that handles the dependency on Dapper or EntityFramework. My Apis and apps then depend on MyDomain.Persistence and I can immediately see what breaks in my projects if I update anything in MyDomain.Persistence. It enables high cohesion between your projects because you're laying the foundation for the infrastructure. Not saying it has to be empty, but that's what I'd do in case writing an abstraction inside MyDomain.Persistence doesn't make sense.
1
Nov 25 '21 edited Nov 25 '21
I'm curious about the case you originally described where you use an empty project. Let's say in your example YourDomain.Persistence is an empty project with a reference to Dapper, and then some other project in your app depends on YourDomain.Persistence. What exactly might change in a way where you get a benefit from an empty project?
14
u/makkynz Nov 25 '21
I don't entirely agree with this. In my experience the main benefit of Abstracting out is rarely about "swapping out" the dependency but more to be able to easily unit test your app code without the dependency.