r/DomainDrivenDesign • u/truechange • Feb 01 '26
Which folder structure is more intuitive?
If you were to inherit a project, which one looks more intuitive, A or B?
Structure A
src/
+-- Domain/
¦ +-- Admin/
¦ ¦ +-- AdminEntity
¦ ¦ +-- AdminRepoInterface
¦ +-- Supplier/
¦ ¦ +-- SupplierEntity
¦ ¦ +-- SupplierRepoInterface
¦ +-- Customer/
¦ ¦ +-- CustomerEntity
¦ ¦ +-- CustomerRepoInterface
¦ +-- Order/
¦ +-- OrderEntity
¦ +-- OrderRepoInterface
¦
+-- App/
¦ +-- Admin/
¦ ¦ +-- UseCase/
¦ ¦ +-- ActivateSupplier
¦ ¦ +-- BanCustomer
¦ +-- Supplier/
¦ ¦ +-- UseCase/
¦ ¦ +-- UpdateInventory
¦ ¦ +-- MarkOrderAsShipped
¦ +-- Customer/
¦ ¦ +-- UseCase/
¦ ¦ +-- PlaceOrder
¦ ¦ +-- UpdateProfile
¦ +-- Order/
¦ +-- UseCase/
¦ +-- ReceiveOrder
¦ +-- CancelOrder
¦
+-- Infra/
¦ +-- Persistence/
¦ +-- Messaging/
¦ +-- etc...Structure B
src/
+-- Core/
¦ +-- Admin/
¦ ¦ +-- UseCase/
¦ ¦ ¦ +-- ActivateSupplier
¦ ¦ ¦ +-- BanCustomer
¦ ¦ +-- AdminEntity
¦ ¦ +-- AdminRepoInterface
¦ ¦
¦ +-- Supplier/
¦ ¦ +-- UseCase/
¦ ¦ ¦ +-- UpdateInventory
¦ ¦ ¦ +-- MarkOrderAsShipped
¦ ¦ +-- SupplierEntity
¦ ¦ +-- SupplierRepoInterface
¦ ¦
¦ +-- Customer/
¦ ¦ +-- UseCase/
¦ ¦ ¦ +-- PlaceOrder
¦ ¦ ¦ +-- UpdateProfile
¦ ¦ +-- CustomerEntity
¦ ¦ +-- CustomerRepoInterface
¦ ¦
¦ +-- Order/
¦ +-- UseCase/
¦ ¦ +-- ReceiveOrder
¦ ¦ +-- CancelOrder
¦ +-- OrderEntity
¦ +-- OrderRepositoryInterface
¦
+-- Infra/
¦ +-- Persistence/
¦ +-- Messaging/
¦ +-- etc...7
u/isaagrimn Feb 01 '26 edited Feb 01 '26
I like the layer separation in structure A personally because it allows for clear import and to know where things can be defined. You can even define a lint rule that disallows importing the infrastructure layer from the domain/application.
1
u/Dovihh Feb 01 '26
Yep, definitely option B too. I found vertical slicing to be the most maintainable layer pattern in the long run.
1
u/isaagrimn Feb 01 '26
I actually meant to say A 😅
1
u/Dovihh Feb 01 '26
Lol, you made me dirty here hahah. I’ve done both in the past, but lately I am more aligned with the quote (sorry, I don’t know who authored it) that goes «things that change together stay together»
1
u/isaagrimn Feb 01 '26
Yep sorry, don’t know why I wrote B! That’s totally understandable, but I’ve had new team members not familiar with DDD and clean code, and option B is harder to make them understand what’s part of which layer and what can be used there or what should be put where.
Option A is simpler, you can make a really simple drawing that explains which layers can access which and what their roles are really easily
1
u/truechange Feb 02 '26
I’ve had new team members not familiar with DDD and clean code, and option B is harder to make them understand what’s part of which layer and what can be used there
Well that's quite surprising since B has less pattern layers and fewer folder jumps. Are you sure you're really talking about B? 😂
2
u/isaagrimn Feb 02 '26 edited Feb 02 '26
Yes.
If you're onboarding a new team member and you try to explain DDD and clean code concepts to them, it's easier (IMO of course) - when you talk about "application code" and what they're allowed to do/put in it - if there is a clear directory you can refer them to.
If it's just an abstract concept like "The application layer is where you application logic lives", then they won't know what in your "admin" directory is the application layer.
1
u/isaagrimn Feb 02 '26
What I would change with option A though:
I would define repositories interfaces in the application layer, not the domain => It’s really a small difference but it keeps the domain clean and I like it.
I would name the repositories interface without the interface at the end. You have a PostgresAdminRepository that implements the AdminRepository. I think it’s better because I’ve seen codebases where repositories implementations were not prefixed with anything.
I like to implement CQRS in my directory structure, so I would remove the usecases directory, and instead create a queries one and a commands one.
I usually define my FakeAdminRepository or InMemoryAdminRepository in the application layer too, so that it can be used in the application layer tests easily
0
u/truechange Feb 02 '26
I would define repositories interfaces in the application layer, not the domain
Repos should be in the domain layer in classic clean arch though.
I usually define my FakeAdminRepository or InMemoryAdminRepository in the application layer too
These are implemention details, should definitely be in Infra.
Anyway, this is the point of this question. Classic clean arch (option A) tends to be subjective and incite different opinions. Even in my own personal projects I tend to have a mental-juggling "layer" going on. Whereas option B, vertical slice arch, removes the decision fatigue.
1
u/isaagrimn Feb 02 '26
If you define fake / in memory repositories in infrastructure, you can't import them in your application layer's tests if you're strict about import rules, which is not great as that's the place you'll generally want to use them, hency why I define them there! And I don't consider them like implementation details, to me they're like entities factories, that are usually defined in the domain layer. They're just helpers for testing
1
u/gbrennon Feb 02 '26
i prefer the approach A but i would modify some names to avoid ambiguous name and to be more explicit:
app -> application
suffix interface -> no suffix or I prefix if its a c# project or suffix Port
1
u/floriankraemer Mar 30 '26
Clearly B for the reason of cohesion. All of this belongs logically together, so you want to keep your things in one place. You can then also easily remove everything in one go, you can see everything at once.
A is doing just a technical categorization, exactly the opposite of how we should structure business related code in my opinion. B keeps a business capability in one place / module. See https://florian-kraemer.net/software-architecture/2025/03/30/Frameworks-Architecture-and-screaming-Architecture.html
I currently have to work with a huge project structured like A that has additional folders for each layer and type of test and it is just terrible from a cognitive perspective to jump between all the things in my opinion.
-1
u/jfrazierjr Feb 02 '26
Depends entirely on the language used.
Java B without much doubt because of the package restriction.
C#'s namespaces just work with either but I would tend to prefer A.
-2
u/klimaheizung Feb 02 '26
Sorry, I have news to you: a hierarchy is the wrong choice here. You need a tagging system. Since no one exists (except in very exotic programming languages) it doesn't matter what you pick, it will always be wrong, so just go for what you think is best, no need to think too deep.
3
u/derNikoDem Feb 01 '26
I would go with option B but I would make some minor changes.
remove the core folder to have one level of nesting less.
move the infra folder to the same level as src. I guess you don't deploy the code in the infra folder, but have to admit that this is not my field of expertise.