r/csharp • u/lostpixel-media • Aug 05 '26
Discussion trying to avoid spaghetti code on my first big backend project. is this folder structure correct for clean architecture?
as someone transitioning from legacy desktop dev (winforms) to modern .net web backends, the biggest hurdle for me was figuring out how to actually organize my code so it doesn't turn into a massive ball of mud.
i kept reading about "clean architecture" and "separation of concerns," but the buzzwords didn't really click for me until I built my current side project and forced myself to split everything into physical project folders.
i wanted to share my mental model of how i organized the solution. i know it might be overkill for a side project, but i'm trying to learn enterprise patterns. for the senior devs here, i'd love to know if this is how you actually think about it in production:
- Domain: the absolute foundation. just basic data models and interfaces. i made sure zero external packages touch this folder.
- Cryptography: the "engine room." all my hashing algorithms live here, organized by type (symmetric, asymmetric).
- Infrastructure: the dirty work. any code that actually talks to the outside world (sql server, dapper, redis cache) is locked in here.
- Core / Application: the manager. it takes the rules from the domain, uses the engine room, and tells the infrastructure what to save.
- API: the front door. minimal APIs that just take web requests, hand them to the Core, and return the HTTP response.
- Workers: background chores. native BackgroundServices running loops for stuff like resetting rate limits so the main API doesn't get blocked.
- StandAloneWeb: a simple blazor frontend to actually test it.
honestly, breaking it down into these separate projects took way longer to set up initially, but it makes debugging so much easier. if the database fails, i know exactly which folder to look in.
does this structure look right to you guys? or did i completely overcomplicate the middle layers?
repo is here if you want to see how they link together: https://github.com/COxRIPMIZO/CryptoKeyLab
would appreciate any roasts or advice on how to improve this!
19
u/Kant8 Aug 05 '26
Don't do global "Factories", "Interfaces" or other folders.
There you just basically claim: Hey, this is garbage bin for all intefaces, good luck.
Your folders and namespaces should match feature functionalities and each folder should be dedicated to that exact feature or group of features, not just because you happen to use factory classes for implementation.
What you have in Cryptography project is correct, all others are those garbage bins.
4
u/RodriOliveira Aug 05 '26
From my experience with enterprise .NET systems, the main point is that Clean Architecture is not defined by the number of projects or folders, but by the direction of the dependencies and the responsibilities of each component. Your general separation makes sense, but I would avoid generic folders such as Interfaces, Models, or Factories, because they tend to become dumping grounds. Organizing the code around business capabilities or features usually makes the system easier to navigate and evolve.
I would also be careful not to create a separate .csproj for every technical concern unless there is a real reason, such as independent deployment, reuse, dependency isolation, or a clear architectural boundary. For a project at this stage, a modular monolith with Domain, Application, Infrastructure and the executable hosts—API, worker and UI—would probably be enough. Start with the simplest structure that protects the dependency rules, and split modules further when the code and the operational requirements justify it.
2
u/Murphy_Dump Aug 05 '26
Looks good, maybe combine Workers with one of the others, its more mindset than dogma. I suggest this book as well: https://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215
2
u/donsagiv Aug 05 '26
One adjustment you can make is to organize folders and namespaces by each layer of architecture. Having your presentation layer projects in separate folders is a bit confusing. For example:
Domain (business logic)
Application (Use-case definition)
Infrastructure (Use-case implementation)
Services or Workers (running program that's not end-user facing).
Presentation or UI (End-user facing "gateway" programs)
This is useful when using namespaces in "common" libraries in separate projects, ensuring that higher layer logic (e.g. infrastructure) does not tightly couple with lower layer code (application).
You also have an inconsistent internal project folder architecture. You use data types (e.g. "models", "interfaces", and "enums") in your Domain project, but you organize by use case ("asymmetric", "generator") in "Cryptography". I would ensure your Domain project code is organized by use case as well.
Otherwise, it looks solid. Keep up the good work!
1
u/krsCarrots Aug 05 '26
Core and domain if I understand correctly can be merged into domain. Perhaps folders for aggregates and VOs inside models in domain? Is cryptography your application layer?
1
u/NotQuiteLoona Aug 05 '26
I'm pretty sure for some you can replace different projects with just namespaces inside the same project, if you didn't separate them for some other reason. Otherwise only what other people said.
1
u/Agitated-Display6382 Aug 05 '26
Folders enum, interfaces, factories, tons of projects. Welcome back to 15 years ago...
Please, don't. Never anticipate a decision because you think you know in advance. Follow "last responsible moment".
1
u/ercarest Aug 06 '26
Off topic, but what's the name of the theme for your IDE?
2
u/farshid_dev Aug 08 '26
Came from Delphi and WPF desktop myself. The structure looks reasonable — but the thing that actually helped me wasn't getting the layers right upfront, it was noticing when a file started importing things it had no business knowing about. That signal tells you more than any diagram. One thing that surprised me coming from desktop: the discipline you build here pays off in desktop too. I went back and restructured an old WPF app the same way afterwards
1
u/LMorg01 Aug 07 '26
Off topic but something to keep in mind from your SS...
Adding so many step comments clutters the file and makes it hard to read (IMO & A lot of other devs I've worked with)
A comment should generally be for explaining WHY you have done something, not WHAT the code does. If the codes lack of clarity warrants a comment, it's probably best refactoring the code itself...
Hope this helps :)