0Pricing
Jetpack Compose Academy · Lezione

Il pattern Repository

Intermedî in modo ordinato tra le fonti dati.

Il pattern Repository è una lezione Jetpack Compose Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Jetpack Compose Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Jetpack Compose Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Il pattern Repository» è gratuita?

Sì — il testo completo di «Il pattern Repository» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Jetpack Compose Academy, passa a CoddyKit PRO. Il corso Jetpack Compose Academy include 4 lezioni in totale.

Cosa imparerò in «Il pattern Repository»?

Intermedî in modo ordinato tra le fonti dati. Eserciti Jetpack Compose Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Jetpack Compose Academy?

Non è richiesta alcuna esperienza precedente. Jetpack Compose Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Il pattern Repository»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Jetpack Compose Academy?

Sì. Ogni lezione Jetpack Compose Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Entità, DAO e database
  2. Query reattive che restituiscono Flow
  3. Il pattern Repository
  4. Strategia di caching offline-first
← Torna a Jetpack Compose Academy