SimpleContainer

A simple implementation of Container.

SimpleContainer stores providers keyed by the TypeInfo and name, in that order. The container itself is registered, so that it can be injected as a dependency into other objects.

Constructors

this
this()
Undocumented in source.

Members

Functions

addProvider
void addProvider(TypeInfo info, Provider provider)
Undocumented in source.
addProvider
void addProvider(TypeInfo info, string name, Provider provider)
Undocumented in source.
canResolve
bool canResolve(TypeInfo info, string name)
Undocumented in source. Be warned that the author may not have intended to support it.
canResolve
bool canResolve(TypeInfo info)
Undocumented in source. Be warned that the author may not have intended to support it.
withResolvedPtr
void withResolvedPtr(TypeInfo info, void delegate(void*) dg)
Undocumented in source. Be warned that the author may not have intended to support it.
withResolvedPtr
void withResolvedPtr(TypeInfo info, string name, void delegate(void*) dg)
Undocumented in source. Be warned that the author may not have intended to support it.

Inherited Members

From Container

addProvider
void addProvider(TypeInfo type, Provider provider)
void addProvider(TypeInfo type, string name, Provider provider)

Register a Provider for it's TypeInfo. A name can also be provided to disambiguate when multiple providers are needed

value
void value(string name, T value)
void value(T value)

Register a single value.

register
void register(Flag!"asSingleton" asSingleton)
void register(string name, Flag!"asSingleton" asSingleton)

Register a Class.

factory
void factory(Flag!"asSingleton" asSingleton)
void factory(string name, Flag!"asSingleton" asSingleton)
void factory(F func, Flag!"asSingleton" asSingleton)
void factory(string name, F func, Flag!"asSingleton" asSingleton)

Register a factory function for a type.

provider
void provider(Provider provider)
void provider(string name, Provider provider)

Register a provider using the type returned by provider.providedType.

Examples

import injected.injection;

@Injectable!(int, Injected!(int, "a"))
static class A {
    int x;
    int y;
    this(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
static class B : A  {
    string name;
    this(int x, int y, string name) {
        super(x, y);
        this.name = name;
    }
}
static struct C {
    string name;
    ushort id;
    static ushort idCounter = 0;
}
static C makeC() {
    return C("Foo", C.idCounter++);
}
auto container = makeContainer();
container.value!int(0);
container.value!int("a", 1);
container.register!A();
container.register!(A, B)("b");
container.factory!string(delegate(C c) {
    return c.name;
});
container.factory!(C, makeC)();
container.factory!(C, makeC)("c", No.asSingleton);

assert(container.resolve!int() == 0);
assert(container.resolve!int("a") == 1);
assert(container.resolve!string() == "Foo");
auto a = container.resolve!A();
assert(a.x == 0);
assert(a.y == 1);
auto b = cast(B) container.resolve!A("b");
assert(b);
assert(b.x == 0);
assert(b.y == 0);
assert(b.name == "Foo");
assert(container.resolve!C().id == 0);
auto c = container.resolve!C("c");
assert(c.name == "Foo");
assert(c.id == 1);
assert(container.resolve!C("c").id == 2);

Meta