أساليب تحويل DTO
في هذا الدرس 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.
تفاصيل المخطط المبدئي
عرض عملي
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.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.