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.
- 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)
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.
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.
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.
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.
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)
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]
}
- Enrich this README and add wiki pages
- Implement DSL
- Implement
init
hooks