DTOs مقابل نماذج النطاق
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
keep the API shape separate from the domain.
الموضوعات المخطط لها
- A DTO (data transfer object) is the shape of a request or response.
- The domain model holds business data and rules; it may have fields clients must never see.
- If you serialize the domain object directly, any internal change breaks API clients.
- Use different DTOs for create, update, and response.
- Map in one place with a mapper (manual method or a library).
تفاصيل المخطط المبدئي
عرض عملي
record ProductResponse(long id, String name, BigDecimal price) {}
class Product { long id; String name; BigDecimal priceNet; int costCents; /* internal */ }اختبر فهمك
Why not return Product directly?
أظهر الإجابة
It may expose internal fields and ties the API to internals.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.