مقدمة في DDD
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- الكيانات (Entities)
- كائنات القيمة (Value Objects)
- التجمّعات (Aggregates)
- المستودعات (Repositories)
- خدمات المجال (Domain Services)
تعلّم أولًا
3 ساعة تدريب (تقدير)
مخرجات التعلم
- Distinguish entities and value objects and model each in Java.
- Design aggregates around invariants and keep them small, referencing other aggregates by ID.
- Use repositories per aggregate and domain services for logic that spans aggregates.
- Build a ubiquitous language with domain experts and reflect it in code.
الدروس
المختبر والتمارين
Lab
MID-060-LAB1 · Order aggregate
Outcomes: 1–3 · Time: ~3 h
- Redesign
OrderwithOrderId,Money,OrderLine; remove public setters. - Enforce inside
Order: status transitions (NEW → PLACED → PAID → SHIPPED), line totals, and cancellation rules (no cancel after shipping). - Raise
OrderPlacedandOrderCancelledevents from the aggregate. - Keep
OrderRepositoryas a domain interface; services only orchestrate. - Write unit tests per invariant; add a short glossary of the language used.
Acceptance: no invariant enforced in services; unit tests per invariant.
Exercises (assessment: aggregate design for an unseen domain; six exercises)
| ID | Outcome | Task |
|---|---|---|
| MID-060-EX01 | 1 | Classify as entity or value object: Customer, Money, Address, Order, DateRange. Justify each. |
| MID-060-EX02 | 1 | Implement Email as a record that rejects invalid input, plus an equality test. |
| MID-060-EX03 | 2 | For a hotel booking domain, list the invariants and draw the aggregates; keep them small. |
| MID-060-EX04 | 2 | Find and fix the problem: class Order { Customer customer; List<Product> products; }. |
| MID-060-EX05 | 3 | Decide where logic goes: "apply a voucher across several orders", "an order cannot ship unpaid". |
| MID-060-EX06 | 4 | Run a mini glossary session on a domain of your choice (6 terms) and rename five code identifiers to match. |
أظهر الحلول
- EX01 Entities:
Customer,Order(identity). Value objects:Money,Address,DateRange(equal values interchangeable). - EX02
record Email(String value) { Email { if (!value.matches(".+@.+\\..+")) throw new IllegalArgumentException(); } }; two instances with the same value are equal. - EX03 Example:
Bookingprotects "check-out is after check-in" and "status flow"; "no overlapping bookings for a room" is a cross-aggregate rule, handled by aRoomavailability aggregate or a domain service. KeepBooking,Room,Guestseparate, linked by IDs. Accept any design that ties aggregates to invariants. - EX04 Replace with
CustomerIdandList<OrderLine>holdingProductId, quantity, and unit price snapshot; aggregates reference others by ID. - EX05 Voucher across orders: domain service (spans aggregates). "Cannot ship unpaid": inside
Order.ship(). - EX06 Judged against the glossary; code names must match business terms.
الاختبار
Outcomes: O1 entities and value objects · O2 aggregates · O3 repositories and domain services · O4 ubiquitous language.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
Six exercises MID-060-EX01 to EX06 plus the lab. Pass: at least five exercises correct, and the lab's Order enforcing every invariant with a unit test each.
أخطاء شائعة
One giant aggregate; anemic models with setter-based "domain"; aggregates holding object references to other aggregates.
التقييم
Aggregate design for an unseen domain.
الصلة بالمسار الوظيفي
Strategic DDD (SRA-003).