Code Monkey home page Code Monkey logo

difk's Introduction

DIFK (Dependency Injection For Kotlin)

Latest release: 0.1

CI: Build Status

Email: [email protected]

License: Apache 2.0

Simple, lightweight and functional IoC framework for Kotlin

  • Tired of long XML/JSON gibberish configs?

  • Fed up with reflection/byte code magic?

  • Want super simple but still functional dependency injection?

Choose DIFK

Example

import DifkInjector
import DifkInjector.addDestructor
import DifkInjector.loadPropertiesFromClasspath
import DifkInjector.addSingleton
import DifkInjector.getProperty
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test

@Test
fun simpleScenario() {
    loadPropertiesFromClasspath("test.properties")
    val dataSource = addSingleton("dataSource", { DataSource(
            getProperty("db.url")!!,
            getProperty("db.driver.class.name")!!,
            getProperty("db.username")!!,
            getProperty("db.password")!!
    ) })
    val dao = addSingleton("dao", { Dao(dataSource) })
    val service = addSingleton("service", { val s = Service(dao); s.init(); s })
    addDestructor { service.close() }
    
    assertFalse(service.closed!!)
    val difkService: Service = getInstance("service")
    assertTrue(service === difkService)
    assertEquals("my_url", difkService.dao.dataSource.url)
    assertEquals("my_class", difkService.dao.dataSource.driverClassName)
    assertEquals("my_user", difkService.dao.dataSource.username)
    assertEquals("my_password", difkService.dao.dataSource.password)
    
    DifkInjector.close()
    assertTrue(service.closed!!)
}

class DataSource(val url: String, val driverClassName: String, val username: String, val password: String)

class Dao(val dataSource: DataSource)

class Service(val dao: Dao) {
    var closed: Boolean? = null

    fun init() {
        closed = false
    }

    fun close() {
        closed = true
    }
}

Features

  • 3 types of instances supported: singletons (single instance), prototypes (new instance for every call) and thread locals (new instance for every thread)
  • property management support (addPropertiesFromFile, loadPropertiesFromClasspath)
  • init and destroy methods support (look at example)
  • simple explicit configuration with kotlin code
  • preserving your favourite kotlin style
  • minimum external dependencies (look at build.gradle)
  • no reflection/byte code magic (you're fully in control of your code)
  • self-explaining API

Useful tips

  • Use registerShutdownHook method to finish your application gracefully (invoke all destroyers) on jvm shutdown

difk's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.