- ✅ Domain (Entity + Value Object)
- Business Rules (Pure english)
- ✅ Application (Use Case + Port)
- ✅ Infrastructure (Fake Repository)
- ✅ Presentation (Simple controller function)
Build a minimal Clean Architecture setup for creating a user with validation and repository storage.
Development sequence must be like this
Entity -> Port -> Usecase -> Infrastructure -> Controller
Business Rules:
- Entities
- User has an ID
- User has a phone number
- User has a sex
- User has a role
- User has an age
- Value Objects
- ID is a sequencial integer
- PhoneNumber is a 10 digit field
- Sex is male or female
- Role can be admin/user/guest
- Age is an integer from 0 to 120
-
Port: UserRepository is an interface which specify user's model behaviour without the implementation. e.g. save(user) or findById(id)
-
UseCase: CreateUser is an usecase which use the UserRepository's methods to add some functionalities on domain area.
Ports (Repositories) in fact are an interface between the Usecases and Entities
MongoUserRepository in fact implements the UserRepository methods. The UserRepository specifies WHAT to do and MongoUserRepository specifies HOW to do it with MongoDB.
You can swap MongoDB for PostgreSQL, MySQL, or an in-memory store without causing any change in application or domain layer.
createUserController connects the framework to the CreateUser Usecase so Controllers use the UseCases to implement their functionality
- Get DB client (e.g. MongoClient)
- Create infrastructure repo (MongoUserRepository)
- Pass MongoUserRepository's instance to CreateUser UseCase (It's possible because that infrastructure repository implements port repository)
- Pass the CreateUser UseCase to the createUserController
- Controller execute the UseCase
- Node.js 18+ (or use nvm)
- MongoDB running on
localhost:27017
- Navigate to the node directory:
cd node- Install dependencies:
npm install- Run the server:
npm startThe server will start on http://localhost:3000
curl -X POST http://localhost:3000/create_user \
-H "Content-Type: application/json" \
-d '{
"phone_number": "1234567890",
"role": "user",
"age": 25,
"sex": "male"
}'curl -X POST http://localhost:3000/create_user -H "Content-Type: application/json" -d '{"phone_number":"1234567890","role":"user","age":25,"sex":"male"}'curl -X POST http://localhost:3000/create_user \
-H "Content-Type: application/json" \
-d '{
"phone_number": "9876543210",
"role": "admin",
"age": 30,
"sex": "female"
}'curl -X POST http://localhost:3000/create_user \
-H "Content-Type: application/json" \
-d '{
"phone_number": "5555555555",
"role": "guest",
"age": 18,
"sex": "male"
}'curl -X POST http://localhost:3000/create_user \
-H "Content-Type: application/json" \
-d '{
"phone_number": "123",
"role": "user",
"age": 25,
"sex": "male"
}'Expected Response: 400 Bad Request
curl -X POST http://localhost:3000/create_user \
-H "Content-Type: application/json" \
-d '{
"phone_number": "1234567890",
"role": "user",
"age": 150,
"sex": "male"
}'Expected Response: 400 Bad Request
curl -X POST http://localhost:3000/create_user \
-H "Content-Type: application/json" \
-d '{
"phone_number": "1234567890",
"role": "user",
"age": -5,
"sex": "male"
}'Expected Response: 400 Bad Request
curl -X POST http://localhost:3000/create_user \
-H "Content-Type: application/json" \
-d '{
"phone_number": "1234567890",
"role": "invalid_role",
"age": 25,
"sex": "male"
}'Expected Response: 400 Bad Request
curl -X POST http://localhost:3000/create_user \
-H "Content-Type: application/json" \
-d '{
"phone_number": "1234567890",
"role": "user",
"age": 25,
"sex": "invalid"
}'Expected Response: 400 Bad Request
curl -X POST http://localhost:3000/create_user \
-H "Content-Type: application/json" \
-d '{
"phone_number": "123456789a",
"role": "user",
"age": 25,
"sex": "male"
}'Expected Response: 400 Bad Request
If you have jq installed, you can pretty-print the JSON response:
curl -X POST http://localhost:3000/create_user \
-H "Content-Type: application/json" \
-d '{
"phone_number": "1234567890",
"role": "user",
"age": 25,
"sex": "male"
}' | jq .CC Architecture
CC Implementation