Contexto Delimitado
Escrito por Darjus Kazakevic · revisado el
One model and one meaning per word, inside a boundary you draw on purpose.
A bounded context is a boundary in the software. Inside it, one model holds and every word has exactly one meaning. In an online shop, “Customer” means a buyer with a credit limit to Sales, a person with open tickets to Support, and a delivery address to Shipping.
Put all of that in one Customer class and it grows fields for every team until no team can change it safely. Bounded contexts give each part its own model and connect the parts through explicit contracts. In system design, they are the usual answer to “where do I split this into services?”. The cost is some duplicated data and translation code at each edge.
En esencia — A model stays clear only inside a boundary: draw it on purpose, keep one meaning per word inside it, and translate at the edges.
Cuándo recurrir a él
Recurre a él cuando
- One class is gaining fields and flags for several departments
- The same word means different things to different teams
- You are deciding how to split a system into modules or services
Reconsidéralo cuando
- A small app with one team and one clear vocabulary
- A generic subdomain you buy rather than model
- The domain is still too new to know where the seams are
What makes a context
- One model, one meaning
- Inside the context, each term has one definition and the rules do not contradict each other. You can reason about a change without asking other teams.
- A hard boundary
- An edge that code cannot cross by accident: a module, a package, a database schema or a service. If outside code can import the internal model, the boundary exists only on paper.
- One owning team
- One team owns the context, its words and its releases. One team may own several contexts, but two teams should never share one.
- Translation at the edge
- Contexts still need each other's data. They exchange it through a contract (an event, an API or a shared schema), and each side maps it into its own model.
Drawing the boundary
- Draw the line where a word changes meaning
- When people need a qualifier to be clear, like “the billing customer” or “the support customer”, you have found a boundary. Follow the language, not the database tables.
- Prefer copied data to a shared table
- Two contexts that each store the customer fields they need, kept in sync by events, can change on their own. Two contexts that share one table must always change together.
- Start with a few large contexts
- Early boundaries are often wrong and costly to move once data and contracts harden. Start with a few contexts as modules in one deployable. Make one a service when the language, the changes or the teams show a real seam.
- Make a crossed boundary fail the build
- A boundary that lives only in a diagram gets crossed by the next person in a hurry. Enforce it with module visibility, a lint rule or an architecture test.
Errores habituales
- Boundaries drawn along technical layers (“the API context”) instead of language
- A “common” module that every context depends on: the boundary nobody drew
- One database schema behind several contexts, which couples them where nobody looks
- Splitting before the domain is understood, then paying to move the lines