简体   繁体   English

将过多的参数从 UI 传递到 UseCase Android Clean Architecture

[英]Pass too much params from UI to UseCase Android Clean Architecture

I am applying Clean Architecture, but actually still have many questions about it.我正在应用清洁架构,但实际上仍然有很多问题。

Suppose, my API @POST fun createJob(@Body request: JobRequest) .假设,我的 API @POST fun createJob(@Body request: JobRequest) And the params are provided from UI.并且参数是从 UI 提供的。 So,所以,

  1. In data : I have created a JobRequest like below:data中:我创建了一个JobRequest ,如下所示:
data class JobRequest(
    @Expose @SerializedName("p1") val p1: String?,
...
    @Expose @SerializedName("p7") val p7: String?
)
  1. In domain : I create a CreateJobUseCase like below:domain :我创建了一个CreateJobUseCase ,如下所示:

class CreateJobUseCase(private val userRepository: UserRepository) :
    BaseUseCase<CreateJobUseCase.Input, Job>() {

    override suspend fun execute(input: Input): Job {
        return userRepository.createJob(input.p1, ...input.p7)
    }

    data class Input(
       val p1: String,
       ....
       val p7: String,
   ) : BaseInput()
}
  1. In presentation : I will call from ViewModel like:presentation :我将从ViewModel调用,例如:
   createJobUseCase.execute(CreateJobUseCase.Input(p1, p2...p7)) {
     //handle output
   }

My question is:我的问题是:

In domain , should my UserRepository interface like A or B?domain ,我的UserRepository接口应该像 A 还是 B?

A: suspend fun createJob(input: CreateJobUseCase.Input) : Job

or

B: suspend fun createJob(p1: String,... p7: String) : Job

In many projects, I saw them use B , it seems the repository function is cleared.在许多项目中,我看到他们使用B ,似乎存储库 function 已清除。 But, I think A still ok, because it's still flow the dependency rule, and CreateJobUseCase.Input already contains what we need for createJob function.但是,我认为A仍然可以,因为它仍然遵循依赖规则,并且CreateJobUseCase.Input已经包含我们需要的createJob function。 Am I wrong?我错了吗?

It depends on what logic are you going to put inside your function.这取决于您要在 function 中放入什么逻辑。 Overall, I think you should choose whatever variant you like better and what fits your needs beter.总的来说,我认为您应该选择您更喜欢的任何变体以及更适合您需求的变体。

Also, if you're going to use same type variables I can propose another option C) .此外,如果您要使用相同类型的变量,我可以提出另一个选项C) Maybe vararg would fit your needs even better?也许vararg会更好地满足您的需求? At least function's constructor would be shorter and cleaner.至少函数的构造函数会更短更简洁。 Eg:例如:

suspend fun createJob(vararg p: String) {
    for (item in p) {
        println(item)
    }
}

And then you could call your function like this:然后你可以像这样调用你的 function :

createJob("1", "2", "3")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM