9

I recently stumbled upon this opinion from 2016 saying that there is still case for domain logic in database.

I thought that this is totally obsolete. I am just wondering if the guy is still living in 90ties or this is can be really true. Set aside legacy systems.

What about having domain logic in database because of security requirements. Is that really a thing to do ?

ps-aux
  • 301
  • 3
    Did you actually read that article in full? I think the author makes his point clear very well, and explains for which parts of the BL he thinks is best placed in a database, and for which parts it is not. So which points from that article exactly you are struggeling with? – Doc Brown Nov 19 '16 at 11:13
  • Yes, it closed on February 18th 2009 and is now false. – Michael Durrant Nov 20 '16 at 02:22
  • It's curious. I read this guy, giving a lot of SQL examples (strongly tied to Oracle) and then I can't avoid to remember my customer telling me: I forgot to buy Oracle's licence for the PRE env. We have it for PRO but not for PRE. You will have to deploy the app, against MySQL in PRE and Oracle in PRO, for a little while... I will ask to that guy where all the shinny features of oracle goes now... (Problem here was that for a reason I don't want to know, the architect decided to go native Sql - row mapping way instead of Orm, but still close to the problem of being locked to DB provider). – Laiv Nov 20 '16 at 09:33
  • @Laiv: That is insane, and no amount of ORM or other abstractions will save you. You are basically deploying untested code to production with such a setup. – JacquesB Nov 20 '16 at 12:55
  • @Laiv The insanity could be mitigated slightly by using PostgreSQL instead of MySQL, of course. Much greater compatibility. – David Aldridge Nov 20 '16 at 13:55
  • 2
    "Is the age of domain logic in databases over?" God, I hope so. –  Nov 21 '16 at 15:00

3 Answers3

13

Programmers nowadays seem to think very dogmatically, especially when they read the thoughts that other people write in their blogs.

Take Bob Martin's Clean Coding blog, for example. As a general observation, I find Bob Martin's writings quite clear and lucid, so it baffles me that people are constantly getting confused by the things he writes, such as the SOLID principles. They get hung up on what a "Single Responsibility" should be, or why some class violates Liskov's principles, when what they probably should be doing is simply to strive to write better code, and obtain some experience first, so that what they read in blogs has some context.


Basically what you're saying is that the database should contain tables and data, and that is all it should contain. But databases are uniquely suited to doing certain things that... well, databases are good at.

The article cites these things:

  • Data Integrity and Validation (e.g. null and unique constraints)
  • Row-Level Security
  • Writing an API Using Stored Procedures
  • Calculating balances
  • Asking the database questions (i.e. querying and reporting)
  • Avoiding ORM problems such as N+1

as suitable things for putting into the database. I happen to agree with him.

Reasons you don't put business logic (in general) into a database:

  • Vendor lock-in
  • Your database isn't the central authority
  • Your team does not think relationally
  • Inferior tooling.

But these things generally only apply to those techniques, tools and training for which the database is not uniquely suited.

So, as with any other technique in software development, it depends. You evaluate your alternatives, and make your decision based on what you believe is the best possible course of action for your specific application.

Robert Harvey
  • 199,517
9

enter image description here

The age of horse and buggy is over, yet you can still buy buggy whips.

Why? When cars are faster, cheaper to maintain, and neglecting them won't produce visits from the humane society, why is the horse and cart still around?

Because sometimes you have different reasons to do something besides the popular reasons.

What you should be learning is why domain logic in a database causes problems and what anyone could possibly get out of it. Then make up your own mind.

My personal view:

Domain logic is about behavior. Databases are about persistence, relationships, and, well, data. When you see it this way business rules shouldn't be in the database.

On the other hand who, said the database couldn't have behavior? I've built office databases using Filemaker. People call it a database but it's really a whole application development environment as well. Everything seamlessly integrated into one, and called a database.

Wizdom is usually found between extreme views. I've no doubt either could be made to work. When trying to find the middle it's tempting to just follow the herd. I will caution against it here.

A system that keeps domain logic in the database can work well. A system that keeps domain logic out of the database can work well. A system that mixes domain logic in both places is going to drive me batty. I won't know where to put new behavior. I won't be sure where to find old behavior.

If you still can't decide flip a coin and take its decision as gospel for any particular project. As far as I can tell that coin knows what's best as well as anyone else.

candied_orange
  • 108,538
  • 1
    Your answer sounds you did not even took a look into the article linked by the OP (not that I saying the author is right or wrong), but can you tell us where you agree or differ to the views described in that article? – Doc Brown Nov 19 '16 at 11:16
  • @DocBrown I did not read it either, but this answer is a good one anyway where I'd fully agree. And it addresses the OP's question (last sentence), not a cited article. –  Nov 19 '16 at 12:28
  • @DocBrown I don't think the article read the Uncle Bob article that it cites: "Plugin architectures are very robust because stable high value business rules can be kept from depending upon volatile low value modules such as user interfaces and databases.". Uncle Bob has stronger views against this idea than I do. This article cherry picks from Bob's article and makes it seem like Bob is saying something he isn't. – candied_orange Nov 19 '16 at 13:40
2

I had a case where solving it in the business layer would have been a real performance killer.

Our application OO security concept consists of roles AND groups. And both are recursive structures. We wrote a stored procedure that resolves the permission for a user on a domain object.

There are really less needs to fall back to database logic. But in this case I decided to go that way. But what you always have to consider: You give up abstraction. As soon as you have business logic in the database you have a hard day to change your persistence layer. So be very very careful.

oopexpert
  • 769
  • 4
  • 7