Skip to content

🌟 Clean Architecture template for Next.js – ✨ Code Smart πŸ’‘ πŸ“‚ layered πŸ§ͺ testable βœ… assured πŸš€ scalable πŸ“¦ dev-friendly

License

Notifications You must be signed in to change notification settings

maurogioberti/next-clean-architecture

Repository files navigation

Mauro Gioberti

Clean Architecture Template for Next.js πŸš€

Next.js Clean Architecture Template πŸ—οΈ

⭐ Stars are welcome 😊 πŸ› Report an issue 🌐 Visit my site

Built with Codescouts

maintained - yes contributions - welcome license - MIT

πŸš€ Getting Started

⬇️ Clone this repo

git clone https://github.com/maurogioberti/next-clean-architecture.git
cd next-clean-architecture

πŸ‚ Install dependencies

npm install

🏁 Run the app

npm run dev

πŸ“‚ Folder Structure

This project is structured to align with Clean Architecture principles. This folder structure is designed to maximize separation of concerns and ensure testability, scalability, and maintainability.

Here's how it's organized:

project-root/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ core/  # Clean Architecture Core Logic
β”‚   β”‚   β”œβ”€β”€ application/  # Use cases
β”‚   β”‚   β”‚   └── get-message-usecase.ts --> Handles application-specific logic
β”‚   β”‚   β”œβ”€β”€ crosscutting/  # Shared utilities and cross-cutting concerns
β”‚   β”‚   β”‚   └── injection/
β”‚   β”‚   β”‚       └── DependencyInjectionContainer.ts --> Configures DI container and bindings
β”‚   β”‚   β”œβ”€β”€ domain/  # Business rules
β”‚   β”‚   β”‚   β”œβ”€β”€ events/
β”‚   β”‚   β”‚   β”‚   └── MessageEvent.ts --> Represents a domain event for notifications
β”‚   β”‚   β”‚   β”œβ”€β”€ model/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Message.ts --> Models a message entity
β”‚   β”‚   β”‚   β”‚   └── User.ts --> Models a user entity
β”‚   β”‚   β”‚   β”œβ”€β”€ repository/
β”‚   β”‚   β”‚   β”‚   └── MessageRepository.ts --> Interface for message repository
β”‚   β”‚   β”‚   └── services/
β”‚   β”‚   β”‚       └── MessageService.ts --> Interface for message service
β”‚   β”‚   └── infrastructure/  # External service implementations
β”‚   β”‚       β”œβ”€β”€ repository/
β”‚   β”‚       β”‚   └── MessageRepositoryImpl.ts --> Concrete implementation of MessageRepository
β”‚   β”‚       └── services/
β”‚   β”‚           └── MessageServiceImpl.ts --> Handles message service
β”‚   └── app/  # Next.js UI and pages
β”‚       β”œβ”€β”€ layout.tsx --> Global layout
β”‚       β”œβ”€β”€ pages/  # Next.js pages
β”‚       β”‚   β”œβ”€β”€ page.tsx --> Page Home component (View)
β”‚       β”‚   └── homeViewModel.ts --> Page Home UI business logic (ViewModel)
β”‚       └── components/  # Reusable UI components
β”‚           └── HomeBanner.tsx --> Example of a reusable banner component
β”œβ”€β”€ public/  # Static assets (e.g., favicon)
β”‚   └── favicon.ico --> Example favicon
└── di.ts  # Dependency injection configuration

πŸ§ͺ Testing

Clean Architecture makes testing simple:

  • Isolated Layers: Test each layer independently (Domain, Application, Infrastructure).
  • Mockable Dependencies: Easily replace services and repositories with mocks.
  • Solid Coverage: Unit and integration tests for core; smoke tests suggested for UI.

✨ Naming Conventions

Consistency is key! Here's the naming convention used in this project:

Layer Example Filename Convention Purpose Notes
Pages page.tsx camelCase UI entry points for each route Represents Next.js pages
Components HomeBanner.tsx PascalCase Reusable UI components Stateless, reusable pieces
ViewModels homeViewModel.ts camelCase UI business logic Encapsulates state
Domain Event MessageEvent.ts PascalCase Models domain-specific events Example: Notification triggers
Domain Model Message.ts PascalCase Defines business entities Core entities for the domain
Domain Repository MessageRepository.ts PascalCase Abstracts data access logic Interface for persistence
Domain Service MessageService.ts PascalCase Encapsulates domain-specific operations Logic specific to the domain
Application Use Cases get-message-usecase.ts kebab-case Implements application-specific workflows Directly consumes repositories
Infrastructure Repository MessageRepositoryImpl.ts PascalCase Implements domain repository interfaces Connects to external services
Infrastructure Service MessageServiceImpl.ts PascalCase Implements domain service interfaces Example: Notifications backend

πŸ§‘β€πŸ’» About This Project

This is a Clean Architecture implementation in Next.js, inspired by Damian Pumar and the amazing work at CodeScouts Academy.

Based on their approach to Clean Architecture, it does not use any of their libraries, as they are CSR-focused. Instead, we implemented our own methods to handle DI with SSR.

If you’re interested in Clean Architecture, check out their React Template.

πŸ€” Contributing

Feel free to fork this repository and send PRs with improvements or fixes. I’d love to hear your feedback or collaborate on cool ideas. Don’t hesitate to open an issue if something isn’t working as expected!


πŸ“œ License

Released under MIT License by Mauro Gioberti.

About

🌟 Clean Architecture template for Next.js – ✨ Code Smart πŸ’‘ πŸ“‚ layered πŸ§ͺ testable βœ… assured πŸš€ scalable πŸ“¦ dev-friendly

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published