تعريف واجهة Repository
كشف احتياجات البيانات دون تسريب تفاصيل HTTP
تعريف واجهة Repository درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What a Repository Is
A repository is the single doorway your apps walk through to get data, hiding where that data actually comes from.
Start With an Interface
Define the repository as a Kotlin interface first, so callers depend on a contract instead of a concrete class.
interface UserRepository {
suspend fun getUser(id: String): User
}Speak in Domain Terms
Name methods after what the app needs, like getUser, not after how you fetch it. The intent stays clear and stable.
Return Domain Models
The interface returns clean domain models, never raw JSON or HTTP responses, so the UI stays simple.
suspend fun getUsers(): List<User>No HTTP Words Allowed
Keep words like Ktor, headers, and status codes out of the interface. Callers should never know HTTP exists.
Suspend for Async Work
Mark data methods as suspend so they can do async work without blocking, and fit naturally into coroutines on both apps.
suspend fun refresh()It Lives in commonMain
Put the interface in commonMain so Android and iOS share the exact same data contract with zero duplication.
Many Implementations Possible
One interface can have several implementations: a real network one and a fake one for tests, swapped freely.
Easier to Test
Because callers depend on the interface, you can inject a fake repository in tests and avoid hitting the real network.
class FakeUserRepository : UserRepositoryClear, Focused Methods
Keep each method small and purposeful. A focused API is easier to read, mock, and evolve over time.
A Complete Example
Here is a tidy repository contract that exposes everything the user feature needs and nothing more.
interface UserRepository {
suspend fun getUsers(): List<User>
suspend fun getUser(id: String): User
}Quick Check
Where should the repository interface live in a KMP project?
Recap
A repository interface in commonMain defines what data the app needs using suspend methods and domain models, hiding all HTTP details.
الأسئلة الشائعة
هل درس «تعريف واجهة Repository» مجاني؟
نعم — نص درس «تعريف واجهة Repository» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «تعريف واجهة Repository»؟
كشف احتياجات البيانات دون تسريب تفاصيل HTTP تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «تعريف واجهة Repository»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تعريف واجهة Repository
- وضع API خلف Repository
- تحويل DTOs إلى نماذج النطاق
- كشف النتائج على هيئة Flow