Skip to content

Steeoghr/rs-macro-di

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);
})

Releases

No releases published

Packages

No packages published

Languages