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

البنية متعددة الطبقات (Layered architecture)

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

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

نظرة عامة

الموضوعات

  • Controller
  • Service
  • Repository
  • DTO
  • نماذج النطاق/الكيانات (Domain/Entity models)

تعلّم أولًا

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

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

  1. Assign responsibilities to controller, service, repository, and domain layers.
  2. Separate API DTOs from domain/entity models and map between them.
  3. Organize code by feature with layers inside each feature.
  4. Write a minimal @RestController that delegates to a service.

الدروس

  1. 01لماذا الطبقات
  2. 02الـ Controllers: حافة HTTP
  3. 03الـ Services: منطق العمل والتنسيق
  4. 04الـ Repositories: الوصول إلى البيانات خلف واجهة
  5. 05DTOs مقابل نماذج النطاق
  6. 06التنظيم حسب الميزة، والطبقات داخلها
  7. 07عرض عملي: بناء ميزة المنتجات طبقة بطبقة
  8. 08عرض عملي: فصل DTOs عن نماذج النطاق

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

Lab

JNR-013-LAB1 · Products feature

Outcomes: 1–4 · Time: ~4 h

  1. In package com.shop.api.products, create Product, ProductRepository, InMemoryProductRepository, ProductService, ProductController.
  2. Implement GET /products and GET /products/{id}.
  3. Return ProductResponse DTOs through a mapper; never the domain class.
  4. Throw a not-found exception from the service for an unknown id (the HTTP mapping comes in JNR-015).

Acceptance: the controller contains no business logic; domain objects are never serialized directly; the service has no HTTP types.

Exercises (assessment: layering review of a provided feature)

IDOutcomeTask
JNR-013-EX011Sort these into layers: parse JSON, check stock, find by id, set HTTP 404, compute total with tax.
JNR-013-EX021Review a provided "fat controller" and list five violations.
JNR-013-EX032Write OrderResponse and a mapper from Order that hides internalNotes.
JNR-013-EX042Explain what breaks for clients if you rename a field of a serialized domain class.
JNR-013-EX053Reorganize a package-by-layer tree into package-by-feature.
JNR-013-EX064Write a @RestController with GET /customers/{id} that only delegates to a service.
JNR-013-EX073Decide: may orders use ProductRepository directly? Justify and write the alternative.
أظهر الحلول
  • EX01 Controller: parse JSON, set HTTP 404. Service: check stock, compute total with tax. Repository: find by id.
  • EX02 Typical violations: repository calls in the controller, business rules in it, a returned entity, try/catch and status logic, one DTO for everything.
  • EX03 record OrderResponse(long id, List<ItemResponse> items, BigDecimal total){} and static OrderResponse toResponse(Order o).
  • EX04 The JSON field name changes, so clients break; a DTO decouples this.
  • EX05 products/{ProductController, ProductService, ProductRepository, dto/} and the same for orders.
  • EX06 @RestController @RequestMapping("/customers") class CustomerController { ... @GetMapping("/{id}") CustomerResponse get(@PathVariable long id){ return service.get(id);} }
  • EX07 No: use ProductService (or a narrow interface) so the products feature controls its own data and rules.

الاختبار

Outcomes: O1 layer responsibilities · O2 DTOs vs domain · O3 package by feature · O4 minimal controller.

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

  1. 1Where do business rules belong?O1
  2. 2What is a controller's job?O1
  3. 3Which dependency direction is correct?O1
  4. 4A service method returning ResponseEntity is:O1
  5. 5A DTO is:O2
  6. 6Why not serialize domain objects directly?O2
  7. 7Using one DTO for create, update, and response is risky because:O2
  8. 8Package by feature means:O3
  9. 9Another feature needs product data. Best:O3
  10. 10A minimal REST controller:O4

الجزء العملي

Seven exercises JNR-013-EX01 to EX07 plus the lab. The assessment: review a provided feature's layering (EX02 is the template). Pass: quiz at least 8/10, review finds at least four violations, lab acceptance met.

أخطاء شائعة

Fat controllers; services that return ResponseEntity; one DTO reused for everything.

التقييم

Layering review of a provided feature.

الصلة بالمسار الوظيفي

The "established architecture" the Junior gate asks the learner to work within; hexagonal architecture (MID-059).

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