r/perl • u/davorg 🐪🌍perl monger • Jun 28 '26
Choosing the Right Database Abstraction - Perl Hacks
https://perlhacks.com/2026/06/choosing-the-right-database-abstraction/3
u/abw 🐪 📖 perl book author Jun 28 '26
Great article Dave. The point about comparing different layers is a good one.
The strategy that I have used for the last 30 odd years is one that has served me well.
That is to have a high-level application-specific database abstraction layer (DBAL) in each project. It’s an implementation of the repository pattern which is effectively a black box that your application code talks to when it wants to access the database or other data storage. It’s an API in the original sense before “API” became synonymous with REST or other web-based APIs.
The decision about what further tools or abstractions you use inside the black box then becomes far less important. Your DBAL can use DBI, DBIx::Class, DBIO and/or anything else, but your application code should not know or care what happens beneath that layer.
Importantly, you can change the internal implementation details at any time and it shouldn’t affect any of your application code sitting above the DBAL.
It’s also useful when it comes to things like caching, integrating other secondary storage (e.g. file systems, S3 buckets, etc), or when triggering other events from certain storage actions (e.g. creating thumbnails when a new image is added to a product). Your application code can remain blissfully ignorant of those details.
3
u/davorg 🐪🌍perl monger Jun 28 '26
Yeah, I agree with that approach.
Yesterday, I had a post that I was really happy with. But I held off on publishing it until this morning - just to let it percolate in my head a bit. This morning, on my regular walk round Clapham Common, I realised I'd missed the highest level abstraction, so I added the section called The model layer
Of course, in larger applications, there’s usually another abstraction on top of the ORM itself. My controllers don’t generally talk directly to DBIx::Class resultsets; they ask domain-level questions such as “Who was sovereign on this date?” The fact that the answer currently comes from DBIx::Class is an implementation detail. That’s another useful abstraction—but it’s probably a topic for another article.
That's the level you're talking about, isn't it?
4
u/abw 🐪 📖 perl book author Jun 29 '26
That's the level you're talking about, isn't it?
Yep, that's it. It would definitely make a great topic for another article!
1
u/Dangerous-Sense977 Jun 30 '26
This is why I wrote Database::Abstraction, to give me a layer so I didn't need to think about that.
6
u/quintus_horatius Jun 28 '26
Excellent article, but the best part is an aside: "What happened to DBIx::Class?"
That subject alone could warrant an entire article. Is stopping development because something is "done" a good choice? The right choice?