Giter Club home page Giter Club logo

injector's Introduction

Injector

Injector is a small dependency injection library for Scala and Scala.js.

Install

Add Injector dependency to your project.

// For Scala.js
libraryDependencies += "com.github.fomkin" %%% "injector" % "0.1.0"

// For Scala.jvm
libraryDependencies += "com.github.fomkin" %% "injector" % "0.1.0"

Configure

Define modules

class RobotRegistry {
  def deploy(robot: Robot): Unit = {
    println(s"Robot ${robot.uuid} deployed")
  }
}

class LeftLeg()
class RightLeg()
class Robot(registry: RobotRegistry, leftLeg: LeftLeg, rightLeg: RightLeg) {
  val uuid = UUID.randomUUID()
  println(s"New robot $uuid created")
  registry.deploy(this)
}

Configure robots factory

import injector._

val injector = configure("robots")(
  singleton[RobotsRegistry],
  prototype[LeftLeg],
  prototype[RightLeg],
  prototype[Robot]
)

Inject your robot

val newRobot = injector[Robot]
// New robot 40dc6398-4a28-4b92-8579-403681c9fa92 created
// Robot 40dc6398-4a28-4b92-8579-403681c9fa92 deployed 

Switch implementations

We can switch implementation of singletons and prototypes depends on environment.

class ServerLeftLeg(launcher: ReactiveRocketJump) extends LeftLeg

val injector = configure("robots")(
  ...
  singleton[RobotsRegistry],
  prototype[LeftLeg].use[ServerLeftLeg],
  prototype[RightLeg],
  prototype[Robot]
)

In this case injector.Injector will invoke ServerLeftLeg constrictor.

External dependencies

Sometime you need dependency created out of scope of injector.

val injector = configure("robots")(
  ...
  instance(system.executionContext)
)

Generate *.dot files

Injector will automatically generate Dot files if dot directory exists in project root folder. You can convert it to image via Graphviz

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.