Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

android - Dao in Usecase. MVVM or Clean Architecture anti-pattern?

In our "SearchUsecase" we have access to "ShowFtsDao" directly.

Does it violate the Clean Architecture principles? Does it violate the MVVM architecture?

Assuming our intention is to develop a well-built, standard structure, is there anything wrong with this piece of code?

class SearchUsecase @Inject constructor(
    private val searchRepository: SearchRepository,
    private val showFtsDao: ShowFtsDao,
    private val dispatchers: AppCoroutineDispatchers
) : SuspendingWorkInteractor<SearchShows.Params, List<ShowDetailed>>() {
    override suspend fun doWork(params: Params): List<ShowDetailed> {
        return withContext(dispatchers.io) {
            val remoteResults = searchRepository.search(params.query)
            if (remoteResults.isNotEmpty()) {
                remoteResults
            } else {
                when {
                    params.query.isNotBlank() -> showFtsDao.search("*$params.query*")
                    else -> emptyList()
                }
            }
        }
    }

    data class Params(val query: String)
}
question from:https://stackoverflow.com/questions/65873656/dao-in-usecase-mvvm-or-clean-architecture-anti-pattern

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I believe your use case handles more logic than it needs to. As a simple explanation I like to think about the components this way:

  • Sources: RemoteSource (networking), LocalSource (db), optionally MemorySource are an abstraction over your database and networking api and they do the IO thread switching & data mapping (which comes in handy on big projects, where the backend is not exactly mobile driven)
  • Repository: communicates with the sources, he is responsible for deciding where do you get the data from. I believe in your case if the RemoteSource returns empty data, then you get it from the LocalSource. (you can expose of course different methods like get() or fetch(), where the consumer specifies if it wants the latest data and based on that the repository calls the correct Source.
  • UseCases: Talk with multiple repositories and combine their data.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...