Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Basic Usage

Building objects with dconstructor consists of two steps: registering types and building. Using autobuild (see BuilderOptions) can eliminate some or all of the registration process, depending on your usage of interfaces and inheritance.

There are two ways to register a type. Let's have a look:

import dconstructor.build2;
class MyClass {}
void main ()
{
    builder.register!(MyClass);
}

Or with an interface:

interface Interface {}
class MyClass : Interface {}
void main ()
{
    builder.bind!(Interface, MyClass);
}

Alternatively, you can perform the registration in the class itself:

interface Interface {}
class MyClass : Interface
{
    mixin (Implements!(Interface));
}

Building

The only method you need to use in order to build an object is Builder.get:

void main ()
{
    MyClass instance = builder.get!(MyClass);
}

Two-stage builds and circular dependencies

Let's consider a pluggable formatting system:

class Formatter
{
    this (DateTimeFormatter datetimes, TimeFormatter times, DateFormatter dates) { ... }
}

class DateTimeFormatter
{
    this (Formatter formatter) { ... }
    public char[] format (DateTime value)
    {
        return formatter.format ("{} {}", value.date, value.time);
    }
}

In this case, DateTimeFormatter? cannot be built without a Formatter, and Formatter cannot be built without a DateTimeFormatter?. Dconstructor will throw a CircularDependencyException? if you try building this.

However, it isn't necessary to have a Formatter reference in the DateTimeFormatter? as soon as the DateTimeFormatter? is created, as long as the reference is there before it's used. Dconstructor allows for a second building stage:

class DateTimeFormatter
{
    public void inject (Formatter formatter) { ... }
    public char[] format (DateTime value)
    {
        return formatter.format ("{} {}", value.date, value.time);
    }
}

Note that this only works with singletons. If you attempt this with classes that are not singletons, dconstructor will still throw a CircularDependencyException?.