DEV Community

Discussion on: Domain-driven Design (DDD): File Structure

Collapse
 
aleron75 profile image
Alessandro Ronchi

Thanks for sharing your point of view on how to apply DDD in real projects, very useful reading.

I've also found it very inspiring to read this book by Matthias Noback:

matthiasnoback.nl/book/advanced-we...

The book goes deep into the idea of separating core (domain) code from infrastructure, with a lot of real code examples.

It uses PHP examples but the concepts can be applied in general.

Collapse
 
stevescruz profile image
Steve Cruz

I did not know this book. Thanks for sharing Alessandro, I'll definitely check it out.

Collapse
 
goceb profile image
Goce • Edited

+1 for the Matthias Noback book, although I do not agree with everything in the book (personal preferences etc..), still it is a good read especially if you are just starting out with getting some order in your code..
@stevescruz the folder structure presented seems like it fits just one bounded context and at a larger scale it will get messy. Something that has been in use for quite some time in the DDD community is the Application/Domain/Infrastructure groupings by BC. That way you have clear separation between them and if you are starting with a monolith it is quite easy to move to microservices since the code is already logically grouped across your future service boundaries. Here is a quick example of what a single folder looks like:

Folders

Collapse
 
stevescruz profile image
Steve Cruz

You are right, I can view more clearly the domain and the infrastructure groups, but sometimes I lose sight of the application group.

Thanks for sharing this file structure, it is very organized. I love seeing how other people structure things.
Where do you store your routes (I imagine in the web folder at infrastructure)? and your controllers and why?

Thread Thread
 
goceb profile image
Goce

Controllers are part of the infrastructure layer so, Infrastructure/Web is where all the web related stuff sits. Console is for console commands (still in Infrastructure). Routes on the other hand exist in an independent file just for routes (not a big fan of annotations), dependencies are defined in a separate file too, which aggregates the different dependency providers for each BC or module. I use PHP (no framework) but I think this approach is language agnostic.

Collapse
 
delucca profile image
Daniel De Lucca • Edited

Sorry for reviving this thread, but I've just found out your suggested DDD folder structure and it is pretty interesting!

I was separating application, domain and infrastructure inside my src folder, but your approach is way cleaner.

I just have a simple question, where do you store abstract interfaces? For example, I have a bunch of abstract classes that I use inside all my layers, in which folder would you store those?

Just to be clear, those abstractions are used by basically all my aggregates. For example, I have an "DomainAggregateService" abstract class, which is basically some global methods that every aggregate service has.

I'm thinking to give it a shot to your suggestion, and placing it in something like lib/... or something like that (while my aggregates would be inside src, since I'm using Javascript and it is pretty common to place source code inside src folder)

Thread Thread
 
goceb profile image
Goce

Abstract classes like AggregateRoot, AggregateRootCollection and interfaces like DomainEvent / Listeners etc are defined in a composer package that is imported in the project.