Skip to content

A library for simple Dependency Injection in Scala projects

License

Notifications You must be signed in to change notification settings

astonbitecode/kind-of-di

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kind-of-di Build Status Maven Central

This is a small library that helps in achieving simple Dependency Injection (DI) in Scala.

Its purpose is not to provide a full DI Framework, but to assist in keeping the code clean and easer to test.

Features

  • Separation of Objects' initialization and usage
  • No XML configuration needed. It is up to the developer to decide where, how and when the Objects' initialization is defined
  • Hooks upon Objects' creation, destruction, injection (TODO)

Quick Start

The kind-of-di is actually creating instances of Types and injects them in other instances that need them.

In order for this to happen, the user code needs to provide information on how an instance can be created.

How to define Objects creation

If you want a HelpfulClass of yours to be created once and get injected in other classes (like in Spring's Singleton scope) you can do it with the following piece of code:

import com.github.astonbitecode.di._

diDefine(() => new HelpfulClass())

The HelpfulClass has nothing special. It can be any Class.

The call to diDefine has to be done in an early phase before any instantiation of Classes that need the HelpfulClass to be injected.

How to use Injections in your Classes

If you want the HelpfulClass from above to be injected into a Class of yours, you can do the following:

import com.github.astonbitecode.di._

class MyClass {
  private val helpfulClass = inject[HelpfulClass]

	/*
	 * Rest of the code here
	 */
}

Now the MyClass can be instantiated as usual...

val myClass = new MyClass()

...and the helpfulClass field will get populated by the library.

Scopes and Lifecycle

When calling the diDefine, you provide the information of how instances will be created and injected.

The kind-of-di uses these definitions when needed in order to deliver/inject the right object. Following the paradigm of other DI frameworks, scopes define the lifecycle of injected instances. Currently, the supported scopes are Singleton and Prototype.

Singleton Scope

A Singleton is an instance that is created only once, no matter how many Objects it will be injected in.

When defining Classes that should be injected as Singleton objects, there is one more thing to take into consideration: When the instance will be created.

A Singleton instance can be created Eagerly, at the time of its definition, or Lazily, at the time of its injection.

  • Eager Singletons are defined like:

    diDefine(() => new MyInjectableClass(), DIScope.SINGLETON_EAGER)

  • Lazy Singletons are defined like:

    diDefine(() => new MyInjectableClass(), DIScope.SINGLETON_LAZY)

Prototype Scope

Unlike Singleton scope, in the Prototype scope a new instance is created for each Object that needs to have it injected in.

Prototypes are defined like:

diDefine(() => new MyInjectableClass(), DIScope.PROTOTYPE)

Because of the Prototype nature, the laziness cannot be controlled by the kind-of-di. The library will create instances whenever the injection should happen.

However, laziness can be controlled by Scala anyway; defining the injected field as a lazy val:

class MyClassWithPrototype {
  lazy val mic = inject[MyInjectableClass]
}

Short TODO List

  • Enrich this README and add wiki pages
  • Implement DSL
  • Implement init hooks

About

A library for simple Dependency Injection in Scala projects

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages