التجمّعات والثوابت (Invariants)
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
design aggregates around the rules they protect.
الموضوعات المخطط لها
- An invariant is a rule that must always hold, such as "order total equals the sum of its lines".
- An aggregate is a cluster of objects changed as one unit, with a root (
Order) as the only entry point. - Keep aggregates small; one transaction changes one aggregate.
- Reference other aggregates by ID, not by object reference (
CustomerId, notCustomer). - Design around invariants, not around tables. Rules across aggregates are eventually consistent (MID-030).
تفاصيل المخطط المبدئي
عرض عملي
public class Order { // aggregate root
private final OrderId id;
private final CustomerId customerId; // by ID
private final List<OrderLine> lines = new ArrayList<>();
private Status status = Status.NEW;
public void addLine(ProductId p, int qty, Money price) {
if (status != Status.NEW) throw new IllegalStateException("order not editable");
lines.add(new OrderLine(p, qty, price));
}
}اختبر فهمك
How should Order refer to its customer?
أظهر الإجابة
By CustomerId.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.