Inventory Management System - Robust Backend Folder Structure

πŸ“š Hierarchy of Folders & Files on the Left.
πŸ’‘ Architectural Purpose in Hexagonal, Microservices, and Event Driven Columns on the Right.

Hierarchy Structure

πŸ“ inventory_management_backend/
β”œβ”€β”€ πŸ“ app/
Main application package
β”‚ β”œβ”€β”€ πŸ“ domain/
Business entities and pure domain logic
β”‚ β”‚ β”œβ”€β”€ πŸ“ models/
Domain entities
β”‚ β”‚ β”‚ β”œβ”€β”€ πŸ“„ user.py
β”‚ β”‚ β”‚ └── πŸ“„ product.py
β”‚ β”‚ └── πŸ“ services/
β”‚ β”‚ └── πŸ“„ inventory_service.py
β”‚ β”œβ”€β”€ πŸ“ application/
Use cases, DTOs, orchestrators, event handlers
β”‚ β”‚ β”œβ”€β”€ πŸ“ use_cases/
Business operations
β”‚ β”‚ β”‚ β”œβ”€β”€ πŸ“ product/
β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ πŸ“„ create_product.py
β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ πŸ“„ get_product.py
β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ πŸ“„ update_product.py
β”‚ β”‚ β”‚ β”‚ └── πŸ“„ delete_product.py
β”‚ β”‚ β”‚ └── πŸ“ user/
β”‚ β”‚ β”‚ β”œβ”€β”€ πŸ“„ register_user.py
β”‚ β”‚ β”‚ └── πŸ“„ login_user.py
β”‚ β”‚ β”œβ”€β”€ πŸ“ dto/
DTOs for requests/responses
β”‚ β”‚ β”‚ β”œβ”€β”€ πŸ“„ product_dto.py
β”‚ β”‚ β”‚ └── πŸ“„ user_dto.py
β”‚ β”‚ └── πŸ“ event_handlers/
β”‚ β”‚ └── πŸ“„ inventory_events.py
β”‚ β”œβ”€β”€ πŸ“ ports/
Abstract interfaces for repositories, auth, event bus
β”‚ β”‚ β”œβ”€β”€ πŸ“„ product_repository.py
β”‚ β”‚ β”œβ”€β”€ πŸ“„ user_repository.py
β”‚ β”‚ └── πŸ“„ event_bus.py
β”‚ β”œβ”€β”€ πŸ“ adapters/
Infrastructure implementations (DB, auth, events)
β”‚ β”‚ β”œβ”€β”€ πŸ“ repositories/
β”‚ β”‚ β”‚ β”œβ”€β”€ πŸ“„ product_repository_impl.py
β”‚ β”‚ β”‚ └── πŸ“„ user_repository_impl.py
β”‚ β”‚ β”œβ”€β”€ πŸ“ db/
β”‚ β”‚ β”‚ β”œβ”€β”€ πŸ“„ base.py
β”‚ β”‚ β”‚ └── πŸ“„ session.py
β”‚ β”‚ β”œβ”€β”€ πŸ“ auth/
β”‚ β”‚ β”‚ └── πŸ“„ jwt_manager.py
β”‚ β”‚ └── πŸ“ events/
β”‚ β”‚ └── πŸ“„ event_publisher.py
β”‚ β”œβ”€β”€ πŸ“ api/
FastAPI routers/controllers
β”‚ β”‚ β”œβ”€β”€ πŸ“ v1/
β”‚ β”‚ β”‚ β”œβ”€β”€ πŸ“„ product_router.py
β”‚ β”‚ β”‚ └── πŸ“„ user_router.py
β”‚ β”‚ └── πŸ“„ dependencies.py
β”‚ β”œβ”€β”€ πŸ“ config/
β”‚ β”‚ └── πŸ“„ settings.py
β”‚ β”œβ”€β”€ πŸ“„ main.py
β”‚ └── πŸ“„ events.py
β”œβ”€β”€ πŸ“ tests/
Unit & integration tests
β”‚ β”œβ”€β”€ πŸ“„ domain/
β”‚ β”œβ”€β”€ πŸ“„ application/
β”‚ β”œβ”€β”€ πŸ“„ adapters/
β”‚ β”œβ”€β”€ πŸ“„ api/
β”‚ └── πŸ“„ conftest.py
β”œβ”€β”€ πŸ“„ requirements.txt
β”œβ”€β”€ πŸ“„ README.md
└── πŸ“ alembic/
DB migrations

πŸ›‘οΈ Hexagonal

πŸ“ domain/
Defines pure business logic and entities, independent of frameworks/infrastructure. Purpose: Isolate and reuse core logic.
πŸ“ application/
Orchestrates domain logic, use cases, DTOs, and event handlers. Purpose: Implements business workflows.
πŸ“ ports/
Abstract interfaces for repositories, auth, event bus. Purpose: Decouples business logic from infrastructure.
πŸ“ adapters/
Infrastructure implementationsβ€”repositories, DB, auth, event publisher. Purpose: Bridges core to external systems.
πŸ“ api/
FastAPI routers/controllers as entry points. Purpose: Adapts HTTP requests to business logic.
πŸ“ config/
App configuration, secrets, DB settings. Purpose: Technical details kept out of core logic.
πŸ“„ main.py
App entry point, wires dependencies. Purpose: Bootstraps hexagonal application.

πŸͺ Microservices

πŸ“ app/
Microservice boundary (can split into submodules/services). Purpose: Enables independent deployment and scaling.
πŸ“„ main.py
Microservice entry point. Purpose: Run services independently.
πŸ“ config/
Service-specific configuration. Purpose: Environment setup per microservice.
πŸ“ adapters/events/
Messaging adapters for each service. Purpose: Enables async communication between microservices.
πŸ“ tests/
Service/module-level tests. Purpose: Reliability and separation of concerns.
πŸ“ alembic/
Service-specific DB migrations. Purpose: Schema evolution per microservice.

⚑ Event Driven

πŸ“ application/event_handlers/
Handles events and triggers workflows. Purpose: Responds to system changes asynchronously.
πŸ“ adapters/events/
Event publisher for domain/integration events. Purpose: Publishes events to brokers.
πŸ“„ events.py
Centralized event definitions. Purpose: Maintains event registry.
πŸ“„ application/dto/*_dto.py
Event payload DTOs. Purpose: Ensures structured event data.
πŸ“ ports/event_bus.py
Event bus interface. Purpose: Decouples event logic from messaging technology.