握持网络呼叫后提供依赖性

发布于 2025-01-24 09:04:17 字数 358 浏览 0 评论 0原文

我需要为店面API提供图形客户端,但是在进行网络调用后,我只能构建客户端。

@Provides
@Singleton
fun getGraphClient(context: Context, client: Client): GraphClient {
    return GraphClient.build(
        context = context,
        shopDomain = client.shopifyDomain,
        accessToken = client.storefrontAccessToken
    )
}

在进行网络调用以使客户端将其传递给刀片以返回图表客户端后,该如何?

I need to provide a graph client for Storefront API but I can only build the client after making a network call.

@Provides
@Singleton
fun getGraphClient(context: Context, client: Client): GraphClient {
    return GraphClient.build(
        context = context,
        shopDomain = client.shopifyDomain,
        accessToken = client.storefrontAccessToken
    )
}

How can I after making a network call to get the client pass it to hilt to return the graph client?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恰似旧人归 2025-01-31 09:04:18

我通过这样的手动依赖注入解决了此问题:


@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    var graphClient: GraphClient? = null

    fun provideGraphClient(context: Context, client: Client) {
        graphClient = GraphClient.build(
            context = context,
            shopDomain = client.shopifyDomain,
            accessToken = client.storefrontAccessToken
        )
    }
}

然后在API调用后将

AppModule.provideGraphClient(context, client)

其注入到任何此类存储库中:

class SomeRepositoryImpl @Inject constructor(
    @ApplicationContext val applicationContext: Context,
    private val graphClient: GraphClient? = AppModule.graphClient
) : SomeRepository {

....

}

I solved this by manual dependency injection like this:


@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    var graphClient: GraphClient? = null

    fun provideGraphClient(context: Context, client: Client) {
        graphClient = GraphClient.build(
            context = context,
            shopDomain = client.shopifyDomain,
            accessToken = client.storefrontAccessToken
        )
    }
}

And then after the API call

AppModule.provideGraphClient(context, client)

And inject into any repository like this:

class SomeRepositoryImpl @Inject constructor(
    @ApplicationContext val applicationContext: Context,
    private val graphClient: GraphClient? = AppModule.graphClient
) : SomeRepository {

....

}
暮光沉寂 2025-01-31 09:04:18

您的解决方案根本不是剑术方法。您可以在没有@Module接口的情况下执行此操作。您应该像以前一样提供图形点,并注入以下内容:

   class SomeRepositoryImpl @Inject constructor(
    @ApplicationContext val applicationContext: Context,
    private lateinit val graphClient: GraphClient
  ) : SomeRepository {

  ....

  }

如果您不熟悉Kotlin“ LateInit”,那么您会发现参考文献直到第一次使用某个地方才能实例化。
我添加了LateInit关键字,并修改了一些您提到的代码段。
请让我知道这是否有效,如果您不觉得它有帮助 - 请在评论中告诉我。我一定会唯一您的刀柄问题。

因为您的帖子年纪大了,您可能已经知道一种更好的方法。但是我只是不想让任何人感到困惑,因为您的解决方案不是实际的剑术方式。因此,我认为我应该指导您获得专家​​在生产代码方面的解决方案。

愉快的编码!

Your solution is not Hilt approach at all. You could do it without @Module interface. You should provide the GraphClient like you did before and inject like following:

   class SomeRepositoryImpl @Inject constructor(
    @ApplicationContext val applicationContext: Context,
    private lateinit val graphClient: GraphClient
  ) : SomeRepository {

  ....

  }

if you are not familiar with kotlin "lateinit" then you will find out the reference does not instantiate until it's used somewhere for the first time.
I added lateinit keyword and modified a little of your mentioned code snippet.
Please let me know if this works, If you don't find it helpful - just let me know in comments. I will definitely sole your hilt issues.

As your post is much older and you may already know a better approach. But I just don't want anyone confused others that your solution is not actual Hilt way. So I thought i should guide you to the solution an expert do in production codes.

Happy coding!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文