Skip to content

Latest commit

 

History

History
executable file
·
132 lines (99 loc) · 4.17 KB

README.md

File metadata and controls

executable file
·
132 lines (99 loc) · 4.17 KB

Mindy

Build Status Download

Mindy is Minimum Dependency Injection library.

Feature:

  • Resolve generics type parameter
  • Resolve instance as singleton or else
  • Create parameterized instance

Usage

Install

build.gradle

allprojects {
    repositories {
        google()
        jcenter()
        maven{ url "https://dl.bintray.com/mobile-act/Mindy" } // need!
    }
}
dependencies {
    implementation 'mobile-act:mindy:VERSION' // adapt latest version
}

Simple

val mindy = Mindy()

// register type: String
mindy.register{ "Example" }

val value = mindy.resolve<String>()

Polymorphism and TypeParameter

interface IValue<T> {

    val value: T
}

abstract class BaseValue<T>(override val value: T) : IValue<T>

class Value<T>(value: T) : BaseValue<T>(value)

class StringValue(value: String) : BaseValue<String>(value)
val mindy = Mindy()

// regster type: IValue<Int>
mindy.register<IValue<Int>>{ Value(10) } 

// register type: IValue<String>
mindy.register<IValue<String>>{ StringValue("Example") }

// register type: IValue<IValue<String>>
// dependency resolve(): IValue<String>
mindy.register<IValue<IValue<String>>>{ Value(resolve()) }


val intValue = mindy.resolve<IValue<Int>>()
val stringValue = mindy.resolve<IValue<String>>()
val nestedStringValue = mindy.resolve<IValue<IValue<String>>>()

Create instance every resolve

val mindy = Mindy()

mindy.register<IValue<String>> { StringValue("Example") }

val a = mindy.create<IValue<String>>()
val b = mindy.create<IValue<String>>()
// a and b is different instance

Parameterized

val mindy = Mindy()

mindy.register<IValue<IValue<String>>> { Value(resolve()) }

val a = mindy.create<IValue<IValue<String>>> {
    register<IValue<String>> { StringValue("a") }
}
val b = mindy.create<IValue<IValue<String>>> {
    register<IValue<String>> { StringValue("b") }
}
// a and b is different value

Named

val mindy = Mindy()

mindy.register<IValue<String>>(name = "a") { StringValue("a") }
mindy.register<IValue<String>>(name = "b") { StringValue("b") }

val a = mindy.resolve<IValue<String>>(name = "a")
val b = mindy.resolve<IValue<String>>(name = "b")
// a and b is different instance/value

Notify

Now, Mindy is using @ExperimentalStdlibApi by typeOf().

License

This library is under MIT License

Rule

Sample

Contribute

ToDo: Write

Other

Author: @MeilCli