Skip to main content

Crate di

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. 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.
InjectBuilder
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.
ServiceDescriptorBuilder
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§

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§

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§

inject
Represents the metadata used to identify an injected function.
injectable
Represents the metadata used to implement the Injectable trait.