Explicit Bindings¶
Explicit bindings make use of the Bind<T>()
method within a module.
Type¶
Allows you to define what implementation of type you will use, but lets the injector create it for you.
Bind<IService>()
.To<ServiceImpl>()
.In(BindingScope.Singleton);
...
public sealed class ServiceConsumer {
private IService service;
[Inject]
private ServiceConsumer(IService service) {
this.service = service;
}
}
Untargeted¶
An untargeted binding specifies a concrete class when calling Bind<T>()
. This can be used to make the injector aware of a type and modify the scope or name of the binding.
Bind<ServiceImpl>()
.In(BindingScope.Singleton);
Instance¶
Bind to a specific instance of a type. This is primarily used when binding to primitives.
Bind<int>()
.ToInstance(8000)
.Named("port");
Bind<string>()
.ToInstance("/service")
.Named("path");
...
public sealed class ServiceImpl : IService {
private int port;
private string path;
[Inject]
private ServiceImpl(
[Named("port")] int port,
[Named("path")] string path) {
this.port = port;
this.path = path;
}
}
Singleton
scope. While you can override scope, doing so WILL result in the injector trying to instantiate the types instead.