01หลักร่วมก่อนเลือก Folder Structure
Architecture ไม่ใช่ชื่อ folder แต่คือ กฎของ dependency และขอบเขตการเปลี่ยนแปลง เริ่มจากแบ่งตาม business capability เช่น order, payment หรือ identity แล้วทำ public API ของแต่ละ feature ให้เล็ก ส่วน framework, HTTP, database และ third-party service อยู่รอบนอกของ business rule
โค้ดที่เปลี่ยนด้วยเหตุผลเดียวกันอยู่ใกล้กัน
แก้ feature เดียวแต่ต้องเปิดหลายสิบ folder ตาม technical layer
feature รู้จักกันผ่าน contract ที่จำเป็น
import ข้ามไปหา internal file หรือใช้ database table ร่วมกันตรง ๆ
business rule ไม่ผูกกับ UI, transport, ORM หรือ vendor SDK
domain object รับ Request/Response หรือ ORM session เข้ามา
ประกอบ dependency ที่ entry point และมอง lifecycle ได้ชัด
สร้าง client หรืออ่าน environment แบบกระจายทั่ว codebase
02React Frontend Architecture
Feature-based Structure
React ควรแบ่งตาม user capability มากกว่ารวม components, hooks และ services ของทุก feature ไว้คนละกอง หน้า route เป็น composition layer ที่นำ feature มาประกอบ ส่วน design-system component และ utility ที่ไม่มี business meaning จึงอยู่ใน shared
src/
├── app/ # routing, providers, composition root
├── features/
│ └── checkout/
│ ├── api/ # request + response mapping
│ ├── components/ # UI ของ checkout
│ ├── hooks/ # orchestration ของ use case ฝั่ง UI
│ ├── model/ # types, schema, pure business rules
│ └── index.ts # public API ของ feature
├── shared/
│ ├── ui/ # Button, Dialog, FormField
│ └── lib/ # framework-agnostic utilities
└── main.tsx
# dependency direction
app → features → shared
feature A ✕ import ไฟล์ภายในของ feature BComponent ควรรับข้อมูลผ่าน props ที่แคบและ render ได้จากข้อมูลนั้น orchestration เช่น fetch, mutation, navigation และ analytics อยู่ใน route หรือ feature hook อย่าให้ component ระดับ shared import store, API client หรือ business feature กลับเข้ามา
State และ Data Boundaries
component ที่ใช้งาน หรือ parent ร่วมที่ใกล้ที่สุด
dialog open, input draft, selected tab
router เพื่อให้ bookmark, share และ back/forward ได้
search, filter, sort, pagination
data-fetching/cache layer ไม่คัดลอกลง global store โดยไม่จำเป็น
orders, profile, permissions จาก API
store/context เฉพาะข้อมูลที่มี owner และ lifecycle ชัด
session view, cart draft, multi-step workflow
เก็บ state ให้น้อยที่สุด ค่าที่คำนวณจาก props หรือ state อื่นได้ควร derive ตอน render เพื่อหลีกเลี่ยงข้อมูลสองชุดไม่ตรงกัน และยก state ขึ้นเพียงถึง common owner ที่ต้องใช้จริง
อ่านเอกสารทางการ: React — Thinking in React ↗03Go Backend Architecture
ใน Go ให้ package เป็น boundary ที่มีความหมาย ใช้ cmd สำหรับ executable และ composition root ส่วน server logic ที่ไม่ต้องการ expose ให้ module อื่นอยู่ใน internal จากนั้นแบ่ง package ตาม capability ไม่ใช่สร้าง package models, utils และ services ขนาดใหญ่ร่วมกันทั้งระบบ
cmd/api/main.go # wiring, config, lifecycle
internal/
order/
model.go # entity + invariant
service.go # use cases
repository.go # port ที่ consumer ต้องการ
http.go # transport adapter
postgres.go # persistence adapter
service_test.go
platform/
database/
telemetry/
// dependency direction ภายใน feature
http → service → model
postgres ─implements→ repository port- ประกาศ interface ใกล้ฝั่ง consumer และรับ interface เท่าที่ use case ต้องใช้ ไม่สร้าง interface ให้ทุก struct
- ส่ง
context.Contextจาก transport ผ่าน call chain แต่ไม่เก็บ context ไว้ใน struct หรือ domain entity - แปลง HTTP/DB model ที่ boundary และรักษา domain error ให้ไม่ผูกกับ status code หรือ driver error
04Node.js Backend Architecture
Node.js เหมาะกับ feature module ที่รวม route, use case และ adapter ของ capability เดียวกัน TypeScript ช่วยบังคับ contract ตอน build แต่ runtime input ยังต้อง validate ที่ HTTP, queue และ database boundary เสมอ ไม่ควรปล่อย DTO หรือ ORM entity ไหลเป็น domain model ทั้งระบบ
src/
├── app.ts # composition root
├── modules/
│ └── orders/
│ ├── domain/ # entity + pure rules
│ ├── application/ # use cases + ports
│ ├── infrastructure/ # DB, queue, third-party adapters
│ ├── http/ # route, validation, presenter
│ └── index.ts # exported surface
└── platform/ # config, logger, telemetry
request → route → use case → port → adapter
↘ domain rules// application/create-order.ts
export interface OrderRepository {
save(order: Order): Promise<void>;
}
export class CreateOrder {
constructor(private readonly orders: OrderRepository) {}
async execute(input: CreateOrderInput) {
const order = Order.create(input); // pure domain rule
await this.orders.save(order); // port, ไม่รู้จัก ORM
return { id: order.id };
}
}ใช้ entry point เช่น index.ts หรือ package exports เป็น public surface เพื่อป้องกัน deep import และทำให้ refactor ภายใน module ได้ ส่วน controller รับผิดชอบ validation, authentication context และ mapping response—ไม่ใส่ transaction workflow หรือ business rule หลักไว้ใน controller
05Python Backend Architecture
Python ใช้ package เป็น boundary และวาง application code ใต้src/ เพื่อให้ test import package ที่ติดตั้งจริง แยก pure domain ออกจาก FastAPI/Django schema และ ORM model เมื่อ business rule มีน้ำหนัก แต่สำหรับ CRUD เล็กสามารถรวมไฟล์ให้ตรงไปตรงมาได้
pyproject.toml
src/app/
├── main.py # composition root
├── orders/
│ ├── domain.py # entities, value objects, rules
│ ├── service.py # application use cases
│ ├── ports.py # Protocol / abstract contracts
│ ├── api.py # HTTP boundary + schemas
│ ├── repository.py # SQL/ORM adapter
│ └── tests/
└── platform/
├── config.py
└── telemetry.py
api → service → domain
repository ─implements→ ports- ใช้
Protocolหรือ abstract contract ที่ application ต้องการ แล้ว inject adapter ตอน startup - ให้ transaction boundary ครอบหนึ่ง use case และจัดการ commit/rollback ที่ application หรือ unit-of-work layer
- แปลง Pydantic/HTTP schema, ORM row และ domain object ที่ boundary เพื่อลด coupling และป้องกัน accidental lazy load
06Testing Boundary และการบังคับ Architecture
invariant, calculation และ state transition
ไม่ต้อง mock framework หรือ database
workflow, authorization และ error path
fake เฉพาะ port เช่น repository หรือ gateway
SQL mapping, HTTP serialization และ queue contract
ใช้ dependency จริงผ่าน test container/server เมื่อคุ้มค่า
critical user journey ผ่าน deployable system
mock เฉพาะระบบภายนอกที่ควบคุมไม่ได้
Boundary ที่มีแต่เอกสารมักเสื่อมตามเวลา ให้เพิ่ม lint rule เช่นห้าม shared import feature, dependency test สำหรับ module graph และตรวจ public API ใน CI แต่หลีกเลี่ยงกฎละเอียดระดับไฟล์จนย้ายโค้ดธรรมดาไม่ได้
07เลือก Architecture ให้พอดีกับระบบ
route → service → data access แบบบาง
เริ่มมี rule ซ้ำ, test ยาก หรือเปลี่ยน adapter บ่อย
feature-based modular monolith + explicit contracts
domain ซับซ้อนจึงเพิ่ม domain layer และ ports
bounded modules พร้อม ownership และ observability
แยก service เมื่อ release cadence, scale หรือ isolation ต้องแยกจริง