Connect World implements Domain-Driven Design (DDD) to keep business logic clean, testable, and independent from frameworks and infrastructure.
DDD Architecture Overview
Domain-Driven Design organizes code into four distinct layers, each with specific responsibilities:Domain Layer
The Domain Layer is the heart of the application, containing pure business logic with zero dependencies on frameworks or infrastructure.Entities
Entities represent core business concepts with identity:Customer Entity
Customer Entity
Location: Key Points:
src/domain/entities/Customer.ts- Plain TypeScript interface (no Mongoose, no database concerns)
- Factory function ensures consistent creation
- Optional
idandcreatedAt(set by infrastructure layer)
Order Entity
Order Entity
Location: Business Rules:
src/domain/entities/Order.ts- Activation date is set to current time
- Expiration date calculated from months duration
- Payment method and status are type-safe enums
Plan Entity
Plan Entity
Location: Business Logic:
src/domain/entities/Plan.ts- Centralized pricing matrix
- Factory function generates all plan variations
- Type-safe device counts and durations
Value Objects
Value objects encapsulate validation logic and have no identity:Email Value Object
Location: Benefits:
src/domain/value-objects/Email.ts- Validation at construction time
- Immutable once created
- Automatic normalization (lowercase, trim)
Phone Value Object
Location: Validation:
src/domain/value-objects/Phone.ts- Strips non-digit characters
- Enforces length constraints
- Throws on invalid input
Repository Interfaces
Repositories define data access contracts without implementation details:Application Layer
The Application Layer orchestrates business workflows by coordinating domain entities and repositories.Use Cases
CreateOrderUseCase
CreateOrderUseCase
Location: Responsibilities:
src/application/use-cases/CreateOrderUseCase.ts- Validate input using value objects
- Create domain entities
- Coordinate repository operations
- Transform output to DTO
Data Transfer Objects (DTOs)
OrderDto
OrderDto
Location: Purpose:
src/application/dtos/OrderDto.ts- Define API contracts
- Decouple external API from internal entities
- Facilitate serialization (Date → ISO string)
Infrastructure Layer
The Infrastructure Layer implements technical details like database access and external APIs.Repository Implementations
MongoOrderRepository
MongoOrderRepository
Location: Key Features:
src/infrastructure/repositories/MongoOrderRepository.ts- Implements
IOrderRepositoryinterface - Converts between camelCase (domain) and snake_case (database)
- Handles MongoDB connection
- Uses Mongoose models internally
MongoCustomerRepository
MongoCustomerRepository
Location:
src/infrastructure/repositories/MongoCustomerRepository.tsSimilar pattern to MongoOrderRepository:- Implements
ICustomerRepository - Maps customer entities to/from database documents
- Handles queries and persistence
Database Models
OrderModel (Mongoose Schema)
OrderModel (Mongoose Schema)
Location: Database Conventions:
src/infrastructure/database/models/OrderModel.ts- snake_case field names (MongoDB standard)
- Timestamps with
created_at - Unique constraint on
payment_receipt_id
Database Connection
MongoDB Connection
MongoDB Connection
Location: Serverless Optimization:
src/infrastructure/database/connection.ts- Connection cached globally
- Reuses existing connections across function invocations
- Prevents connection exhaustion
External Services
TMDB Service
TMDB Service
Location: Features:
src/infrastructure/external/tmdbService.ts- Axios client with pre-configured base URL and API key
- Spanish language content (“es-ES”)
- Helper functions for poster URLs
Presentation Layer
The Presentation Layer handles user interaction through React components and API routes.API Route Example
Order API Route
Order API Route
Location: Layers Interaction:
src/app/api/orders/route.ts- Presentation receives HTTP request
- Presentation sanitizes and validates input
- Presentation instantiates repositories (Infrastructure)
- Presentation creates use case (Application)
- Application orchestrates domain logic
- Domain enforces business rules
- Infrastructure persists to database
- Presentation returns HTTP response
Component Organization
Section Components
Location:
src/presentation/components/sections/HeroSection.tsx: Landing hero with animationsPlansSection.tsx: Pricing displayCatalogSection.tsx: Content showcaseCheckoutModal.tsx: Payment formContactSection.tsx: Contact infoFooter.tsx: Site footerNavbar.tsx: Navigation bar
UI Components
Location:
src/presentation/components/ui/BackgroundGradient.tsx: Gradient effectsGlowCard.tsx: Card with glow effectMovingCards.tsx: Animated card carouselSpotlight.tsx: Spotlight effectSparklesCore.tsx: Particle effectsTextReveal.tsx: Text animationsNumberCounter.tsx: Animated counters
Benefits of DDD Architecture
Testability
Domain logic can be tested without:
- Database connections
- HTTP requests
- Framework dependencies
CreateOrderUseCase with mock repositoriesFlexibility
Easy to swap implementations:
- MongoDB → PostgreSQL
- Stripe → another payment processor
- Next.js API → Express.js
Clarity
Each layer has a clear purpose:
- Domain: Business rules
- Application: Workflows
- Infrastructure: Technical details
- Presentation: User interaction
Maintainability
Changes are localized:
- Database schema change? Update infrastructure layer
- New business rule? Update domain layer
- UI redesign? Update presentation layer
Dependency Flow
Dependency Rule: Dependencies point inward. Outer layers depend on inner layers, never the reverse.
- Presentation depends on Application and Infrastructure
- Application depends on Domain
- Infrastructure implements Domain interfaces
- Domain depends on nothing (pure business logic)
Real-World Example Flow
Let’s trace a complete order creation:- User Action: Customer clicks “Pay” in
CheckoutModal.tsx(Presentation) - HTTP Request: POST to
/api/ordersroute (Presentation) - Validation: Rate limiting, sanitization, honeypot check (Presentation)
- Dependency Injection: Create repositories and use case (Presentation)
- Use Case Execution:
CreateOrderUseCase.execute()(Application) - Value Object Validation:
new Email(),new Phone()(Domain) - Entity Creation:
createCustomer(),createOrder()(Domain) - Persistence:
customerRepo.create(),orderRepo.create()(Infrastructure) - Database Operation: Mongoose inserts to MongoDB (Infrastructure)
- Response Mapping: Entity → DTO → JSON (Application + Presentation)
- HTTP Response: 201 with order details (Presentation)
Notice how each layer has a specific role, and the domain layer never knows about MongoDB, HTTP, or Next.js!
Next Steps
Project Structure
Complete directory tree and file details
API Reference
Detailed endpoint documentation
Development Guide
Setup and workflow instructions
Architecture Overview
High-level system architecture
