Dependency Injection Container
Atlas supports Compile time dependency injection.
To get started using it please import the plugin into your project (shared.gradle file):
_11plugins {_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:
_1AtlasDI.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:
_361. @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.
_13import com.architect.atlas.architecture.mvvm.ViewModel_13class MyService {_13_13}_13_13class MyViewModel: ViewModel() {_13_13}_13_13AtlasDI.registerInstance(MyService())_13AtlasDI.registerSingleton(MyService())_13AtlasDI.registerViewModel(MyViewModel())_13AtlasDI.registerFactory(MyService())
To access the service via the Atlas DSL:
_8_8@ViewModels_8class SampleViewModel: ViewModel(){_8_8}_8_8AtlasDI.resolveService<MyServiceComponent>()_8AtlasDI.resolveViewModel<SampleViewModel>()
To resolve the service/viewmodel lazily, please use:
_1AtlasDI.resolveLazyService<MyServiceComponent>()
This is equivalent to:
_3val 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:
_3class 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