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§
- lazy
- Contains support for lazy service resolution.
Structs§
- Activator
inject
- Represents an activator for a service instance.
- Inject
Builder inject
- Represents the builder for an injected type.
- Keyed
Ref - Represents a holder for a keyed service.
- Scoped
Service Provider - Represents a scoped
ServiceProvider
. - Service
Collection - Represents a service collection.
- Service
Dependency - Represents a service dependency.
- Service
Descriptor - Represents the description of a service with its service type, implementation, and lifetime.
- Service
Descriptor Builder builder
- Represents a
ServiceDescriptor
builder. - Service
Provider - Represents a service provider.
- Type
- Represents a type.
- Validation
Error - Represents an validation error.
Enums§
- Service
Cardinality - Represents the possible cardinalities of a service dependency.
- Service
Lifetime - Represents the possible service lifetimes.
Traits§
- Injectable
inject
- Defines the behavior of an injectable type.
Functions§
- exactly_
one builder
- Creates a new
ServiceDependency
with a cardinality of exactly one (1:1). - exactly_
one_ with_ key builder
- Creates a new keyed
ServiceDependency
with a cardinality of exactly one (1:1). - existing
builder
- Creates a new singleton
ServiceDescriptor
for an existing service instance. - existing_
as_ self builder
- Creates a new singleton
ServiceDescriptor
for an existing service instance. - existing_
with_ key builder
- Creates a new singleton
ServiceDescriptor
for an existing service instance with a key. - existing_
with_ key_ as_ self builder
- Creates a new singleton
ServiceDescriptor
for an existing service instance with a key. - scoped
builder
- Initializes a new scoped
ServiceDescriptorBuilder
. - scoped_
factory builder
- Initializes a new scoped
ServiceDescriptor
. - scoped_
with_ key builder
- Initializes a new scoped keyed
ServiceDescriptorBuilder
. - scoped_
with_ key_ factory builder
- Initializes a new keyed scoped
ServiceDescriptor
. - singleton
builder
- Initializes a new singleton
ServiceDescriptorBuilder
. - singleton_
as_ self builder
- Initializes a new singleton
ServiceDescriptorBuilder
. - singleton_
factory builder
- Initializes a new singleton
ServiceDescriptor
. - singleton_
with_ key builder
- Initializes a new keyed singleton
ServiceDescriptorBuilder
. - singleton_
with_ key_ factory builder
- Initializes a new keyed singleton
ServiceDescriptor
. - transient
builder
- Initializes a new transient
ServiceDescriptorBuilder
. - transient_
as_ self builder
- Initializes a new transient
ServiceDescriptorBuilder
. - transient_
factory builder
- Initializes a new transient
ServiceDescriptor
. - transient_
with_ key builder
- Initializes a new keyed transient
ServiceDescriptorBuilder
. - transient_
with_ key_ as_ self builder
- Initializes a new transient keyed
ServiceDescriptorBuilder
. - transient_
with_ key_ factory builder
- Initializes a new keyed transient
ServiceDescriptor
. - validate
- Validates the specified
ServiceCollection
. - zero_
or_ more builder
- Creates a new
ServiceDependency
with a cardinality of zero or more (0:*). - zero_
or_ more_ with_ key builder
- Creates a new keyed
ServiceDependency
with a cardinality of zero or more (0:*). - zero_
or_ one builder
- Creates a new
ServiceDependency
with a cardinality of zero or one (0:1). - zero_
or_ one_ with_ key builder
- Creates a new keyed
ServiceDependency
with a cardinality of zero or one (0:1).
Type Aliases§
- Keyed
RefMut - Represents a holder for a keyed, mutable service.
- Ref
- Represents the type alias for a service reference.
- RefMut
- Represents the type alias for a mutable service reference.
- Service
Factory - 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 the
Injectable
trait.