Skip to content

oceandrift/di

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

oceandrift/di

A lightweight Dependency Injection (DI) framework with focus on simplicity.

  • Inversion of Control (IoC).
  • Convention over configuration.
  • Injects dependencies via constructor parameters.
  • Supports structs as well (… as classes and interfaces).
  • No clutter – this library is a single, readily comprehensible file.
  • No external dependencies. (*Only the D standard library is used.)

Installation

When using DUB, it’s as simple as running dub add oceandrift-di.

Usage

See in-code documentation for details. Or check out the pre-rendered copy on dpldocs.

class Dependency {}

class Foo {
    this(Dependency d) {
        //
    }
}

// Bootstrap the DI framework.
// Then let it resolve the whole dependency tree of `Foo`
// and construct dependencies as needed.
auto di = new DI();
Foo foo = di.resolve!Foo();

Less boilerplate

class Foo {
    // Mark fields as @dependency.
    private @dependency Dependency1 d1;
    private @dependency Dependency2 d2;

    // Generate a constructor that assigns all dependencies.
    mixin DIConstructor;

// The generated constructor corresponds roughly to this code:
/*
    public this(
        Dependency1 d1,
        Dependency2 d2,
    ) {
        this.d1 = d1;
        this.d2 = d2;
    }
 */
}

About

Lightweight Dependency Injection (DI) framework

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages