Crate flow_di

Source
Expand description

§Flow-DI

A Rust dependency injection framework inspired by C# AutoFac and Microsoft.Extensions.DependencyInjection

§Features

  • Three Lifetime Management Types: Singleton, Scoped, Transient
  • Keyed Service Support: Named services and key-based service resolution
  • Automatic Dependency Injection: Automatic dependency injection when creating service instances
  • Fluent API: Easy-to-use service registration API using builder pattern
  • Circular Dependency Detection: Automatic detection and prevention of circular dependencies
  • Thread Safety: Full support for multi-threaded environments
  • Scope Management: Nested scopes and automatic resource cleanup

§Quick Start

use flow_di::{ContainerBuilder, ServiceProviderExt};
use std::sync::Arc;

// Define service interface
trait IRepository: Send + Sync {
    fn get_data(&self) -> String;
}

// Implement service
#[derive(Debug)]
struct DatabaseRepository {
    connection_string: String,
}

impl IRepository for DatabaseRepository {
    fn get_data(&self) -> String {
        format!("Data from database: {}", self.connection_string)
    }
}

// Configure and build container
let provider = ContainerBuilder::new()
    .add_instance("localhost:5432".to_string()) // Register configuration
    .add_singleton_with_deps::<DatabaseRepository, DatabaseRepository, String>(
        |connection_string| DatabaseRepository {
            connection_string: (*connection_string).clone(),
        }
    )
    .build();

// Resolve service
let repository = provider.get_required_service::<DatabaseRepository>().unwrap();
println!("{}", repository.get_data());

Re-exports§

pub use builder::ContainerBuilder;
pub use container::Container;
pub use container::ServiceProvider;
pub use container::ServiceScope;
pub use descriptor::ServiceDescriptor;
pub use descriptor::ServiceFactory;
pub use descriptor::ServiceProvider as ServiceProviderTrait;
pub use descriptor::ServiceProviderExt;
pub use errors::DiError;
pub use errors::DiResult;
pub use lifetime::Lifetime;
pub use service_key::ServiceKey;

Modules§

builder
container
descriptor
errors
lifetime
service_key

Macros§

container
Convenient macro for creating container builder
service_key
Convenient macro for creating service keys

Functions§

container
Convenient function to create a container
container_builder
Convenient function to create a container builder