[][src]Crate teloc

There are 2 types can be provider of services: ServiceProvider and Scope. First used as store for dependencies with Instance and Singleton lifetimes, and for declaring all dependencies using .add_*() methods. Scope can be created from ServiceProvider object by calling method ServiceProvider::scope.

There are four lifetimes for dependencies:

  1. Transient. Service will be created when resolves. Can depend on dependencies with anything lifetime. If depend on dependency with Scoped lifetime can be resolves only from Scope object.
  2. Scoped. Service will be created once at Scope when it resolved (lazy). Can depend on dependencies with anything lifetime.
  3. Singleton. Service will be created once at ServiceProvider when it resolved (lazy). Can depend on dependencies with anything lifetime exclude Scoped.
  4. Instance. Dependency was created outside of ServiceProvider.

Process of working with library:

  1. Define your structs.
  2. Create constructors and add #[inject] macro on its.
  3. Create a ServiceProvider object.
  4. Add your services and dependencies using ServiceProvider::add_* methods.
  5. Create Scope if need.
  6. Get service from container using .resolve() method.
  7. Work with service.

Example:

use teloc::*;

struct ConstService {
    number: i32,
}
#[inject]
impl ConstService {
    pub fn new(number: i32) -> Self {
        ConstService { number }
    }
}

// derive macro can be used when all fields implement `Dependency` trait,
// but we do not recommend use it in production code
#[derive(Dependency)]
struct Controller {
    number_service: ConstService,
}

let container = ServiceProvider::new()
    .add_transient::<ConstService>()
    .add_transient::<Controller>();
let scope = container.fork().add_instance(10);
let controller: Controller = scope.resolve();
assert_eq!(controller.number_service.number, 10);

Modules

container

This is a section for advanced usage. For common usage you can not read this page.

get_dependencies

This is a section for advanced usage. For common usage you can not read this page.

Structs

ServiceProvider

ServiceProvider struct is used as an IoC-container in which you declare your dependencies.

Traits

Dependency

Trait is used to working with Resolver trait. If you want that your service can be resolved by Resolver, you may implement this trait for your service. There are three ways:

Resolver

This trait is used to resolve some object from service provider. Generic T used only to avoid absence of specialization and for working of type inference. You must implement it yourself only when you implement your own version of container.

Attribute Macros

inject

Macro can be used on free functions and impls, including impl traits, with only one implement method. It will generate Dependency impl in which calling function that will tagged by this macro. We recommend using this macro in production code.

Derive Macros

Dependency

Derive macro can be used on structs with named fields when all fields implements Dependency trait or fields described using #[init(...)] attr. We do not recommend using this macro in production code.