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. After they ar defined, the information can be composed into a simple application.
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 EnglishPhrase;
impl Phrase for EnglishPhrase {
fn salutation(&self) -> &str {
"Hello world!"
}
}
#[injectable]
struct Person {
phrase: Rc<dyn Phrase>,
}
impl Person {
fn speak(&self) -> &str {
self.phrase.salutation()
}
}
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
- Represents an activator for a service instance.
- Inject
Builder - 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 - 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
- Defines the behavior of an injectable type.
Functions§
- exactly_
one - Creates a new ServiceDependency with a cardinality of exactly one (1:1).
- exactly_
one_ with_ key - Creates a new keyed ServiceDependency with a cardinality of exactly one (1:1).
- existing
- Creates a new singleton ServiceDescriptor for an existing service instance.
- existing_
as_ self - Creates a new singleton ServiceDescriptor for an existing service instance.
- existing_
with_ key - Creates a new singleton ServiceDescriptor for an existing service instance with a key.
- existing_
with_ key_ as_ self - Creates a new singleton ServiceDescriptor for an existing service instance with a key.
- scoped
- Initializes a new scoped ServiceDescriptorBuilder.
- scoped_
factory - Initializes a new scoped ServiceDescriptor.
- scoped_
with_ key - Initializes a new scoped keyed ServiceDescriptorBuilder.
- scoped_
with_ key_ factory - Initializes a new keyed scoped ServiceDescriptor.
- singleton
- Initializes a new singleton ServiceDescriptorBuilder.
- singleton_
as_ self - Initializes a new singleton ServiceDescriptorBuilder.
- singleton_
factory - Initializes a new singleton ServiceDescriptor.
- singleton_
with_ key - Initializes a new keyed singleton ServiceDescriptorBuilder.
- singleton_
with_ key_ factory - Initializes a new keyed singleton ServiceDescriptor.
- transient
- Initializes a new transient ServiceDescriptorBuilder.
- transient_
as_ self - Initializes a new transient ServiceDescriptorBuilder.
- transient_
factory - Initializes a new transient ServiceDescriptor.
- transient_
with_ key - Initializes a new keyed transient ServiceDescriptorBuilder.
- transient_
with_ key_ as_ self - Initializes a new transient keyed ServiceDescriptorBuilder.
- transient_
with_ key_ factory - Initializes a new keyed transient ServiceDescriptor.
- validate
- Validates the specified ServiceCollection.
- zero_
or_ more - Creates a new ServiceDependency with a cardinality of zero or more (0:*).
- zero_
or_ more_ with_ key - Creates a new keyed ServiceDependency with a cardinality of zero or more (0:*).
- zero_
or_ one - Creates a new ServiceDependency with a cardinality of zero or one (0:1).
- zero_
or_ one_ with_ key - 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
- Represents the metadata used to identify an injected function.
- injectable
- Represents the metadata used to implement the
Injectabletrait.