نمط المستودع
نسّق بين مصادر البيانات بوضوح
نمط المستودع درس مجاني في Jetpack Compose Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Jetpack Compose Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Hide the Data Details
Your ViewModel should not know about DAOs or networks. A repository hides those details behind a clean, simple interface.
A Single Doorway to Data
The repository is the one place the rest of the app asks for data, whether it comes from Room, the network, or memory.
Wrap the DAO
Give the repository the DAO and let it expose friendly methods. Callers see notes, not SQL queries.
class NoteRepository(private val dao: NoteDao) {
val notes = dao.observeAll()
}Expose Actions, Not Tables
Add purposeful functions like addNote. The name says what happens; the storage mechanism stays hidden inside.
suspend fun addNote(text: String) = dao.insert(Note(text = text))Swap Sources Freely
Because callers depend on the repository, you can switch from Room to a fake or a remote API without touching the ViewModel.
Easier to Test
Tests can pass a fake repository to the ViewModel, so you verify logic without a real database on disk. ✅
class FakeRepo : NoteRepository { /* in-memory */ }Inject the Repository
The ViewModel receives the repository through its constructor, never creating it directly. That keeps dependencies explicit.
class NoteViewModel(private val repo: NoteRepository) : ViewModel()Combine Multiple Sources
A repository can merge cache and network, deciding what to return. The UI just asks once and stays unaware of the plumbing.
Pass Flows Straight Through
For reactive data the repository can simply forward the DAO Flow, adding transformations only when the app actually needs them.
val notes: Flow<List<Note>> = dao.observeAll()One Repository per Domain
Keep repositories focused: one for notes, one for users. Each owns a single area of data and its sources.
Clean Layers, Clear Roles
UI shows data, ViewModel holds state, repository fetches it, Room stores it. Each layer has one job and stays replaceable.
Quick Check
What is the main job of a repository in this architecture?
Recap: A Clean Middle Layer
You put a repository between the ViewModel and your data, exposing actions and Flows while hiding the source. Next you will make it work offline first.
الأسئلة الشائعة
هل درس «نمط المستودع» مجاني؟
نعم — نص درس «نمط المستودع» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Jetpack Compose Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
ماذا ستتعلم في «نمط المستودع»؟
نسّق بين مصادر البيانات بوضوح تتمرن على Jetpack Compose Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Jetpack Compose Academy؟
لا تُشترط خبرة سابقة. Jetpack Compose Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «نمط المستودع»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Jetpack Compose Academy هذا؟
نعم. كل درس في Jetpack Compose Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الكيانات وDAOs وقاعدة البيانات
- الاستعلامات التفاعلية التي تعيد Flow
- نمط المستودع
- استراتيجية التخزين المؤقت دون اتصال أولًا