{"id":25631046,"url":"https://github.com/ivianuu/injekt","last_synced_at":"2025-04-05T05:02:47.741Z","repository":{"id":43273274,"uuid":"163419495","full_name":"IVIanuu/injekt","owner":"IVIanuu","description":"Next gen dependency injection library for Kotlin [WIP]","archived":false,"fork":false,"pushed_at":"2025-04-02T22:47:00.000Z","size":15720,"stargazers_count":109,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T23:28:55.260Z","etag":null,"topics":["compile-time-dependency-injection","compiler-plugin","dependency-injection","kotlin","kotlin-compiler-plugin"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/IVIanuu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-28T14:34:27.000Z","updated_at":"2025-04-02T22:47:03.000Z","dependencies_parsed_at":"2024-03-08T10:40:09.900Z","dependency_job_id":"a5c982ec-7b80-4fa8-a567-5673a7d759ef","html_url":"https://github.com/IVIanuu/injekt","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IVIanuu%2Finjekt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IVIanuu%2Finjekt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IVIanuu%2Finjekt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IVIanuu%2Finjekt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/IVIanuu","download_url":"https://codeload.github.com/IVIanuu/injekt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247289409,"owners_count":20914464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compile-time-dependency-injection","compiler-plugin","dependency-injection","kotlin","kotlin-compiler-plugin"],"created_at":"2025-02-22T20:18:25.352Z","updated_at":"2025-04-05T05:02:47.725Z","avatar_url":"https://github.com/IVIanuu.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Injekt\n\nNext gen dependency injection library for Kotlin.\n```kotlin\n@Provide fun jsonParser() = JsonParser()\n\ninterface Http\n\n@Provide class HttpImpl : Http\n\n@Provide class Api(private val http: Http, private val jsonParser: JsonParser)\n\n@Provide class Repository(private val api: Api)\n\n@Provide data class AppDependencies(val repository: Repository)\n\nfun main() {\n  val dependencies = create\u003cAppDependencies\u003e() // translates to AppDependencies(Repository(Api(HttpImpl(), jsonParser()))\n  dependencies.repo\n}\n```\n\n# Setup\n```kotlin\nplugins {\n  id(\"io.github.ivianuu.injekt\") version latest_version\n}\n\ndependencies {\n  // core runtime\n  implementation(\"io.github.ivianuu.injekt:core:${latest_version}\")\n  // optional - common utilities\n  implementation(\"io.github.ivianuu.injekt:common:${latest_version}\")\n}\n```\n\n# Providers\nYou can provide dependencies by annotating them with @Provide:\n```kotlin\n// classes and objects\n@Provide class MyApi(baseUrl: BaseUrl)\n\n// constructors\nclass MyService @Provide constructor(logger: Logger) {\n  @Provide constructor()\n}\n\n// functions\n@Provide fun okHttp(authenticator: Authenticator): OkHttpClient = ...\n\n// properties and local variables\n@Provide val apiKey: ApiKey = ...\n\n// value parameters\nfun run(@Provide config: Config) {\n}\n```\n\n# Scoping\nThe core of Injekt doesn't know anything about scoping, but there is a api in the common module.\nYou have to annotate your class or the return type of a function or a property with ```@Scoped``` tag.\n```kotlin\n@Provide @Scoped\u003cUiScope\u003e class MyViewModel\n```\nThen you have to provide a ```Scope``` instance.\n```kotlin\n// use a object as name for the scope\nobject UiScope\n```\nThen you can inject your class.\n```kotlin\n@Provide val uiScope = Scope\u003cUiScope\u003e()\n\nfun onCreate() {\n  // use ui scoped dependency\n  val db = create\u003cMyViewModel\u003e()\n}\n```\nLater it should be disposed like so.\n```kotlin\nfun onDestroy() {\n  uiScope.dispose()\n}\n```\n\n# Multi injection\nYou can inject all dependencies of a given type by injecting a ```List\u003cT\u003e```\n```kotlin\n@Provide fun singleElement(): String = \"a\"\n@Provide fun multipleElements(): Collection\u003cString\u003e = listOf(\"b\", \"c\")\n\nfun main() {\n  create\u003cList\u003cString\u003e\u003e() == listOf(\"a\", \"b\", \"c\") // true\n}\n```\n\n# Function injection\nSometimes you want to delay the creation, need multiple instances, want to provide additional parameters,\nor to break circular dependencies.\nYou can do this by injecting a function.\n```kotlin\n// inject a function to create multiple Tokens\n@Provide class HttpClient(tokenFactory: () -\u003e Token) {\n  val tokenA = tokenFactory()\n  val tokenB = tokenFactory()\n}\n\n// inject a function to create a MyViewModel with the additional String parameter\n@Provide class MyActivity(viewModelFactory: (String) -\u003e MyViewModel) {\n  val viewModel by lazy { viewModelFactory(\"user_id\") }\n}\n\n// break cycles\n@Provide class Foo(val bar: Bar)\n@Provide class Bar(foo: (Bar) -\u003e Foo) {\n   val foo = foo(this)\n}\n```\n\n# Distinguish between types\nSometimes you have multiple dependencies of the same type\nInjekt will need help to keep them apart here are three strategies:\n```kotlin\n// value class\n@JvmInline value class PlaylistId(val value: String)\n@Provide val playlistId = PlaylistId(\"my_playlist\")\n\n// tag annotation\n@Tag annotation class CurrentUserId\n@Provide val currentUserId: @CurrentUserId String = \"my_user_id\"\n\n// tag typealias\n@Tag typealias PlaylistOwnerId = String\n@Provide val playlistOwnerId: PlaylistOwnerId = \"my_playlist_owner_id\"\n\n@Provide class PlaylistTracksPresenter(\n  playlistId: PlaylistId, // = PlaylistId(\"my_playlist\")\n  currentUserId: @CurrentUserId String, // \"my_user_id\"\n  playlistOwnerId: PlaylistOwnerId // \"my_playlist_owner_id\"\n)\n```\n\n# Errors / Debugging\nInjekt will show an error if there are missing dependencies.\nAdditionally it will dump generated code in a kotlin like syntax in the /build/injekt/dump folder\nfor each file where injections happen\n\n# More complex uses can be found in my essentials project(base project for my apps)\nhttps://github.com/IVIanuu/essentials\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivianuu%2Finjekt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fivianuu%2Finjekt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivianuu%2Finjekt/lists"}