r/nextjs • • 8d ago

Domain driven design

Hi all!

I'm building an app with next.js and payload.

This application is growing and I would go for DDD.

Any suggestions on project structure?

I'm eager to read any wise advice.

2 Upvotes

22 comments sorted by

4

u/Old-Register-4255 8d ago

Since you asked for a folder example, here's a rough one: src/modules/<domain>/ with its own service.ts (business logic), schema.ts (validation), queries.ts (db access) and types.ts, and for Payload put that domain's collection config and hooks in the same folder. Then src/app only holds thin routes and pages that call the module's service, and src/shared has what every domain uses, like auth, the db client and utils. One rule that keeps it honest: a module can import from shared, but not reach into another module's internals, only through its exported functions. And don't add layers like repositories and entities until a domain is actually complicated enough to need them.

1

u/boer0327 2d ago

that module boundary rule holds up really well in production. we enforce it with an eslint no-restricted-imports rule so nobody can accidentally import another domain's internal queries file.

1

u/Old-Register-4255 2d ago

Nice, that's a solid way to enforce it. The lint rule is better than relying on review.

1

u/boer0327 1d ago

for sure, reviewers always let one slip during a deadline crunch. having CI fail the build right away keeps the boundaries clean.

0

u/[deleted] 8d ago

[removed] — view removed comment

1

u/Old-Register-4255 8d ago

Fair point, but I'd call the root config composition, not a leak. It's the one place that's allowed to know every module, and it only imports each module's exported collection config from an index file. The rule I care about is that modules don't import each other's internals or the root config. Root knowing about modules is fine, modules knowing about root isn't.

3

u/BrownCarter 8d ago

Don't just over do the DDD

1

u/Easy-Shelter-5140 8d ago

What do you mean?

2

u/RequirementGuide 7d ago

I think what he means is don't build the full textbook version before you need it. I've done that before and ended up with a pile of aggregates and repositories and events for an app that was really just a few CRUD screens with one tricky bit in the middle. The folder split you already got here is the useful part, keeping each domain's logic together so it doesn't leak everywhere. The trap is adding layers for rules that don't exist yet, because then every small change touches four files. I'd start with the module split and only add the heavier DDD pieces when a domain actually gets complicated enough to earn them.

2

u/SecureComfortable259 6d ago

I'd organize by feature first (modules/billing, modules/catalog) with domain/application/infrastructure inside each, and keep Payload collections in infrastructure so its types never leak into your domain.

2

u/phiger78 6d ago

I've followed DDD, or what I call "DDD Lite", within a monorepo architecture.

There are two main parts to Domain-Driven Design:

  • Strategic DDD – Bounded contexts, ubiquitous language, subdomains and context mapping.
  • Tactical DDD – Entities, value objects, aggregates, repositories, domain services and domain events.

However, you don't necessarily need to adopt all the tactical patterns. For example, repositories may introduce unnecessary abstraction when you already have API clients and TanStack Query managing data retrieval and mutations.

In my usecase i adopted specific domain libraries that had enfroced rules to keep that bounded context. Inside of these domain libraries i used DTO's, mappers. Within each domain i adopted other folders

domain/data-access - Domain-specific API calls

domain/featyre - Domain logic, smart containers, and domain-specific hooks

domain/utils - Domain specific helpers or shared utilities

dmain/ui - Domain specific dumb UI components

I used a combinationof turborepo boundaries rules with eslint imports to enforce domain rules - eg you can't import other domains + specific rules around each folder- data-access must only contain data-access calls. It can't import from shared UI

1

u/Discordbothost 7d ago

The tricky bit with Payload is that the admin UI and your Next.js app can both change the same record. I'd pick one rule, like which status changes are allowed, then write a test that tries an invalid change through each path. That gives you a concrete reason to keep the rule in one place as you split things into modules.

2

u/[deleted] 8d ago

[removed] — view removed comment

1

u/Easy-Shelter-5140 8d ago

Thank you. Could you provide a folder structure example please?

3

u/Informal_External_55 8d ago

Look at alchemy.run/domain for a good explainer on practical DDD in typescript. It uses effect-ts, but same concepts apply

2

u/Easy-Shelter-5140 8d ago

I will do. Thank you

0

u/[deleted] 5d ago

[removed] — view removed comment

1

u/Easy-Shelter-5140 5d ago

It Sounds like you had some experience.

Could you tell me more?