[][src]Crate inject

Library for dependency injection

Inject implements macros for dependency resolution, attempting to promote the Inversion of control (IoC) principle.

Quick Start

To get you started quickly, there are 3 procedural macros and one attribute macro to keep track of: container!, get!, call! and #[inject].

container! constructs a new container with providers.

get! resolves a type using a container.

call! invokes a function using a container.

#[inject] generates code to enable the above two macros.

Example

use ::inject::*;

struct Connection(isize);

impl Connection {
    #[inject]
    fn new(foo: isize) -> Self {
        Self(foo)
    }
}

struct Instance(isize);

impl Instance {
    #[inject]
    fn new(conn: &Connection) -> Self {
        Self(conn.0)
    }
}
let conn = Box::new(Connection(2));
let container = container![
    ref conn
];

let instance = get!(&container, Instance).unwrap();

assert_eq!(instance.0, 2)

The container resolves the dependencies of the Instance struct, using the installed provider to resolve the &Connection dependency.

Re-exports

pub use error::InjectError;
pub use provider::Provider;
pub use provider::RefProvider;
pub use crate::inject::Inject;
pub use crate::inject::InjectExt;

Modules

error

Errors due to injection resolution failure

inject

Injectable types

module
provider

Provides types

providers

Macros

call

Call a function with dependency resolution for its arguments

container

Create a container with providers

get

Resolve a dependency from a container

singleton

Structs

Container

Contains providers for resolvable types.

Attribute Macros

inject

Generate functionality for a function/constructor to be injectable