在 kotlin 中从干净的架构中定义用例的惯用方法

Bol*_*usz 2 android kotlin clean-architecture kotlinx.coroutines

我尝试将我的用例类从 Java 切换到 Kotlin,从 Rxjava 切换到协程。

这是我的GetTopSongs课。

class GetTopSongs {

    suspend fun execute(limit:Int):Either<List<Song>>{
        //... do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否有比getTopSongs.execute(10)Kotlin更好的方法?

Sae*_*umi 5

使用运算符重载

class GetTopSongs {

    suspend operator fun invoke(limit:Int):Either<List<Song>>{
        //... do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

所以你可以忽略额外的.execute调用:

val getTopSongs = GetTopSongs()
val result = getTopSongs(10)
Run Code Online (Sandbox Code Playgroud)