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:
Transient. Service will be created when resolves. Can depend on dependencies with anything lifetime. If depend on dependency withScopedlifetime can be resolves only fromScopeobject.Scoped. Service will be created once atScopewhen it resolved (lazy). Can depend on dependencies with anything lifetime.Singleton. Service will be created once atServiceProviderwhen it resolved (lazy). Can depend on dependencies with anything lifetime excludeScoped.Instance. Dependency was created outside ofServiceProvider.
Process of working with library:
- Define your structs.
- Create constructors and add
#[inject]macro on its. - Create a
ServiceProviderobject. - Add your services and dependencies using
ServiceProvider::add_*methods. - Create
Scopeif need. - Get service from container using
.resolve()method. - Work with service.
Example:
use *;
// derive macro can be used when all fields implement `Dependency` trait,
// but we do not recommend use it in production code
let container = new
.
.;
let scope = container.fork.add_instance;
let controller: Controller = scope.resolve;
assert_eq!;