Skip to main content

Dependency Injection Container

Atlas supports Compile time dependency injection.

To get started using it please import the plugin into your project (shared.gradle file):


_11
plugins {
_11
id("io.github.thearchitect123.atlasGraphGenerator") // generates the dependency graph in your project
_11
}
_11
_11
kotlin {
_11
sourceSets {
_11
commonMain.dependencies {
_11
implementation("io.github.thearchitect123:atlas-core:+") // contains the DSL Api used to access the dependency graph
_11
}
_11
}
_11
}

Please also make sure to call this function inside your startup class before using Atlas:


_1
AtlasDI.injectContainer(AtlasContainer)

Register at Compile time - Annotations

To register dependencies for your compile time dependency graph, please use the below annotations. These will auto register your services into your dependency graph automatically and apply the behavior based on the annotations:


_36
1. @Singleton - Marks a class or function as a singleton, meaning that only one instance of it will be created and shared across the entire application.
_36
Use this when a service should have a single instance throughout the app lifecycle.
_36
_36
Example Usage:
_36
_36
@Singleton
_36
class MyServiceComponent {
_36
fun getString() = "My Sample String"
_36
}
_36
_36
//to use your service, please use the DSL Api.
_36
val result = AtlasDI.resolve<MyServiceComponent>().getString()
_36
_36
// result = "My Sample String"
_36
_36
2. @Factory - Marks a class or function as a factory, meaning a new instance is created every time it is requested.
_36
Use this when you need to regenerate a fresh instance each time.
_36
_36
3. @Scoped - Marks a class or function as a scoped instance, meaning the instance is tied to a specific scope ID.
_36
Use this when an instance should be shared within a specific scope (e.g., User Session, Request, Activity, etc.).
_36
_36
4. @ViewModels - Marks a class as a ViewModel, meaning it will be lazily initialized and tied to an Android/Fragment owner lifecycle.
_36
Use this for ViewModels in an MVVM architecture if you are using Jetpack Lifecycle Components.
_36
_36
On other platforms generates a single instance that is used once per class (example: ViewController for iOS)
_36
_36
5. @Module, @Provides - Marks a class as a dependency module, meaning it provides dependencies using @Provides.
_36
Use this when you need to define multiple dependencies inside one module.
_36
_36
@Module
_36
class MyServiceComponentModule {
_36
@Provides
_36
fun provideHttpClient(): HttpClient {
_36
return HttpClient()
_36
}
_36
}

Register at Runtime

If you need to register dependencies at runtime you can use the DSL Apis.


_13
import com.architect.atlas.architecture.mvvm.ViewModel
_13
class MyService {
_13
_13
}
_13
_13
class MyViewModel: ViewModel() {
_13
_13
}
_13
_13
AtlasDI.registerInstance(MyService())
_13
AtlasDI.registerSingleton(MyService())
_13
AtlasDI.registerViewModel(MyViewModel())
_13
AtlasDI.registerFactory(MyService())

To access the service via the Atlas DSL:


_8
_8
@ViewModels
_8
class SampleViewModel: ViewModel(){
_8
_8
}
_8
_8
AtlasDI.resolveService<MyServiceComponent>()
_8
AtlasDI.resolveViewModel<SampleViewModel>()

To resolve the service/viewmodel lazily, please use:


_1
AtlasDI.resolveLazyService<MyServiceComponent>()

This is equivalent to:


_3
val myService by lazy {
_3
AtlasDI.resolveService<MyServiceComponent>()
_3
}

What if I need access to Atlas in Swift directly?

No problem. Atlas has got you covered.

First inside your shared.gradle file please make sure to expose the Atlas API to swift


_15
listOf(iosX64, iosArm64, iosSimulatorArm64).forEach {
_15
it.binaries.framework {
_15
baseName = "shared"
_15
isStatic = true
_15
export(libs.atlas.core)
_15
}
_15
}
_15
_15
sourceSets {
_15
val commonMain by getting {
_15
dependencies {
_15
api(libs.atlas.core)
_15
}
_15
}
_15
}

And if you have a kotlin class in your multiplatform project:


_3
class SampleClass{
_3
_3
}

Then to resolve this dependency on the Swift side, you can use this snippet


_1
let resolved = AtlasDI.companion.resolveServiceNullableByName(clazz: SwiftClassGenerator.companion.getClazz(type: SampleClass.self)) as! SampleClass