انتقل إلى المحتوى
أمين أبوزيد
MID-060

مقدمة في DDD

  • 7 درس
  • 3 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • الكيانات (Entities)
  • كائنات القيمة (Value Objects)
  • التجمّعات (Aggregates)
  • المستودعات (Repositories)
  • خدمات المجال (Domain Services)

تعلّم أولًا

3 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Distinguish entities and value objects and model each in Java.
  2. Design aggregates around invariants and keep them small, referencing other aggregates by ID.
  3. Use repositories per aggregate and domain services for logic that spans aggregates.
  4. Build a ubiquitous language with domain experts and reflect it in code.

الدروس

  1. 01لماذا التصميم الموجّه بالمجال (DDD)
  2. 02اللغة الموحّدة (Ubiquitous Language)
  3. 03الكيانات وكائنات القيمة
  4. 04التجمّعات والثوابت (Invariants)
  5. 05المستودعات وخدمات المجال
  6. 06أحداث المجال
  7. 07عرض عملي: إعادة تصميم تجمّع Order

المختبر والتمارين

Lab

MID-060-LAB1 · Order aggregate

Outcomes: 1–3 · Time: ~3 h

  1. Redesign Order with OrderId, Money, OrderLine; remove public setters.
  2. Enforce inside Order: status transitions (NEW → PLACED → PAID → SHIPPED), line totals, and cancellation rules (no cancel after shipping).
  3. Raise OrderPlaced and OrderCancelled events from the aggregate.
  4. Keep OrderRepository as a domain interface; services only orchestrate.
  5. 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)

IDOutcomeTask
MID-060-EX011Classify as entity or value object: Customer, Money, Address, Order, DateRange. Justify each.
MID-060-EX021Implement Email as a record that rejects invalid input, plus an equality test.
MID-060-EX032For a hotel booking domain, list the invariants and draw the aggregates; keep them small.
MID-060-EX042Find and fix the problem: class Order { Customer customer; List<Product> products; }.
MID-060-EX053Decide where logic goes: "apply a voucher across several orders", "an order cannot ship unpaid".
MID-060-EX064Run 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: Booking protects "check-out is after check-in" and "status flow"; "no overlapping bookings for a room" is a cross-aggregate rule, handled by a Room availability aggregate or a domain service. Keep Booking, Room, Guest separate, linked by IDs. Accept any design that ties aggregates to invariants.
  • EX04 Replace with CustomerId and List<OrderLine> holding ProductId, 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.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Which has an identity that persists as attributes change?O1
  2. 2Value objects should be:O1
  3. 3An invariant is:O2
  4. 4An aggregate should reference another aggregate by:O2
  5. 5One transaction should normally change:O2
  6. 6The best sign of an aggregate that is too big:O2
  7. 7A repository is provided for:O3
  8. 8Logic spanning several aggregates belongs in:O3
  9. 9The ubiquitous language is:O4
  10. 10An anemic domain model means:O2

الجزء العملي

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).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.