DConstructor: a lightweight dependency injection tool
DConstructor is a dependency injection framework for D. It's modeled, vaguely, after Guice.
It should work with dmd1 and dmd2, with tango and phobos. If there are problems, let me know; I use phobos with dmd2 and tango with dmd1, so I might not catch some bugs.
Download: version 0.91
There's a static builder defined: dconstructor.build.builder. It has three methods:
get(T)()
Build an object of type T, where T is a concrete class, or an interface or abstract class that has been bound to a concrete class. All its constructor dependencies will be supplied by calling this method recursively.
bind(TVisible, TImpl)()
Whenever someone tries to build TVisible, build TImpl.
provide(T)(T value)
Whenever someone tries to build T, give them the value supplied.
Example:
import dconstructor.build; import dconstructor.singleton; interface IFoo {} class Foo : IFoo {} // Just by implementing the Singleton interface, we make Bar a singleton. class Bar : Singleton { this (IFoo foo) {} } void main () { builder.bind!(IFoo, Foo); auto bar1 = builder.get!(Bar); auto bar2 = builder.get!(Bar); // We said Bar's a singleton, so we should expect these two are the same. assert (bar1 is bar2); auto foo = new Foo(); builder.provide!(IFoo)(foo); assert (builder.get!(IFoo) is foo); assert (builder.get!(Foo) !is foo); }
