Expand description
More Dependency Injection
More DI is a dependency injection (DI) library for Rust. A trait
or struct
can be used as the injected type.
You may be looking for:
Features
This crate provides the following features:
- default - Abstractions for dependency injection, plus the builder and inject features
- builder - Functions for configuring service descriptors
- async - Use dependencies in an asynchronous context
- inject - Code-generate common injection scenarios
- lazy - Lazy-initialize service resolution
- fmt - Additional output formatting
- alias - Use alternate type aliases
Supported Lifetimes
A service can have the following lifetimes:
- Transient - a new instance is created every time it is requested
- Singleton - a single, new instance is created the first time it is requested
- Scoped - a new instance is created once per provider that it is requested from
Dependency Injection in Action
Consider the following traits and structures.
Proc macro attributes are not required, but they are the fastest and simplest approach to add DI in your applications.
use di::*;
use std::rc::Rc;
trait Phrase {
fn salutation(&self) -> &str;
}
#[injectable(Phrase)]
struct EnglishPhase;
impl Phrase for EnglishPhrase {
fn salutation(&self) -> &str {
"Hello world!"
}
}
#[injectable]
struct Person {
phase: Rc<dyn Phrase>,
}
impl Person {
fn speak(&self) -> &str {
self.phrase.salutation()
}
}
This information can now be composed into a simple application:
use crate::*;
use di::*;
fn main() {
let provider = ServiceCollection::new()
.add(EnglishPhrase::singleton())
.add(Person::transient())
.build_provider()
.unwrap();
let person = provider.get_required::<Person>();
println!("{}", person.speak());
}
License
This project is licensed under the MIT license.
Modules
- Contains support for lazy service resolution.
Structs
- Activator
inject
Represents an activator for a service instance. - InjectBuilder
inject
Represents the builder for an injected type. - Represents a holder for a keyed service.
- Represents a scoped
ServiceProvider
. - Represents a service collection.
- Represents a service dependency.
- Represents the description of a service with its service type, implementation, and lifetime.
- ServiceDescriptorBuilder
builder
Represents aServiceDescriptor
builder. - Represents a service provider.
- Represents a type.
- Represents an validation error.
Enums
- Represents the possible cardinalities of a service dependency.
- Represents the possible service lifetimes.
Traits
- Injectable
inject
Defines the behavior of an injectable type.
Functions
- exactly_one
builder
Creates a newServiceDependency
with a cardinality of exactly one (1:1). - exactly_one_with_key
builder
Creates a new keyedServiceDependency
with a cardinality of exactly one (1:1). - existing
builder
Creates a new singletonServiceDescriptor
for an existing service instance. - existing_as_self
builder
Creates a new singletonServiceDescriptor
for an existing service instance. - existing_with_key
builder
Creates a new singletonServiceDescriptor
for an existing service instance with a key. - existing_with_key_as_self
builder
Creates a new singletonServiceDescriptor
for an existing service instance with a key. - scoped
builder
Initializes a new scopedServiceDescriptorBuilder
. - scoped_factory
builder
Initializes a new scopedServiceDescriptor
. - scoped_with_key
builder
Initializes a new scoped keyedServiceDescriptorBuilder
. - scoped_with_key_factory
builder
Initializes a new keyed scopedServiceDescriptor
. - singleton
builder
Initializes a new singletonServiceDescriptorBuilder
. - singleton_as_self
builder
Initializes a new singletonServiceDescriptorBuilder
. - singleton_factory
builder
Initializes a new singletonServiceDescriptor
. - singleton_with_key
builder
Initializes a new keyed singletonServiceDescriptorBuilder
. - singleton_with_key_factory
builder
Initializes a new keyed singletonServiceDescriptor
. - transient
builder
Initializes a new transientServiceDescriptorBuilder
. - transient_as_self
builder
Initializes a new transientServiceDescriptorBuilder
. - transient_factory
builder
Initializes a new transientServiceDescriptor
. - transient_with_key
builder
Initializes a new keyed transientServiceDescriptorBuilder
. - transient_with_key_as_self
builder
Initializes a new transient keyedServiceDescriptorBuilder
. - transient_with_key_factory
builder
Initializes a new keyed transientServiceDescriptor
. - Validates the specified
ServiceCollection
. - zero_or_more
builder
Creates a newServiceDependency
with a cardinality of zero or more (0:*). - zero_or_more_with_key
builder
Creates a new keyedServiceDependency
with a cardinality of zero or more (0:*). - zero_or_one
builder
Creates a newServiceDependency
with a cardinality of zero or one (0:1). - zero_or_one_with_key
builder
Creates a new keyedServiceDependency
with a cardinality of zero or one (0:1).
Type Aliases
- Represents a holder for a keyed, mutable service.
- Represents the type alias for a service reference.
- Represents the type alias for a mutable service reference.
- Represents the callback function used to create a service.
Attribute Macros
- inject
inject
Represents the metadata used to identify an injected function. - injectable
inject
Represents the metadata used to implement theInjectable
trait.