How Onion Architecture solves the database and business rule problem

View profile for Marcus Dantas

Software Engineer | Full Stack Developer | Node.js | React | Next | AWS

Ever tried to change the database of an application and ended up having to change even your business rules? That's exactly the pain Onion Architecture was designed to fix. Onion Architecture is all about putting your business logic at the core, surrounded by layers that depend inward, never outward. Each outer layer can depend on the one inside it, but the core is completely independent. ✅ The layers typically look like this - Core (Domain): Entities, value objects, and business rules - Application: Use cases and services that orchestrate domain logic - Infrastructure: Databases, frameworks, external APIs, UI, etc. The golden rule: the core never knows about the outside world. Databases, frameworks, and APIs depend on the domain, not the other way around. ✅ Why it matters: - Keeps business logic framework-agnostic - Makes testing simpler and faster - Encourages clean, maintainable architecture - Allows easy swapping of infrastructure (e.g., from REST to GraphQL, SQL to NoSQL) ✅ Use Onion Architecture when: - You want long-term maintainability and flexibility - Your app has complex business rules or multiple data sources - You're tired of framework lock-in and messy dependencies The takeaway: Protect your core. Everything else is just an outer layer.

  • diagram
Carla Coelho

Test Analyst | QA | B2 English

3w

Very Good! The explanation hits the main point: the Onion Architecture solves the headache of having to change the business rules because of a database change.

Good 👏

Lucas Romão

AI Engineer @ ExACTa PUC-Rio | PhD Candidate @ PUC-Rio | ML Engineering Researcher | Data Science| Machine Learning (ML) | Deep Learning | Artificial Intelligence (AI) | Gen AI | LLM | NLP

3w

Great explanation — you captured the essence of Onion Architecture perfectly: protecting the domain from external chaos. I like how you framed it around the real-world pain point of database changes leaking into business logic — that’s exactly what the architecture was meant to prevent. The clear breakdown of the layers and the “depends inward” principle makes the concept immediately accessible, even to those who’ve only seen it mentioned in passing. The reminder that frameworks are details is also key; it’s a mindset shift that helps developers think in terms of business value rather than tools. When you’ve applied Onion Architecture in real projects, what’s been the hardest part for teams to internalize — resisting the temptation to leak infrastructure details into the core, or structuring dependencies cleanly enough that testing remains frictionless?

See more comments

To view or add a comment, sign in

Explore content categories