Skip to main content

Crate rust_dicore

Crate rust_dicore 

Source
Expand description

LRDI — Rust Dependency Injection Framework

A dependency injection container for Rust inspired by Microsoft.Extensions.DependencyInjection (MEDI).

§Quick start

use rust_dicore::*;
use std::sync::Arc;

struct Logger(String);
struct Worker { log: Arc<Logger> }

let provider = ServiceCollection::new()
    .singleton(|_| Arc::new(Logger("INFO".into())))
    .singleton(|_| Arc::new(Worker { log: Arc::new(Logger("INFO".into())) }))
    .build()
    .unwrap();

let logger: Arc<Logger> = provider.get::<Logger>();

§Features

  • Three lifetimes: Singleton, Scoped, Transient
  • Keyed services: multiple instances of the same type, distinguished by key
  • Constructor injection: #[derive(Inject)] on structs
  • Compile-time module scanning: #[rust_dicore::module] collects service registrations
  • Cross-DLL support: named service registry for cdylib plugins

§Crate layout

ModuleDescription
ServiceCollectionRegister services with a builder API
ServiceProviderThe root DI container
Scope / ServiceScopeScoped container (one per scope)
IServiceResolverResolution trait (implemented by ServiceProvider & Scope)
ServiceProviderWrapperChild-first layered container
ServiceLifetimeEnum: Singleton, Scoped, Transient
IServiceLocator / ServiceLocatorBridgeCross-DLL / plugin integration
RdiErrorError types

§Proc-macros

  • #[derive(Inject)] — auto-generates constructor injection code
  • #[rust_dicore::module] — compile-time module scanning
  • rust_dicore::inject!(...) — declare services inside #[rust_dicore::module]

Re-exports§

pub use collection::ServiceCollection;
pub use provider::ServiceProvider;
pub use descriptor::ServiceDescriptor;
pub use lifetime::ServiceLifetime;
pub use entry::IServiceResolver;
pub use scope::Scope;
pub use scope::Scope as ServiceScope;
pub use entry::ServiceEntry;
pub use entry::ServiceFactory;
pub use wrapper::ServiceProviderWrapper;
pub use service_locator::IServiceLocator;
pub use service_locator::INamedRegistrar;
pub use bridge::RdiProvider;
pub use bridge::ServiceLocatorBridge;
pub use error::RdiError;
pub use registration::ServiceRegistration;

Modules§

bridge
Bridge implementations that adapt RDI types to the sdk::ServiceLocator trait.
collection
descriptor
entry
error
lifetime
provider
registration
scope
service_locator
store
wrapper

Macros§

inject

Attribute Macros§

inject_attr
module

Derive Macros§

Inject