iT邦幫忙

2025 iThome 鐵人賽

DAY 21
1

領域層最簡單的說法,就是這個類別只能引用 Kotlin,所以不能在 import 看到 Android 的東西

例如

import android.content.Context

AI 寫扣經過

  1. 建立 Note,用來儲存筆記
    data class Note(
        val id: Long = 0,
        val title: String,
        val content: String,
        val categoryId: Long,
        val tags: List<String>,
        val createdAt: Long,
        val updatedAt: Long
    )
    
  2. 建立筆記的分類,用來分類筆記
    data class Category(
        val id: Long = 0,
        val name: String,
        val sortOrder: Int
    )
    
  3. 建立介面,用來管理筆記操作。介面這種東西,最萬用的說明就是搖控器,你不需要知道按下音量鍵大聲的邏輯和操作,只要知道他會大聲就好
    interface NoteRepository {
    
        fun getAllNotes(): Flow<List<Note>>
    
        fun getAllCategories(): Flow<List<Category>>
    
        suspend fun getNoteById(id: Long): Note?
    
        suspend fun saveNote(note: Note)
    
        suspend fun deleteNote(note: Note)
    
        suspend fun saveCategory(category: Category)
    
        suspend fun deleteCategory(category: Category)
    }
    
  4. 建立 CategoryEntity 和 Category 的轉換器
    fun NoteEntity.toDomain(): Note {
        return Note(
            id = this.id,
            title = this.title,
            content = this.content,
            categoryId = this.categoryId,
            tags = this.tags,
            createdAt = this.createdAt,
            updatedAt = this.updatedAt
        )
    }
    
    fun Note.toEntity(): NoteEntity {
        return NoteEntity(
            id = this.id,
            title = this.title,
            content = this.content,
            categoryId = this.categoryId,
            tags = this.tags,
            createdAt = this.createdAt,
            updatedAt = this.updatedAt
        )
    }
    
    fun CategoryEntity.toDomain(): Category {
        return Category(
            id = this.id,
            name = this.name,
            sortOrder = this.sortOrder
        )
    }
    
    fun Category.toEntity(): CategoryEntity {
        return CategoryEntity(
            id = this.id,
            name = this.name,
            sortOrder = this.sortOrder
        )
    }
    
  5. 建立 NoteRepositoryImpl,透過 Hilt 注入 NoteDao 和 CategoryDao,實現 NoteRepository 介面
    class NoteRepositoryImpl @Inject constructor(
        private val noteDao: NoteDao,
        private val categoryDao: CategoryDao
    ) : NoteRepository {    
    
        override fun getAllNotes(): Flow<List<Note>> {
            return noteDao.getAllNotes().map { entities ->
                entities.map { it.toDomain() }
            }
        }   
    
        override fun getAllCategories(): Flow<List<Category>> {
            return categoryDao.getAllCategories().map { entities ->
                entities.map { it.toDomain() }
            }
        }   
    
        override suspend fun getNoteById(id: Long): Note? {
            return noteDao.getNoteById(id)?.toDomain()
        }   
    
        override suspend fun saveNote(note: Note) {
            noteDao.insertNote(note.toEntity())
        }   
    
        override suspend fun deleteNote(note: Note) {
            noteDao.deleteNote(note.toEntity())
        }   
    
        override suspend fun saveCategory(category: Category) {
            categoryDao.insertCategory(category.toEntity())
        }   
    
        override suspend fun deleteCategory(category: Category) {
            categoryDao.deleteCategory(category.toEntity())
        }
    }
    
  6. 建立 RepositoryModule 處理綁定的問題
    @Module
    @InstallIn(SingletonComponent::class)
    abstract class RepositoryModule {
    
        @Binds
        @Singleton
        abstract fun bindNoteRepository(
            noteRepositoryImpl: NoteRepositoryImpl
        ): NoteRepository
    }
    

上一篇
114/20 - Vibe Coding 建立本地資料庫
下一篇
114/22 - Vibe Coding 建立 use case
系列文
看見筆記捲土重來,試著用 Vibe Coding 完成一款 App 吧!25
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

1
chiaominchang222
iT邦新手 5 級 ‧ 2025-10-05 14:52:52

大聲起來XD

我要留言

立即登入留言