rs-macro-di

Crates.iors-macro-di
lib.rsrs-macro-di
version0.3.4
sourcesrc
created_at2024-06-11 10:41:28.599872
updated_at2024-06-15 08:55:31.534325
descriptionRust dependency injection by macro
homepagehttps://github.com/Steeoghr/rs-macro-di
repositoryhttps://github.com/Steeoghr/rs-macro-di
max_upload_size
id1268067
size17,660
(Steeoghr)

documentation

README

Rust Macro Dependency Injection

Import lib

rs-macro-di = "0.2.0"

or add with cargo:

cargo add rs-macro-di

Add dependency

// Add singleton service
add_singleton!(Arc::new(SingletonService::new()));

// Add scoped service
add_scoped!(ScopedService);

// Add transient service
add_transient!(ScopedService);

Use a configured service

let service: Arc<ServiceClass> = provide!(ServiceClass);

Or without macro

// Get provider
let mut provider = provider!();
// or
let mut provider = get_mut_service_provider();


// Add singleton service
provider.add_singleton(Arc::new(SingletonService::new()));

// Add scoped service
provider.add_scoped::<ScopedService>();

// Add transient service
provider.add_transient::<ScopedService>();
// Get provider
let provider = provider!();

let service: Arc<ServiceClass> = provider.provide::<ServiceClass>();

Use a scoped block

// Add scoped service
add_scoped!(ScopedService);

// scoped service instances are cleared at the end of with_scope
with_scope(|| {
    let service: Arc<ScopedService> = provide!(ScopedService);
})
Commit count: 44

cargo fmt