8 releases

0.3.2 Jul 21, 2024
0.3.1 Jul 21, 2024
0.2.3 Jul 20, 2024
0.1.0 Jul 15, 2024

#1861 in Rust patterns

Download history 43/week @ 2024-07-30 4/week @ 2024-09-10 5/week @ 2024-09-17 32/week @ 2024-09-24 7/week @ 2024-10-01

252 downloads per month

Custom license

26KB
287 lines

Yadir

GitHub Actions Workflow Status Crates.io Version

Yadir is yet another dependency injection registry for Rust.


About

Yadir's API is heavily inspired by the Microsoft.Extensions.DependencyInjection library for .NET. It provides a simple and easy way to register and resolve dependencies in your Rust application at runtime.

Its initial implementation is based on the Registry design pattern, formulated by Will Crichton in his book about Type-Driven API Design in Rust.

Usage

Add Yadir to your Cargo.toml file:

[dependencies]
yadir = { version = "0.3.0", features = ["derive"] }

Create a new registry and register your dependencies, after implementing the DIBuilder trait for each one of them:

use yadir::core::contracts::DIBuilder;
use yadir::core::primitives::{DIManager, DIObj};
use yadir::{deps, let_deps};
use async_trait::async_trait;
use dyn_clone::{clone_trait_object, DynClone};
use yadir_derive::DIBuilder;

clone_trait_object!(Printer);
clone_trait_object!(Writer);

trait Printer: Sync + Send + DynClone {
    fn print(&self) -> String;
}

trait Writer: Sync + Send + DynClone {
    fn write(&self) -> String;
}

#[derive(Clone, DIBuilder)]
#[build_as(Box<dyn Printer>)]
struct Bar;

impl Printer for Bar {
    fn print(&self) -> String {
        "bar".to_string()
    }
}

#[derive(Clone, DIBuilder)]
#[build_as(Box<dyn Writer>)]
struct Baz;

impl Writer for Baz {
    fn write(&self) -> String {
        "baz".to_string()
    }
}

#[derive(Clone, DIBuilder)]
#[build_method("new")]
struct Foo {
    #[deps]
    printer: Box<dyn Printer>,
    #[deps]
    writer: Box<dyn Writer>,
}

impl Foo {
    fn new(printer: Box<dyn Printer>, writer: Box<dyn Writer>) -> Self {
        Self { printer, writer }
    }

    fn print(&self) -> String {
        format!("foo {} {}", self.printer.print(), self.writer.write())
    }
}

#[tokio::main]
async fn main() {
    let mut manager = DIManager::default();
    manager.build::<Bar>().await;
    manager.build::<Baz>().await;
    
    let foo = manager.build::<Foo>().await.unwrap().extract();
    
    assert_eq!(foo.print(), "foo bar baz");
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Dependencies

~2.7–8.5MB
~74K SLoC