Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🀍

2.3 KB
Raw Blame

title: Organize by Feature Modules impact: CRITICAL impactDescription: "3-5x faster onboarding and development" tags: architecture, modules, organization

Organize by Feature Modules

Organize your application into feature modules that encapsulate related functionality. Each feature module should be self-contained with its own controllers, services, entities, and DTOs. Avoid organizing by technical layer (all controllers together, all services together). This enables 3-5x faster onboarding and feature development.

Incorrect (technical layer organization):

// Technical layer organization (anti-pattern)
src/
β”œβ”€β”€ controllers/
β”‚   β”œβ”€β”€ users.controller.ts
β”‚   β”œβ”€β”€ orders.controller.ts
β”‚   └── products.controller.ts
β”œβ”€β”€ services/
β”‚   β”œβ”€β”€ users.service.ts
β”‚   β”œβ”€β”€ orders.service.ts
β”‚   └── products.service.ts
β”œβ”€β”€ entities/
β”‚   β”œβ”€β”€ user.entity.ts
β”‚   β”œβ”€β”€ order.entity.ts
β”‚   └── product.entity.ts
└── app.module.ts  // Imports everything directly

Correct (feature module organization):

// Feature module organization
src/
β”œβ”€β”€ users/
β”‚   β”œβ”€β”€ dto/
β”‚   β”‚   β”œβ”€β”€ create-user.dto.ts
β”‚   β”‚   └── update-user.dto.ts
β”‚   β”œβ”€β”€ entities/
β”‚   β”‚   └── user.entity.ts
β”‚   β”œβ”€β”€ users.controller.ts
β”‚   β”œβ”€β”€ users.service.ts
β”‚   β”œβ”€β”€ users.repository.ts
β”‚   └── users.module.ts
β”œβ”€β”€ orders/
β”‚   β”œβ”€β”€ dto/
β”‚   β”œβ”€β”€ entities/
β”‚   β”œβ”€β”€ orders.controller.ts
β”‚   β”œβ”€β”€ orders.service.ts
β”‚   └── orders.module.ts
β”œβ”€β”€ shared/
β”‚   β”œβ”€β”€ guards/
β”‚   β”œβ”€β”€ interceptors/
β”‚   β”œβ”€β”€ filters/
β”‚   └── shared.module.ts
└── app.module.ts

// users.module.ts
@Module({
  imports: [TypeOrmModule.forFeature([User])],
  controllers: [UsersController],
  providers: [UsersService, UsersRepository],
  exports: [UsersService], // Only export what others need
})
export class UsersModule {}

// app.module.ts
@Module({
  imports: [
    ConfigModule.forRoot(),
    TypeOrmModule.forRoot(),
    UsersModule,
    OrdersModule,
    SharedModule,
  ],
})
export class AppModule {}

Reference: NestJS Modules