Skip to content
This repository was archived by the owner on Jun 7, 2026. It is now read-only.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

oceandrift/di

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

Important

This library has been adopted by arsd and is now maintained as arsd.di. See: https://github.com/adamdruppe/arsd/blob/master/di.d

  • 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

Stars

5 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages