Hadi is a whole Kotlin container, which you can use very simple on JVM or Android. You can define all the instance that you will need in your app and manage them in a single place.
In your app gradle, you should add the repository:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
and the dependency:
implementation 'com.github.Yesferal:HornsApp-DI:${version}'
implementation 'com.github.Yesferal:HornsApp-Android-DI:${version}'
Then, you should instance Hadi Container in your main class, so any class could access it without any problem.
val container: Container = Hadi()
In case of Android, you should implement HadiApp interface in your Application class.
class MyApp: Application(), HadiApp {
override val container: Container = Hadi()
}
So, you can use the kotlin extensions in any Activity or Fragment:
hadi().resolve<MainContract.ActionListener>()
You could check more about this module in its own Github repository.
Some times the code is better than words, to register any dependency you just need:
container register Factory<String> { "Message: Hadi Container is our first option !" }
container register Singleton<MainRepository> {
MainRepository(message = container.resolve())
}
class MainRepository(private val message: String) {
fun getMessage() = message
}
To resolve any dependency you just need:
private val mainRepository: MainRepository = container.resolve()
To register a dependency using a Tag you just need to define the tag as a string. You can use this feature if you have dependencies that implement the same class, for example String:
container register Factory<String>(tag = "Title") { "Title: Hadi Container" }
container register Factory<String>(tag = "Description") { "Description: This is a demo app, which implement Hadi Container. This strings are injected by Hadi using a Tag, in order to Kadi know which one to use in each case." }
container register Singleton<MainRepository> {
MainRepository(
message = container.resolve(tag = "Title"),
description = container.resolve(tag = "Description")
)
}
container register Factory<MainContract.ActionListener> {
MainPresenter(mainRepository = container.resolve())
}
To resolve a dependency using a Tag you just need to specify the tag that you used to register it:
val message: String = container.resolve(tag = "Title")
val description: String = container.resolve(tag = "Description")
To register a dependency with parameters you just need to define the parameter structure as name: Type
.
You can use this feature if you have dependencies that need some parameters obtained at runtime:
container register Factory(tag = "Parameter") { (string: String) ->
string
}
To resolve a dependency with parameters you just need to specify them inside the Parameters
's constructor:
val description: String = container.resolve(params = Parameters("Description: Inserting value as Parameter"))
Now you're ready to include & use Hadi library into your project!
In this Repository you have an Android Demo that explain how to use it.
Copyright 2020 HornsApp Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.