Koin 共享范围为嵌套图的依赖项 [英] Koin share dependencies scoped to nested graph

查看:29
本文介绍了Koin 共享范围为嵌套图的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用 Koin 库正确确定依赖范围.

由于 Google 推荐了

应遵守以下规则:

  • 每一层代表一个范围.
  • 内层可以注入外层中定义的任何内容.
  • 没有重叠的层不应该能够注入彼此的依赖项.
  • 离开一个层应该处理它的依赖项.

更具体地说:

  • FeatureA 可以注入 Activity 和 App 依赖项,但不能注入 FeatureB 依赖项
  • FeatureB 可以注入 Activity 和 App 依赖项,但不能注入 FeatureA 依赖项

如何在 Koin 中实现这一点?

<块引用>

请注意,共享依赖项不仅限于 ViewModel.
我应该能够在我的范围内共享任何任意类.

解决方案

其实,我觉得你可以在你的特性模块和一些层模块中使用 loadModuleunLoadModulenetwork uiKit 和 ... 控制他们的需求.

FeatureA 可以注入 Activity 和 App 依赖,但不能注入 FeatureB 依赖

我认为在 FeatueA 中注入您的 appModule 是不合理的,您可以拥有一个 coreModule 并将其添加到您需要的所有模块中,您也可以使用 requireActivity 找到您的活动() 在您的fragmnet 中例如:D,因此 FeatureA 永远不能注入 FeatureB 依赖项,因为它们无法相互访问.

I'm wondering how to properly scope dependencies with Koin library.

Since Google recommends a single Activity architecture the AndroidX Navigation lib has become a key library to facilitate this by easily swapping Fragments.

A typical modern Android app has multiple features separated in packages and/or Gradle modules.
These feature modules provides a graph that can be used in the root graph as nested graphs. (See picture)

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_graph"
    app:startDestination="@id/mainFragment">

    <include app:graph="@navigation/nav_graph_feature_a" />
    <include app:graph="@navigation/nav_graph_feature_b" />

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.androidx_navigation.MainFragment"
        android:label="MainFragment"
        tools:layout="@layout/fragment_main">
        <action
            android:id="@+id/action_mainFragment_to_featureAFragment1"
            app:destination="@id/nav_graph_feature_a" />
        <action
            android:id="@+id/action_mainFragment_to_featureBFragment1"
            app:destination="@id/nav_graph_feature_b" />
    </fragment>
</navigation>

Following rules should be respected:

  • Every layer represent a scope.
  • Inner layers can inject anything defined in outer layers.
  • Layers that do not have an overlap should not be able to inject each other's dependencies.
  • Leaving a layer should dispose of its dependencies.

More concretely:

  • FeatureA can inject Activity and App dependencies, but not FeatureB dependencies
  • FeatureB can inject Activity and App dependencies, but not FeatureA dependencies

How to achieve this in Koin?

Note that sharing dependencies is not limited to ViewModel only.
I should be able to share any arbitrary class within my scope.

解决方案

Actually, I think you can use loadModule and unLoadModule in your feature Modules and with some layer modules like network uiKit and ... control their needs.

FeatureA can inject Activity and App dependencies, but not FeatureB dependencies

I think this is not reasonable that inject your appModule in the FeatueA, you can have a coreModule and add this in your all modules that needed, also you can find your activity with requireActivity() in your fragmnet for example:D, so the FeatureA never can inject FeatureB dependencies, because they have no access to each other.

这篇关于Koin 共享范围为嵌套图的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆