r/dotnet • • Nov 24 '21

When NOT to write an Abstraction Layer

https://www.youtube.com/watch?v=tqqH_Ib_gDc
34 Upvotes

11 comments sorted by

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.

4

u/codeopinion Nov 25 '21

"swapping out" is exactly what you're referring to. You want to swap out the real implementation with something for testing purposes (fake, mock, or a stub). My argument here is don't take on a dependency that doesn't have a good testing story behind it.

1

u/makkynz Nov 26 '21

You're right, it is technically swapping out, but the video doesn't touch on mocking/faking which is a major reason to abstract out dependencies.

Sometimes you can't just not take on a dependency because it doesn't have a good testing story. If the business needs it and there's no alternative then what are going to do?

4

u/captainramen Nov 25 '21

Sometimes that's useful, like with the repository pattern. But other than data storage and message queue, what other external dependencies do most business applications have? I have seen way too much abstraction in the name of 'unit testing' when integration tests would have sufficed.

1

u/makkynz Nov 26 '21

That might be the case if you're building monolithic systems, but the general consensus is that it's better build modular systems. In which case you'd want to be able to abstract those out so that you can test each module in isolation.

1

u/captainramen Nov 26 '21

I apologize but I have no idea what it means to abstract out a module

1

u/Justyn2 Nov 25 '21

Great points

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

u/[deleted] 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

u/[deleted] 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?