การบังคับใช้กฎธุรกิจด้วยค่าคงสภาพ
เรียนรู้การเข้ารหัสกฎธุรกิจที่สำคัญให้เป็นค่าคงสภาพภายในเอนทิตี เพื่อให้โดเมนไม่สามารถเข้าสู่สถานะที่ไม่ถูกต้องได้
การบังคับใช้กฎธุรกิจด้วยค่าคงสภาพ เป็นบทเรียน Clean Architecture & Design Patterns in Practice ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Clean Architecture & Design Patterns in Practice และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Clean Architecture & Design Patterns in Practice มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Rules That Must Always Hold
Beyond use-case logic, a domain has rules that must be true at all times. An account balance never goes below zero; an order always has at least one line item.
These are invariants, and the core should make them impossible to violate.
Entity Invariants vs Use-Case Rules
Two layers of rules exist:
- Entity invariants: always true regardless of operation (Enterprise Business Rules).
- Use-case rules: govern a specific workflow (Application Business Rules).
Invariants belong inside the entity itself.
The Danger of Anemic Entities
An anemic entity is just public fields with no protection.
class Account {
public double balance; // anyone can set anything
}Guarding Construction
Enforce invariants when the entity is created. An invalid entity should never exist.
class Account {
private double balance;
Account(double initial) {
if (initial < 0) throw new IllegalArgumentException("balance < 0");
this.balance = initial;
}
}Guarding Mutation
Every state change must preserve the invariant. Expose intent-revealing methods, not setters.
void withdraw(double amount) {
if (amount <= 0) throw new IllegalArgumentException("amount");
if (amount > balance) throw new IllegalStateException("insufficient funds");
balance -= amount;
}Make Illegal States Unrepresentable
A stronger goal than checking: design types so invalid states cannot be expressed.
Use value objects, enums, and required constructor arguments so the compiler enforces what it can before runtime checks ever run.
Value Objects Carry Their Own Rules
Wrap primitives in small immutable types that validate themselves.
final class Email {
private final String value;
Email(String v) {
if (!v.contains("@")) throw new IllegalArgumentException("bad email");
this.value = v;
}
}A Runnable Demonstration
The entity rejects an invalid operation, protecting the invariant.
public class Main {
static class Account {
private double balance;
Account(double b){ if(b<0) throw new IllegalArgumentException(); balance=b; }
void withdraw(double a){ if(a>balance) throw new IllegalStateException("insufficient"); balance-=a; }
double getBalance(){ return balance; }
}
public static void main(String[] args){
Account acc = new Account(100);
acc.withdraw(30);
System.out.println("Balance: " + acc.getBalance());
try { acc.withdraw(1000); } catch(Exception e){ System.out.println("Blocked: " + e.getMessage()); }
}
}Keep Infrastructure Out
Invariant checks are pure domain logic. They must not reach for a database, network, or framework.
If validating a rule needs external data, that check belongs in a use case (interactor), not the entity.
Invariants and Testing
Because invariants live in pure entities, they are trivial to test: construct, call a method, assert the rule held or the operation was rejected.
No mocks, no database, no framework — fast and reliable tests.
Design Guidelines
- Validate on construction and on every mutation.
- Replace public setters with intent-revealing methods.
- Use immutable value objects for self-validating data.
- Keep all checks framework-free.
Quick Check
Test your understanding of invariants.
Recap
You learned to protect the domain with invariants.
- Entity invariants always hold; use-case rules govern workflows.
- Validate on creation and mutation; prefer unrepresentable illegal states.
- Keep checks pure for easy testing.
คำถามที่พบบ่อย
บทเรียน “การบังคับใช้กฎธุรกิจด้วยค่าคงสภาพ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การบังคับใช้กฎธุรกิจด้วยค่าคงสภาพ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Clean Architecture & Design Patterns in Practice ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Clean Architecture & Design Patterns in Practice มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การบังคับใช้กฎธุรกิจด้วยค่าคงสภาพ”
เรียนรู้การเข้ารหัสกฎธุรกิจที่สำคัญให้เป็นค่าคงสภาพภายในเอนทิตี เพื่อให้โดเมนไม่สามารถเข้าสู่สถานะที่ไม่ถูกต้องได้ คุณปฏิบัติ Clean Architecture & Design Patterns in Practice ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clean Architecture & Design Patterns in Practice หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clean Architecture & Design Patterns in Practice บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การบังคับใช้กฎธุรกิจด้วยค่าคงสภาพ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Clean Architecture & Design Patterns in Practice นี้ได้ไหม
ได้ บทเรียน Clean Architecture & Design Patterns in Practice ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การออกแบบเอนทิตีทางธุรกิจ
- การนำกรณีใช้งานมาใช้ (ตัวประสานงาน)
- พอร์ตข้อมูลเข้าและข้อมูลออก
- การบังคับใช้กฎธุรกิจด้วยค่าคงสภาพ