enterprise-ddd
8
总安装量
4
周安装量
#34574
全站排名
安装命令
npx skills add https://github.com/alicoder001/agent-skills --skill enterprise-ddd
Agent 安装分布
openclaw
4
opencode
4
cursor
4
codex
4
claude-code
4
antigravity
4
Skill 文档
Enterprise DDD Architecture
Domain-Driven Design + Feature-Sliced Design + Microservices
Architecture Overview
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â APPS LAYER â
â âââââââââââ âââââââââââ âââââââââââ âââââââââââ â
â â Web â â Admin â â Mobile â â Docs â â
â ââââââ¬âââââ ââââââ¬âââââ ââââââ¬âââââ ââââââ¬âââââ â
âââââââââââ¼âââââââââââââ¼ââââââââââââ¼ââââââââââââ¼âââââââââââââ
â â â â
â¼ â¼ â¼ â¼
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â MODULES LAYER (DDD) â
â ââââââââââââ ââââââââââââ ââââââââââââ ââââââââââââ â
â â @user â â @order â â @payment â â @catalog â â
â â (FSD) â â (FSD) â â (FSD) â â (FSD) â â
â ââââââ¬ââââââ ââââââ¬ââââââ ââââââ¬ââââââ ââââââ¬ââââââ â
âââââââââ¼âââââââââââââ¼ââââââââââââ¼ââââââââââââ¼ââââââââââââââââ
â Events â Events â Events â
â¼ â¼ â¼ â¼
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â SERVICES LAYER (Backend) â
â ââââââââââââââ ââââââââââââââ ââââââââââââââ â
â âuser-serviceâ âorder-serviceâ âpayment-svc â â
â ââââââââââââââ ââââââââââââââ ââââââââââââââ â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Instructions
1. Monorepo Structure
enterprise-project/
âââ apps/ # Deployable applications
â âââ web/ # Next.js main app
â âââ admin/ # Admin dashboard
â âââ mobile/ # React Native
â âââ docs/ # Documentation
â
âââ modules/ # DDD Bounded Contexts
â âââ @user/ # User domain (FSD inside)
â âââ @order/ # Order domain (FSD inside)
â âââ @payment/ # Payment domain (FSD inside)
â âââ @catalog/ # Catalog domain (FSD inside)
â âââ @notification/ # Notification domain (FSD inside)
â
âââ packages/ # Shared infrastructure
â âââ ui/ # Design system
â âââ config/ # Shared configs
â âââ utils/ # Common utilities
â âââ types/ # Shared types
â âââ api-client/ # API client factory
â
âââ services/ # Backend microservices
â âââ api-gateway/ # API Gateway
â âââ user-service/ # User microservice
â âââ order-service/ # Order microservice
â âââ shared/ # Shared service libs
â
âââ infrastructure/ # DevOps
â âââ docker/
â âââ k8s/
â
âââ turbo.json
âââ pnpm-workspace.yaml
âââ package.json
2. Module Structure (FSD per Domain)
modules/@user/
âââ entities/ # Domain entities
â âââ user/
â â âââ model/
â â â âââ user.types.ts # User interface
â â â âââ user.schema.ts # Zod schema
â â â âââ user.store.ts # Entity store
â â âââ api/
â â â âââ user.api.ts # API calls
â â â âââ user.queries.ts # React Query hooks
â â âââ ui/
â â âââ UserCard.tsx
â â âââ UserAvatar.tsx
â âââ session/
â âââ model/
â âââ api/
â âââ ui/
â
âââ features/ # Use cases
â âââ auth/
â â âââ model/
â â âââ api/
â â âââ ui/
â â â âââ LoginForm.tsx
â â â âââ RegisterForm.tsx
â â âââ index.ts
â âââ profile/
â âââ settings/
â
âââ widgets/ # Composite UI blocks
â âââ user-header/
â â âââ UserHeader.tsx
â â âââ index.ts
â âââ user-sidebar/
â
âââ shared/ # Module-specific shared
â âââ api/
â â âââ client.ts # Module API client
â âââ lib/
â â âââ permissions.ts # Permission helpers
â âââ config/
â â âââ routes.ts # Module routes
â âââ events/
â âââ user.events.ts # Domain events
â
âââ index.ts # Public API
âââ package.json
3. Public API Pattern
// modules/@user/index.ts
// ONLY export what other modules can use
// Entities (read-only)
export type { User, UserRole } from './entities/user/model';
export { UserCard, UserAvatar } from './entities/user/ui';
// Features (actions)
export { LoginForm, RegisterForm } from './features/auth';
export { useAuth, useCurrentUser } from './features/auth';
// Widgets (composite)
export { UserHeader } from './widgets/user-header';
// Events (communication)
export { userEvents } from './shared/events';
// DO NOT export internal implementation details
4. Cross-Module Communication
// â FORBIDDEN: Direct cross-module imports
import { Order } from '@order/entities'; // NO!
// â
ALLOWED: Event-based communication
// modules/@user/shared/events/user.events.ts
import { createEventBus } from '@repo/utils/events';
export const userEvents = createEventBus<{
'user:created': { userId: string; email: string };
'user:updated': { userId: string; changes: Partial<User> };
'user:deleted': { userId: string };
}>();
// modules/@order/features/create-order/model/useCreateOrder.ts
import { userEvents } from '@user/shared/events';
userEvents.on('user:created', async ({ userId }) => {
// Create welcome cart for new user
await createWelcomeCart(userId);
});
5. Backend Service Structure (DDD)
services/user-service/
âââ src/
â âââ domain/ # Domain Layer (Pure)
â â âââ entities/
â â â âââ User.ts # User aggregate root
â â âââ value-objects/
â â â âââ Email.ts
â â â âââ Password.ts
â â âââ events/
â â â âââ UserCreated.ts
â â â âââ UserUpdated.ts
â â âââ repositories/
â â âââ IUserRepository.ts # Interface only
â â
â âââ application/ # Application Layer
â â âââ commands/
â â â âââ CreateUser.ts
â â â âââ UpdateUser.ts
â â âââ queries/
â â â âââ GetUser.ts
â â â âââ ListUsers.ts
â â âââ services/
â â âââ AuthService.ts
â â
â âââ infrastructure/ # Infrastructure Layer
â â âââ persistence/
â â â âââ UserRepository.ts # Implementation
â â â âââ prisma/
â â âââ messaging/
â â â âââ RabbitMQPublisher.ts
â â âââ external/
â â âââ StripeClient.ts
â â
â âââ presentation/ # Presentation Layer
â âââ controllers/
â â âââ UserController.ts
â âââ dtos/
â â âââ CreateUserDto.ts
â â âââ UserResponseDto.ts
â âââ mappers/
â âââ UserMapper.ts
â
âââ prisma/
â âââ schema.prisma
âââ Dockerfile
âââ package.json
6. Event-Driven Architecture
// services/shared/events/index.ts
export const DomainEvents = {
User: {
Created: 'user.created',
Updated: 'user.updated',
Deleted: 'user.deleted',
},
Order: {
Created: 'order.created',
Completed: 'order.completed',
Cancelled: 'order.cancelled',
},
Payment: {
Processed: 'payment.processed',
Failed: 'payment.failed',
},
} as const;
// services/user-service/src/application/commands/CreateUser.ts
import { EventPublisher } from '@services/shared/messaging';
export class CreateUserHandler {
constructor(
private userRepo: IUserRepository,
private eventPublisher: EventPublisher,
) {}
async execute(command: CreateUserCommand): Promise<User> {
const user = User.create(command);
await this.userRepo.save(user);
// Publish domain event
await this.eventPublisher.publish(DomainEvents.User.Created, {
userId: user.id,
email: user.email.value,
createdAt: new Date(),
});
return user;
}
}
7. Workspace Configuration
# pnpm-workspace.yaml
packages:
- "apps/*"
- "modules/*"
- "packages/*"
- "services/*"
// turbo.json
{
"$schema": "https://turbo.build/schema.json",
"tasks": {
"build": {
"dependsOn": ["^build"],
"outputs": [".next/**", "dist/**"]
},
"dev": {
"cache": false,
"persistent": true
},
"lint": {
"dependsOn": ["^build"]
},
"test": {
"dependsOn": ["^build"]
}
}
}
8. Import Rules
// eslint-plugin-import rules
const importRules = {
// Apps can import from modules and packages
'apps/*': ['modules/*', 'packages/*'],
// Modules can import from packages, NOT from other modules
'modules/*': ['packages/*', '@repo/*'],
// FSD layer rules within module
'entities': ['shared'],
'features': ['entities', 'shared'],
'widgets': ['features', 'entities', 'shared'],
// Services can import from shared only
'services/*': ['services/shared'],
};
// .eslintrc.js
module.exports = {
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
{
group: ['@order/*', '@payment/*', '@catalog/*'],
message: 'Cross-module imports forbidden. Use events.',
},
],
},
],
},
};
9. Code Generator Commands
# Create new domain module
pnpm generate:module @inventory
# Create new feature in module
pnpm generate:feature @user/password-reset
# Create new entity
pnpm generate:entity @order/order-item
# Create new backend service
pnpm generate:service inventory-service
10. Database Strategy
âââââââââââââââââââââââââââââââââââââââââââââââ
â Database per Service â
âââââââââââââââââââââââââââââââââââââââââââââââ¤
â user-service â PostgreSQL (users) â
â order-service â PostgreSQL (orders) â
â payment-service â PostgreSQL (payments) â
â catalog-service â PostgreSQL (products) â
â notification-svc â MongoDB (notifications) â
â analytics-svc â ClickHouse (events) â
âââââââââââââââââââââââââââââââââââââââââââââââ
Communication: Events via RabbitMQ/Kafka
Decision Matrix
| Decision | Choice | Reason |
|---|---|---|
| Monorepo Tool | Turborepo | Speed, caching |
| Package Manager | pnpm | Disk space, speed |
| Frontend | Next.js 14+ | RSC, performance |
| State (Server) | TanStack Query | Caching, sync |
| State (Client) | Zustand | Simplicity |
| Backend | NestJS | DDD support |
| Database | PostgreSQL | ACID, reliability |
| Events | RabbitMQ | Reliability |
| API Gateway | Kong/NestJS | Flexibility |
| Auth | Keycloak | Enterprise ready |
Quick Start
# Clone template
npx degit Alicoder001/enterprise-template my-project
# Install dependencies
cd my-project && pnpm install
# Start development
pnpm dev