انتقل إلى المحتوى
أمين أبوزيد
درس مخطط لهJNR-020-L02

أساليب تحويل DTO

  • الدرس 2 من 6
  • Junior
في هذا الدرس 4 موضوعات

هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.

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

في هذا الدرس

convert between entities and DTOs and choose how.

الموضوعات المخطط لها

  • A DTO (data transfer object) is the shape sent over the API. An entity is the internal model.
  • Keeping them separate lets you change one without breaking the other and hides internal fields.
  • Plain code: a toResponse() method or a static mapper. Simple, explicit, easy to debug.
  • Mapper library (for example MapStruct): generates mapping code from interfaces; less typing for large models, with a learning cost.
  • For small models prefer plain code; for many similar mappings a library pays off. Be ready to justify.
تفاصيل المخطط المبدئي

عرض عملي

java
record ProductResponse(long id, String name, long priceCents) {
    static ProductResponse from(Product p) {
        return new ProductResponse(p.getId(), p.getName(), p.getPriceCents());
    }
}

اختبر فهمك

Why not return the entity directly?

أظهر الإجابة

It exposes internal fields and ties the API to the model.

التطبيق

طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.

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

تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.

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