简体   繁体   English

将上下文传递给 android 中的域层(用例)是一种好习惯吗

[英]Is it good practice to pass the context to the domain layer (Use cases) in android

I have a usecase which will use the alarm manger to set a repeatingAlarms and the alarm manger requires a pendingIntent which also needs the context to create object from.我有一个用例,它将使用警报管理器来设置重复警报,并且警报管理器需要一个pendingIntent,它还需要上下文来创建 object。 Now my question is it a good approach to pass the context to the usecase?现在我的问题是将上下文传递给用例的好方法吗? if no then what's the solution?如果没有,那么解决方案是什么?

class SetReminderAlarmsUseCase(
    private val hydrateRepository: HydrateRepository,
    private val alarmManager: AlarmManager,
) {

    suspend operator fun invoke(
        wakeUpTime: String,
        sleepTime: String,
        drinkAmount: Int,
        target: Int,
    ) {
        val alarms = getAlarmsList(wakeUpTime, sleepTime, drinkAmount, target)
        insertAlarms(alarms)

        alarms.forEach { alarm ->
            alarmManager.setRepeating(
                AlarmManager.RTC_WAKEUP,
                getAlarmTime(alarm),
                AlarmManager.INTERVAL_DAY,
                pendingIntent
            )
        }
    }

Use cases are related to clean architecture用例与干净的架构有关

https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html

So as per the image - the Use Cases wrap your core business logic.因此,根据图像 - 用例包含您的核心业务逻辑。 It should be abstract like: "do some generic work".No concrete stuff like Context, AlarmManager, etc. As you can see - the Framework is on the outside.它应该是抽象的,比如:“做一些通用的工作”。没有像 Context、AlarmManager 等具体的东西。正如你所看到的 - 框架在外面。

But how do you call "inside stuff" from the outside when the "inside stuff" depends on the "outside stuff".但是当“内部的东西”取决于“外部的东西”时,你如何从外部调用“内部的东西”。

This is the central concept behind Clean Architecture: You use interfaces.这是清洁架构背后的核心概念:您使用接口。 Dependency inversion.依赖倒置。

In your case you need to have something in the "entities layer" that is called for example: ReminderManager.在您的情况下,您需要在“实体层”中有一些东西,例如:ReminderManager。 This ReminderManager should be clean Kotlin code.这个 ReminderManager 应该是干净的 Kotlin 代码。 Pure Kotlin code module.纯 Kotlin 代码模块。

So in the pure Kotlin Module, where the ReminderManager is located, you need to have some interface .所以在ReminderManager所在的纯Kotlin Module中,需要有一些接口

Then you implement the interface in the module where the Framework-related code is located.然后你在Framework相关代码所在的模块中实现接口 Whatever implements the interface - it needs to have both context and AlarmManager.无论实现接口- 它需要同时具有上下文和 AlarmManager。

So you see - in the end, the core logic depends on an interface that is located in the same module as the core logic .所以你看 - 最后,核心逻辑依赖于与核心逻辑位于同一模块中的接口 At the same time, the class that implements the interface is dependent on the interface located in the internal layer.同时,实现接口的class依赖于位于内部层的接口。 This is how you achieve inversion of control.这就是实现控制反转的方式。

I like this example of clean architecture where they use only 2 modules - pure Kotlin code and Framework code.我喜欢这个干净架构的例子,他们只使用 2 个模块——纯 Kotlin 代码和框架代码。

https://www.raywenderlich.com/3595916-clean-architecture-tutorial-for-android-getting-started https://www.raywenderlich.com/3595916-clean-architecture-tutorial-for-android-getting-started

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

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