question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question][HOW TO] separate scopes per logic entitiy

See original GitHub issue

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>

Koin scoping of feature modules

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
timrijckaertcommented, Nov 18, 2021

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.

Correct me if I’m wrong, but with the release of 3.1.3 it seems it is only possible to scope ViewModel instances, not arbitrary classes to a nav graph?

Also it seems that we can not pass any ParameterDefinition any more when using the koinNavGraphViewModel.

1reaction
arnaudgiulianicommented, Nov 18, 2021

it has been pushed in 3.1.3+. Any need to continue on it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

On The Nature of OAuth2's Scopes
OAuth2 says nothing about scopes being claims in access tokens, given that access tokens (ATs from now on) don't need to carry claims...
Read more >
OAuth Scopes Best Practices
Ensure that scopes in each component are easy to reason about, by keeping them high level. Data Models. APIs should separate data both...
Read more >
Multi-Tenant App Deployments with Scopes & Collections
Scopes and Collections are logical containers within a Couchbase Bucket. ... You can easily do so by mapping each tenant to a separate...
Read more >
Where to put restrictions on entities when separating ...
Another idea I had is to put it in the Add() method in the repository, but then I have my business logic right...
Read more >
Think logically about “logical separation”
Think logically about “logical separation”. During a carve-out, asset sale, or spin-off, there is often insufficient time or readiness to fully separate the....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found