Giter Site home page Giter Site logo

simpledependencyinjection's Introduction

Simple Dependency Injection

this project is inspired by Koin and for learning purposes GDK Jakarta 2019

What is Dependency Injection

In software engineering, dependency injection is a technique whereby one object supplies the dependencies of another object. A "dependency" is an object that can be used, for example as a service. Instead of a client specifying which service it will use, something tells the client what service to use. The "injection" refers to the passing of a dependency (a service) into the object (a client) that would use it. The service is made part of the client's state. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.

In simple word Dependency Injection is passing dependency to other objects or framework with some techniques.

Why Dependency Injection

  • Reusability of code
  • Ease of refactoring
  • Ease of testing

Basic Type Dependency Injection

Constructor Injection

Dependencies supplied as parameters

class EngineX

data class Plane(private val engine: EngineX)

fun main() {
    val engine = EngineX()

    // Constructor Injection
    Plane(engine)
}

Property Injection

Dependencies injected by accessing properties of object

class EngineX

class Plane {
    lateinit var enginex: EngineX
}

fun main() {
    val enginex = EngineX()
    val plane = Plane()

    //Property Injection
    plane.enginex = enginex
}

Method Injection

Dependencies to be obtained from super class function that implemented by child of super class

interface SuperEngine

class EngineX: SuperEngine

class ShipEngine: SuperEngine

abstract class Vehicle {
    //Method Injection
    private val enginex: SuperEngine = getEngine()

    protected abstract fun getEngine(): SuperEngine
}

class Plane : Vehicle() {
    override fun getEngine(): SuperEngine {
        return EngineX()
    }
}

class Ship : Vehicle() {
    override fun getEngine(): SuperEngine {
        return ShipEngine()
    }
}

Implemented Dependency Injection

  • Constructor Injection
  • Property Injection

Branch Note

  • release-0.2 Injection with qualifier
  • release-0.1 The simplest

Library

  • Android
  • GSON

References

InjectEngine Documentation

documentation

Log

  • add qualifier 3 December 2019
  • created 1 December 2019 tobe update

simpledependencyinjection's People

Contributors

labibmuhajir avatar

Watchers

James Cloos 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.