Crate di

Source
Expand description

§More Dependency Injection   CI Crates.io MIT licensed

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§

Activatorinject
Represents an activator for a service instance.
InjectBuilderinject
Represents the builder for an injected type.
KeyedRef
Represents a holder for a keyed service.
ScopedServiceProvider
Represents a scoped ServiceProvider.
ServiceCollection
Represents a service collection.
ServiceDependency
Represents a service dependency.
ServiceDescriptor
Represents the description of a service with its service type, implementation, and lifetime.
ServiceDescriptorBuilderbuilder
Represents a ServiceDescriptor builder.
ServiceProvider
Represents a service provider.
Type
Represents a type.
ValidationError
Represents an validation error.

Enums§

ServiceCardinality
Represents the possible cardinalities of a service dependency.
ServiceLifetime
Represents the possible service lifetimes.

Traits§

Injectableinject
Defines the behavior of an injectable type.

Functions§

exactly_onebuilder
Creates a new ServiceDependency with a cardinality of exactly one (1:1).
exactly_one_with_keybuilder
Creates a new keyed ServiceDependency with a cardinality of exactly one (1:1).
existingbuilder
Creates a new singleton ServiceDescriptor for an existing service instance.
existing_as_selfbuilder
Creates a new singleton ServiceDescriptor for an existing service instance.
existing_with_keybuilder
Creates a new singleton ServiceDescriptor for an existing service instance with a key.
existing_with_key_as_selfbuilder
Creates a new singleton ServiceDescriptor for an existing service instance with a key.
scopedbuilder
Initializes a new scoped ServiceDescriptorBuilder.
scoped_factorybuilder
Initializes a new scoped ServiceDescriptor.
scoped_with_keybuilder
Initializes a new scoped keyed ServiceDescriptorBuilder.
scoped_with_key_factorybuilder
Initializes a new keyed scoped ServiceDescriptor.
singletonbuilder
Initializes a new singleton ServiceDescriptorBuilder.
singleton_as_selfbuilder
Initializes a new singleton ServiceDescriptorBuilder.
singleton_factorybuilder
Initializes a new singleton ServiceDescriptor.
singleton_with_keybuilder
Initializes a new keyed singleton ServiceDescriptorBuilder.
singleton_with_key_factorybuilder
Initializes a new keyed singleton ServiceDescriptor.
transientbuilder
Initializes a new transient ServiceDescriptorBuilder.
transient_as_selfbuilder
Initializes a new transient ServiceDescriptorBuilder.
transient_factorybuilder
Initializes a new transient ServiceDescriptor.
transient_with_keybuilder
Initializes a new keyed transient ServiceDescriptorBuilder.
transient_with_key_as_selfbuilder
Initializes a new transient keyed ServiceDescriptorBuilder.
transient_with_key_factorybuilder
Initializes a new keyed transient ServiceDescriptor.
validate
Validates the specified ServiceCollection.
zero_or_morebuilder
Creates a new ServiceDependency with a cardinality of zero or more (0:*).
zero_or_more_with_keybuilder
Creates a new keyed ServiceDependency with a cardinality of zero or more (0:*).
zero_or_onebuilder
Creates a new ServiceDependency with a cardinality of zero or one (0:1).
zero_or_one_with_keybuilder
Creates a new keyed ServiceDependency with a cardinality of zero or one (0:1).

Type Aliases§

KeyedRefMut
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.
ServiceFactory
Represents the callback function used to create a service.

Attribute Macros§

injectinject
Represents the metadata used to identify an injected function.
injectableinject
Represents the metadata used to implement the Injectable trait.