Crates.io | more-di |
lib.rs | more-di |
version | 3.1.0 |
source | src |
created_at | 2022-09-27 23:32:34.055754 |
updated_at | 2023-12-20 02:03:42.747668 |
description | Provides support for dependency injection (DI) |
homepage | https://commonsensesoftware.github.io/more-rs-di/ |
repository | https://github.com/commonsensesoftware/more-rs-di |
max_upload_size | |
id | 675296 |
size | 135,337 |
More DI is a dependency injection (DI) library for Rust. A trait
or struct
can be used as the injected type.
You may be looking for:
This crate provides the following features:
A service can have the following lifetimes:
Consider the following traits and structures.
Proc macro attributes are not required, but they are the fastest and simplest approach to add DI in your applications.
use di::*;
use std::rc::Rc;
trait Phrase {
fn salutation(&self) -> &str;
}
#[injectable(Phrase)]
struct EnglishPhase;
impl Phrase for EnglishPhrase {
fn salutation(&self) -> &str {
"Hello world!"
}
}
#[injectable]
struct Person {
phase: Rc<dyn Phrase>,
}
impl Person {
fn speak(&self) -> &str {
self.phrase.salutation()
}
}
This information can now be composed into a simple application:
use crate::*;
use di::*;
fn main() {
let provider = ServiceCollection::new()
.add(EnglishPhrase::singleton())
.add(Person::transient())
.build_provider()
.unwrap();
let person = provider.get_required::<Person>();
println!("{}", person.speak());
}
This project is licensed under the MIT license.