Repositoryパターン
データソース間をきれいに仲介します。
「Repositoryパターン」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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.
よくある質問
「Repositoryパターン」レッスンは無料ですか?
はい。「Repositoryパターン」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「Repositoryパターン」で何を学びますか?
データソース間をきれいに仲介します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「Repositoryパターン」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- エンティティ、DAO、データベース
- Flowを返すリアクティブクエリ
- Repositoryパターン
- オフラインファーストのキャッシュ戦略