VSA + DDD
VSA + DDD
VSA and DDD compose well: VSA tells you where the code lives, DDD tells you what the code is. Most healthy combinations: one slice approximately equals one bounded context.
Mapping slice to bounded context
The clean case:
src/
order/ <- bounded context "Order"
domain/
order.py # Aggregate
events.py
application/
place_order.py
...
customer/ <- bounded context "Customer"
...
One slice per BC. Each slice's domain/ is one BC's tactical model. Slices communicate by domain events or IDs — same as bounded contexts.
When one BC has multiple slices
A large bounded context can split into sub-slices for ergonomic reasons (different teams, very different feature sets within the same model):
src/
order/ <- one bounded context "Order"
placing/ <- slice
domain/ # Order aggregate
application/
place_order.py
fulfilment/ <- slice
application/
ship_order.py
This works only if both slices share the same ubiquitous language and respect the same aggregate boundaries. Otherwise it's two pretending to be one.
How DDD building blocks map into a slice
| DDD block | Slice location |
|---|---|
| Aggregate root | domain/ |
| Value object | domain/ (or shared/ if truly cross-context) |
| Repository protocol | domain/ |
| Repository impl | infrastructure/ |
| Domain service | domain/ |
| Domain event | domain/events.py |
| Application service (use case) | application/use_cases/ |
| Integration event publisher | infrastructure/ |
Slice boundaries vs bounded context boundaries
When they align, life is easy. When they don't:
- Slice spans two BCs. Reorganise: split along context lines.
- One BC across two slices. Either accept the split (large BC with sub-features), or merge into one slice.
- Sharing aggregates across slices/contexts. Don't. Reference by ID, talk via events.
Two failure modes
- VSA without DDD. Slices are folders with no model discipline. Each slice has its own anaemic
OrderServiceoperating on raw dicts. VSA's structure without DDD's substance. - DDD without VSA. All aggregates in
domain/, all use cases inapplication/. Domain is well-modelled but feature ownership is unclear; adding a feature requires hunting across layers.
Either alone is fine. Together is usually what you want for non-trivial domains.
Practical advice
- Start with one slice. Get the layering right inside it. Then duplicate the structure for the next slice.
- Treat cross-slice events as a public contract — version them, document them, don't break them casually.
- Be strict about shared kernel. The temptation to dump anything reusable there is constant; resist.
- Run a slice dependency check in CI (
import-linter). It catches accidental coupling within minutes of introduction.