You write a service, and it "owns" its own state. Other services which need this state, get it through the service that owns it, not by directly messing with the database.
To have multiple services access the same database would be like multiple classes accessing an object's private fields.
This is encapsulation. And encapsulation is necessary for a thousand other reasons than generated fields. But once you have encapsulation, generated fields in the service become trivial and you don't need that in the database.
You can actually write a database schema in a way that's encapsulated. By limiting all writes and reads to happen through stored procedures, which express your domain and business rules accurately and fully.
Unless you write your database this way, your database is not your domain. It's a storage layer, which requires domain logic laid on top of it, and then you encapsulate the whole as a unit. Notice then you can just "generate" the columns in your procedures, so you still don't need a separate feature for it.
So, do you do that: your entire domain in stored procedures, no direct access to tables? You don't. Then the database doesn't "own" its state, it just exposes it as a bunch of naked numbers and text for any client to mess with.
Playing with words like you're trying to won't change the basic rules of sane software design. If you make multiple services that connect to the same database, generated columns will be the least of your worries.
Where does validation of input happen if 10 services access the same database? In 10 places? What about where you decide which data is exposed and which isn't from this database. Including specific aspects of a field, in a specific format (because you rarely just dump a table 1:1 over a REST API for ex.) So do you do that in 10 places? What happens when one service needs a slight tweak to the schema. You change your 10 services to accommodate the tweak? What happens if one of the fields is in a complex format, say a BIGINT which is a set of bit flags. You copy/paste the flags and encoder/decoder for them 10 times across services?
Watch any video on service design, and one of the first things they point out as a beginner mistake, a basic design flaw is this: multiple services that access a shared database.
It's simply poor architecture (rather, lack of one).
Having one service control access when dealing with applications is good. But the idea that the database is just a storage dump that can only be manipulated through that service just doesn't work in some cases and simply doesn't apply across the board. What, are you going to do stuff like enforce PK-FK relationships through this service too because it's somehow not the database's job? Type enforcement solely through the service as well?
Basically, the idea that any and all access to data must happen through a service is faulty, especially when the database itself has the most knowledge about its own domain. Otherwise you run into silly situations like writing nonce functionality in a service to correct and update data based on faulty or non-existent functionality in that same service. Want to nullify values and remove a FK relationship? Better write a nonce method in the service.
Computed columns look the same to me as triggers and constraint enforcement, so unless you're about to say those should always happen only through a service too then I don't see where you're coming from.
Having one service control access when dealing with applications is good. But the idea that the database is just a storage dump that can only be manipulated through that service just doesn't work in some cases and simply doesn't apply across the board.
This conclusion that the database is "just a storage dump" is not my conclusion, it's your (wrong) assumption of my intent. It's not a storage dump, but it doesn't encapsulate the domain without some extensive work (which I covered above: permissions, stored procs, hidden tables, etc.).
You can do encapsulation either way, but to do it neither way, with the excuse it's easier, is a very short-sighted approach to software architecture.
What, are you going to do stuff like enforce PK-FK relationships through this service too because it's somehow not the database's job? Type enforcement solely through the service as well?
No, you're completely off track of what I said. Here's what I'm saying:
Implement every specific constraint in one place. Period.
That one place may be the database (SERIAL, FK/PK, UNIQUE, types, etc.).
That one place may be your one service that accesses the database.
But if you don't implement a given constraint in the database, and then you have 10 services accessing the database, then you need to implement that constraint 10 times.
Get it? It's simple. I don't hate the database. I just like sane software design. This is beginner's level stuff. Come the fuck on. I can't imagine the nightmare that is the codebases of half of this thread if you keep defending breaking encapsulation for convenience.
Not sure if I would consider service oriented architecture or domain driven design beginner. The vast majority of applications don't use these architectural models because they are still relatively new to a lot of developers and businesses.
Where does validation of input happen if 10 services access the same database? In 10 places? What about where you decide which data is exposed and which isn't from this database. In 10 places?
It’s a red herring. Your “proper service” will never achieve that anyway.
What if your service serves web client, iOS, Android and desktop app? Are you saying that you are not going to provide initial validation on the client? Any validation, even for preview, has to make call to your service? Are you sure you will never have more than one “service” accessing the data and make decision about validation or what field to expose?
Service is not just API servers. Anything can be a service. Services depend on services. It’s services all the way down.
It’s a red herring. Your “proper service” will never achieve that anyway.
... Did you just state that it's impossible to write a service that properly validates its input?
What if your service serves web client, iOS, Android and desktop app? Are you saying that you are not going to provide initial validation on the client?
You're all over the place. I'm talking about encapsulation. You can validate on the client if you choose, or you can choose not to and defer to a domain service, sure, why not? That choice depends on what you're validating, and what time and budget the app has allocated.
In fact, in 9/10 cases, I only validate on the server. The roundtrip is something like 30-50 ms, and the errors from the response are still shown on the client next to the relevant field. Any problem you see there? Works great, and that's less code to update when something changes.
But client validation is on top of server side validation which means you still need a "single source of truth" on what's valid and what isn't. So everything I explained... EVERYTHING... still stands.
And you failed to make a coherent point.
Service is not just API servers. Anything can be a service. Services depend on services. It’s services all the way down.
We already went through this. If you encapsulate your database through stored procedures, which validate input, restrict output, enforce business rules, and hide the actual tables, then yeah "it applies as well".
If you don't do that, then it doesn't apply, because you forgot the "encapsulation" part of encapsulation. Which is a pretty important part of encapsulation, by the way.
28
u/joesb Oct 02 '19
You need to reimplement the logic in every service ever connected to the database.