Giter Site home page Giter Site logo

jam's Introduction

Jam Jam

Maven Central Sonatype Nexus (Snapshots) Build Status License: MIT

Jam is an incredibly simple DI Scala library.

Essential differences from macwire:

  • injects a whole object tree, not only constructor arguments
  • can be easily migrated on Scala 3, since doesn't use context scans
  • incredibly simple: 1 exposed method, ~50 lines of code

Quick start

Add jam dependency:

"com.github.yakivy" %% "jam-core" % "0.0.1"

Usage example:

class DatabaseAccess()
class SecurityFilter(databaseAccess: DatabaseAccess)
class UserFinder(databaseAccess: DatabaseAccess, securityFilter: SecurityFilter)
class UserStatusReader(userFinder: UserFinder)

trait UserModule {
    val singletonDatabaseAccess = jam.brew[DatabaseAccess]
    val userStatusReader        = jam.brew[UserStatusReader]
}

will generate:

trait UserModule {
    val singletonDatabaseAccess = new DatabaseAccess()
    val userStatusReader = new UserStatusReader(
        new UserFinder(
            singletonDatabaseAccess,
            new SecurityFilter(singletonDatabaseAccess)
        )
    )
}

Implementation details

  • injection candidates is being searched in this instance, so to provide an instance for future injection you need to make it a member of this. Examples:
trait A {
    val a = new A
    ...brewing //val a will be used
}

val container = new {
    val a = new A
    ...brewing //val a will be used
}

trait A {
    def b(): String = {
        val a = new A
        ...brewing //new A instance will be created
    }
}

trait A {
    val a1 = new A
    {
        val a2 = new A
        ...brewing //val a1 will be used
    }
}
  • library injects only non implicit constructor arguments, implicits will be resolved by scalac
  • jam is intended to be minimal, features like scopes, object lifecycles or factory methods should be implemented manually, for example:
//to release resources use cats.effect.Resource
dbClientResource.use { dbClient =>
    val container = new {
        //to create an object with custom constructor define val or def member
        val config = Config.fromFile("config.conf")
        //to create singleton define val member
        val userStorage = new UserStorage(dbClient)
        //to create custom factory method define def member
        def userService = new UserService(
            userStorage,
            config.withFallback(new Config())
        )

        ...brewing //provided members will be used here
    }
    ...logic that utilizes container
} // client will be closed

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.