我应该将MVVM的哪一部分存储在数据库中

时间:2019-04-18 12:10:11

标签: android android-mvvm

我只是了解MVVM和注入,我想知道应该在数据库中存储数据的结构的哪一部分

在kotlin中mvvm的简单实现是:

QuotesActivity

class QuotesActivity : AppCompatActivity(), KodeinAware {
    override val kodein by closestKodein()
    private val viewModelFactory: QuotesViewModelFactory by instance()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_quotes)
        initializeUi()
    }

    private fun initializeUi() {
        val viewModel = ViewModelProviders.of(this, viewModelFactory)
                .get(QuotesViewModel::class.java)

        viewModel.getQuotes().observe(this, Observer { quotes ->
            ..
            textView_quotes.text = stringBuilder.toString()
        })

        // When button is clicked, instantiate a Quote and add it to DB through the ViewModel
        button_add_quote.setOnClickListener {
            ..
        }
    }
}

QuotesViewModel

class QuotesViewModel(private val quoteRepository: QuoteRepository)
    : ViewModel() {

    fun addQuote(quote: Quote) = quoteRepository.addQuote(quote)

    fun getQuotes() = quoteRepository.getQuotes()
}

QuotesViewModelFactory

class QuotesViewModelFactory(private val quoteRepository: QuoteRepository)
    : ViewModelProvider.NewInstanceFactory() {

    @Suppress("UNCHECKED_CAST")
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return QuotesViewModel(quoteRepository) as T
    }
}

Database

interface Database {
    val quoteDao: QuoteDao
}

DatabaseFakeImpl

class DatabaseFakeImpl : Database {
    override val quoteDao: QuoteDao = QuoteDaoFakeImpl()
}

QuoteDao界面

interface QuoteDao {
    fun addQuote(quote: Quote)
    fun getQuotes(): LiveData<List<Quote>>
}

QuoteDaoFakeImpl类:

class QuoteDaoFakeImpl : QuoteDao {
    private val quoteList = mutableListOf<Quote>()
    private val quotes = MutableLiveData<List<Quote>>()

    init {
        quotes.value = quoteList
    }

    override fun addQuote(quote: Quote) {
        quoteList.add(quote)
        quotes.value = quoteList
    }

    override fun getQuotes() = quotes as LiveData<List<Quote>>
}

Quote模型类

data class Quote(val text: String, val author: String) {
    override fun toString(): String {
        return "$text - $author"
    }
}

QuoteRepository

interface QuoteRepository {
    fun addQuote(quote: Quote)
    fun getQuotes(): LiveData<List<Quote>>
}

QuoteRepositoryImpl

class QuoteRepositoryImpl(private val quoteDao: QuoteDao)
    : QuoteRepository {
    override fun addQuote(quote: Quote) {
        quoteDao.addQuote(quote)
    }

    override fun getQuotes() = quoteDao.getQuotes()
}

我认为应该在QuoteDaoFakeImpl类(例如简单的middleware)上实现:

class QuoteDaoFakeImpl : QuoteDao {
    ...
    override fun addQuote(quote: Quote) {
        quoteList.add(quote)
        quotes.value = quoteList

        /* IN THIS PART OF CODE */
    }

    override fun getQuotes() = quotes as LiveData<List<Quote>>
}

2 个答案:

答案 0 :(得分:0)

主要是ViewModel。因为是负责检索和显示数据的层。它还可以保存和加载它。

但是,甚至更好的实现方式是将数据库操作分离到单独的逻辑层,在将数据推送到ViewModel之前,可以在其中检索和保存数据。

答案 1 :(得分:0)

您应该将数据存储在存储库中。它应该查询并插入到Room等数据源中。