Dependency injection¶
Dependency injection provided by this framework is a more lightweight alternative to dedicated frameworks, quite similarly to Spring (which is supported) or CDI using Weld.
Rather than you having to construct objects, you may only request them, the framework will then construct it by providing the dependencies required for your service, wherever they may come from.
This avoids having to pass objects everywhere, allowing a more effective decoupling, and allows switching implementations in a completely transparent manner.
Example
ConnectionSupplier
is an interfaced service (an interface that, when implemented, enables the service to be retrieved as such interface).
You can create an implementation of this interface, per database, enabling you to switch your database, for example, using a configuration file, without changing anything else in your code.
2.X Migration
All singletons / classes with static methods were moved as services, including:
Components
EventWaiter
Localization
If you were using ExtensionsBuilder#registerConstructorParameter(Class<T>, ConstructorParameterSupplier<T>)
to get objects in commands,
it is basically the same, except in a much more complete framework, and without having to declare everything with this method.
class TagDatabase { /* */ }
class TagCommand(private val tagDatabase: TagDatabase) {
/* */
}
val tagDatabase = TagDatabase(/* */);
CommandsBuilder.newBuilder()
.registerConstructorParameter(TagDatabase::class.java) { tagDatabase }
// Further configuration
.build();
public class TagDatabase { /* */ }
public class TagCommand {
private final TagDatabase tagDatabase;
public TagCommand(TagDatabase tagDatabase) {
this.tagDatabase = tagDatabase;
}
}
final var tagDatabase = new TagDatabase(/* */);
CommandsBuilder.newBuilder()
.registerConstructorParameter(TagDatabase.class, clazz -> tagDatabase)
// Further configuration
.build();
@BService //Makes this class injectable, can also pull other services in its constructor
class TagDatabase { /* */ }
@Command
class TagCommand(private val tagDatabase: TagDatabase) {
/* */
}
No specific builder code required!
@BService //Makes this class injectable, can also pull other services in its constructor
public class TagDatabase { /* */ }
@Command
public class TagCommand {
private final TagDatabase tagDatabase;
public TagCommand(TagDatabase tagDatabase) {
this.tagDatabase = tagDatabase;
}
/* */
}
No specific builder code required!