Crates.io | simpledi-rs |
lib.rs | simpledi-rs |
version | 0.1.0 |
source | src |
created_at | 2020-12-15 15:40:32.592085 |
updated_at | 2020-12-15 15:40:32.592085 |
description | simpledi-rs, a simple and thread-safe dependency injection library |
homepage | https://github.com/innobead/simpledi-rs |
repository | https://github.com/innobead/simpledi-rs |
max_upload_size | |
id | 323167 |
size | 20,592 |
A simple DI (Dependency Injection) singleton object storage for providing thread-safe shared object management.
[dependencies]
simpledi-rs = "*"
DIContainerTrait
)create_dep!
macroDependencyInjectTrait
via inject_dep!
macro#[macro_use]
extern crate simpledi_rs;
use simpledi_rs::di::{DIContainer, DIContainerTrait, DependencyInjectTrait};
use std::sync::Arc;
#[derive(Debug)]
struct DIAwareStruct(Option<Arc<DIContainer>>);
impl DIAwareStruct {
fn new() -> Self {
Self { 0: None }
}
}
impl DependencyInjectTrait for DIAwareStruct {
fn inject(&mut self, container: Arc<DIContainer>) {
self.0 = Some(container.clone())
}
}
fn main() {
let mut container = DIContainer::new(); // (1)
// add obj to DI
create_dep!(DIAwareStruct::new(), container); // (2)
// inject DI container to aware objects
let container_arc = container.init().unwrap(); // (3)
inject_dep!(DIAwareStruct, container_arc.clone()); // (4)
// get container from aware objects
let injected_obj = container_arc.get::<DIAwareStruct>().unwrap();
}