I've noticed a trend where many APIs mirror their database schemas one-to-one. This approach might work initially, but it can lead to significant issues like client breaks with every schema update, leaking internal details, and painful refactors. I believe the API should be designed independently, focusing on client needs rather than being a direct reflection of the database. I'm curious to know how others manage this trade-off. Do you emphasize the database, the API, or do you maintain a separation between them?
5 Answers
I used to think a direct API-DB mirror was the best way, but after a messy experience, I've learned otherwise. Keeping them separate really streamlines the process and avoids a lot of headaches later on!
Absolutely keep them separate! It's all about using data transfer objects (DTOs). This way, you can keep your API clean and avoid the painful repercussions of tying it directly to the database schema.
Right? Seems like enterprise design patterns are fading away these days.
I thought everyone was using DTOs. It's surprising they're not mentioned more often!
I think you need to start with the API. Make that the contract, then you can work on optimizing your DB for storage and query efficiency separately. This keeps concerns nicely separated.
Exactly! Starting with the API allows for better flexibility, especially for UI flow.
Totally agree, and it means developers on both ends can work more independently.
For my projects, I develop in layers: the API handles the client needs, while the DB is optimized for internal purposes. It makes for a more maintainable setup overall!
Layered systems are the way to go. It also helps future-proof your architecture.
Exactly! A change in the DB shouldn't require changing the whole API.
But what if you need to ensure your data integrity? The API needs to reflect your persistent data model to some extent, right?
Not necessarily! A well-structured API can handle different data shapes and logic without being tied to the DB directly.

Can't believe some of these guys still think it should mirror. This just leads to frazzled developers down the line!