Skip to content

A simple DI (Dependency Injection) singleton object store for thread-safe shared object management.

License

Notifications You must be signed in to change notification settings

innobead/simpledi-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simpledi-rs

What is simpledi-rs?

A simple DI (Dependency Injection) singleton object store for thread-safe shared object management.

Getting Started

Cargo.toml

Add simpledi-rs dependency. You also check the package info at https://crates.io/crates/simpledi-rs.

[dependencies]
simpledi-rs = "*"

Usage

  1. create a DI container (DIContainerTrait)
  2. create objects, and add them to the container via create_dep! macro
  3. initialize the DI container
  4. inject DI container to DI aware objects which implements DependencyInjectTrait via inject_dep! macro
  5. use the container to get the managed objects
#[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();
}

About

A simple DI (Dependency Injection) singleton object store for thread-safe shared object management.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages