# Repository Pattern (Laravel) > Repository pattern (ข้อแลกเปลี่ยน, กรณีการใช้งาน), contracts, service layer, หลักการ SOLID, dependency inversion, ความสามารถในการทดสอบ, เมื่อใดที่ไม่ควรใช้ - 20 คำถามสัมภาษณ์ - Senior - [คำถามสัมภาษณ์: Laravel](https://sharpskill.dev/th/technologies/laravel/interview-questions.md) ## 1. Repository Pattern ใน Laravel คืออะไร? **คำตอบ** Repository Pattern เป็น design pattern ที่เพิ่มเลเยอร์การแยกส่วน (abstraction) ระหว่างลอจิกธุรกิจกับเลเยอร์ข้อมูล โดยห่อหุ้มลอจิกการเข้าถึงข้อมูลไว้ในคลาสเฉพาะ (repositories) ทำให้สามารถแยกความรับผิดชอบและทำให้โค้ดทดสอบได้ง่ายขึ้น ต่างจากการใช้ Eloquent โดยตรงใน controllers ตรงที่ repository รวมศูนย์ query ที่ซับซ้อนและทำให้เปลี่ยนแหล่งข้อมูลได้ง่ายโดยไม่กระทบส่วนอื่นของแอปพลิเคชัน ## 2. Repository Pattern ให้คุณค่ามากที่สุดในสถานการณ์ใด? **คำตอบ** Repository Pattern มีประโยชน์อย่างยิ่งในแอปพลิเคชันที่ซับซ้อนซึ่งมีคิวรีที่ซับซ้อนและแหล่งข้อมูลหลายแหล่ง มันโดดเด่นในโปรเจกต์ระดับองค์กรที่ตรรกะการเข้าถึงข้อมูลมีความซับซ้อน ถูกใช้ซ้ำในหลายที่ หรือมีแนวโน้มที่จะเปลี่ยนแปลง (เช่น การย้ายจาก MySQL ไปยัง MongoDB) สำหรับ CRUD ง่าย ๆ หรือแอปพลิเคชันขนาดเล็ก อาจกลายเป็นการออกแบบที่เกินความจำเป็นโดยไม่จำเป็น ## 3. trade-off หลักของ Repository Pattern คืออะไร? **คำตอบ** trade-off หลักคือความซับซ้อนของโค้ดที่เพิ่มขึ้นและจำนวนไฟล์ที่ต้องดูแลรักษา แต่ละ repository เพิ่มไฟล์อย่างน้อย 2 ไฟล์ (interface + การนำไปใช้) ซึ่งอาจกลายเป็นเยิ่นเย้อได้อย่างรวดเร็วในแอปพลิเคชันที่เรียบง่าย Laravel Eloquent เป็นการแยกนามธรรมที่ทรงพลังอยู่แล้วซึ่งแก้ไขกรณีการใช้งานได้ 80% และการเพิ่มเลเยอร์พิเศษอาจทำให้การพัฒนาช้าลงโดยไม่มีประโยชน์ที่แท้จริงหากคิวรียังคงเรียบง่าย ## มีอีก 17 คำถาม - Repository Pattern นำหลักการ SOLID Dependency Inversion ไปใช้อย่างไร? - ข้อได้เปรียบหลักของ Repository Pattern สำหรับการทดสอบคืออะไร? สมัครฟรี: https://sharpskill.dev/th/login ## หัวข้อสัมภาษณ์ Laravel อื่นๆ - [พื้นฐาน PHP](https://sharpskill.dev/th/technologies/laravel/interview-questions/php-basics.md): 25 คำถาม, Junior - [พื้นฐาน OOP ใน PHP](https://sharpskill.dev/th/technologies/laravel/interview-questions/php-oop-essentials.md): 20 คำถาม, Junior - [Composer & Autoloading](https://sharpskill.dev/th/technologies/laravel/interview-questions/composer-autoloading.md): 18 คำถาม, Junior - [พื้นฐาน Laravel](https://sharpskill.dev/th/technologies/laravel/interview-questions/laravel-fundamentals.md): 20 คำถาม, Junior - [Laravel Routing](https://sharpskill.dev/th/technologies/laravel/interview-questions/laravel-routing.md): 20 คำถาม, Junior - [Blade Templates](https://sharpskill.dev/th/technologies/laravel/interview-questions/blade-templates.md): 18 คำถาม, Junior - [Request & Response](https://sharpskill.dev/th/technologies/laravel/interview-questions/request-response.md): 20 คำถาม, Junior - [พื้นฐาน Eloquent ORM](https://sharpskill.dev/th/technologies/laravel/interview-questions/eloquent-orm-basics.md): 22 คำถาม, Junior - [Eloquent Relationships](https://sharpskill.dev/th/technologies/laravel/interview-questions/eloquent-relationships.md): 25 คำถาม, Mid-Level - [Migrations & Schema Builder](https://sharpskill.dev/th/technologies/laravel/interview-questions/database-migrations.md): 20 คำถาม, Mid-Level - [การตรวจสอบและฟอร์ม](https://sharpskill.dev/th/technologies/laravel/interview-questions/validation-forms.md): 22 คำถาม, Mid-Level - [การยืนยันตัวตน](https://sharpskill.dev/th/technologies/laravel/interview-questions/authentication.md): 20 คำถาม, Mid-Level - [การให้สิทธิ์และ Policies](https://sharpskill.dev/th/technologies/laravel/interview-questions/authorization-policies.md): 18 คำถาม, Mid-Level - [API Resources & Authentication](https://sharpskill.dev/th/technologies/laravel/interview-questions/api-resources-authentication.md): 26 คำถาม, Mid-Level - [Middleware](https://sharpskill.dev/th/technologies/laravel/interview-questions/middleware.md): 18 คำถาม, Mid-Level - [Service Container & DI](https://sharpskill.dev/th/technologies/laravel/interview-questions/service-container-di.md): 20 คำถาม, Mid-Level - [Queues & Jobs](https://sharpskill.dev/th/technologies/laravel/interview-questions/queues-jobs.md): 22 คำถาม, Mid-Level - [Events & Listeners](https://sharpskill.dev/th/technologies/laravel/interview-questions/events-listeners.md): 18 คำถาม, Mid-Level - [การแจ้งเตือนและเมล](https://sharpskill.dev/th/technologies/laravel/interview-questions/notifications-mail.md): 20 คำถาม, Mid-Level - [File Storage](https://sharpskill.dev/th/technologies/laravel/interview-questions/file-storage.md): 18 คำถาม, Mid-Level - [Testing & PHPUnit](https://sharpskill.dev/th/technologies/laravel/interview-questions/testing-phpunit.md): 24 คำถาม, Mid-Level - [Caching](https://sharpskill.dev/th/technologies/laravel/interview-questions/caching.md): 18 คำถาม, Mid-Level - [Livewire & Inertia](https://sharpskill.dev/th/technologies/laravel/interview-questions/livewire-inertia.md): 20 คำถาม, Mid-Level - [Eloquent Advanced](https://sharpskill.dev/th/technologies/laravel/interview-questions/eloquent-advanced.md): 24 คำถาม, Senior - [Laravel Packages](https://sharpskill.dev/th/technologies/laravel/interview-questions/laravel-packages.md): 20 คำถาม, Senior - [Performance Optimization](https://sharpskill.dev/th/technologies/laravel/interview-questions/performance-optimization.md): 22 คำถาม, Senior - [Security Best Practices](https://sharpskill.dev/th/technologies/laravel/interview-questions/security-best-practices.md): 22 คำถาม, Senior - [Laravel Octane](https://sharpskill.dev/th/technologies/laravel/interview-questions/laravel-octane.md): 18 คำถาม, Senior - [Laravel Distributed Systems](https://sharpskill.dev/th/technologies/laravel/interview-questions/laravel-distributed-systems.md): 22 คำถาม, Senior - [Observability & Monitoring](https://sharpskill.dev/th/technologies/laravel/interview-questions/observability-monitoring.md): 20 คำถาม, Senior - [Deployment & DevOps](https://sharpskill.dev/th/technologies/laravel/interview-questions/deployment-devops.md): 20 คำถาม, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/th/technologies/laravel/interview-questions/repository-pattern