GitXplorerGitXplorer
i

IoCContainer

public
0 stars
0 forks
0 issues

Commits

List of commits on branch main.
Verified
44788002d97919926d32c43f6fd8720c463dcd2f

Update renovate.json

iigorwojda committed 2 years ago
Verified
78b858ab23d9991c9587aa21d7315119c8a55385

Merge pull request #1 from igorwojda/renovate/configure

iigorwojda committed 2 years ago
Verified
23cb14dbed0e178eab72c39ff5ee0c87333ba159

Add renovate.json

rrenovate[bot] committed 2 years ago
Unverified
db453dce375c10c767931c1c2a484d8e6d5cb889

Remove class

iigorwojda committed 3 years ago
Unverified
3e9495e5abfe8cb05332712a9d38352997acc9d2

Remove redundant function

iigorwojda committed 3 years ago
Unverified
c987e516fd64ee40d83e01cb75000d351668d961

Merge remote-tracking branch 'origin/main' into main

iigorwojda committed 3 years ago

README

The README file for this repository.

What is it

Basic IoC container written in pure Kotlin.

How to run project

Checkout project and open it in InteliJ IDEA. Open Main.kt file to see a sample application.

To run tests open Gradle View and run Tasks - Verification - test task.

Features

  • Constructor injection
  • Nested dependency resolution
  • Dependency verification
  • Circular dependency detection
  • Create instance of type with parameterless constructor (without need to register)
  • Supports @Inject annotation
  • Supports Kotlin Objects
  • Supports @Singleton annotation
  • Error handling (around access modifiers, abstract classes and interfaces, etc.)

Examples

Getting an instance of the Container:

val container = DependencyContainer.getInstance()

Register instance:

container.registerBinding(Foo::class, FooImpl::class)

Resolve class with parameterless constructor:

class FooImpl: Foo

val container = DependencyContainer.getInstance()

// No registration in contaier is required for class with parameterless constructor
val instance = container.resolveBinding<FooImpl>()
println(instance) // FooImpl

Resolve binding for the interface:

interface Foo
class FooImpl: Foo

val container = DependencyContainer.getInstance()
// Given interface Foo return FooImpl instance
container.registerBinding(Foo::class, FooImpl::class)

val instance = container.resolveBinding<Foo>()
println(instance) // FooImpl

Retrieve class with nested dependency:

interface Foo
class FooImpl: Foo
class Bar(val foo:FooImpl)

val container = DependencyContainer.getInstance()
container.registerBinding(Foo::class, FooImpl::class)

val instance = container.resolveBinding<Bar>()
println(instance) // Bar

Inject singleton:

@Singleton
class Foo

val instance1 = container.resolveBinding<Foo>()
val instance2 = container.resolveBinding<Foo>()

insatnce1 === instance2

More examples can be found in DependencyContainerTest class.

Dependency verification

It is possible to verify all bindings in the container. This option is useful to check if all bindings can be resolved correctly.

val container = DependencyContainer.getInstance()
...
container.verifyBindings()