diff --git a/README.md b/README.md index 31b4a25..fd6a180 100644 --- a/README.md +++ b/README.md @@ -58,26 +58,33 @@ Example: ```rust use teloc::*; +struct ConstServiceConfig { + config_value: i32, +} + // Declare your structs -struct ConstService { - number: i32, +struct ConstService<'a> { + config: &'a ConstServiceConfig, } -// #[inject] macro is indicate that dependency can be constructed using this function + +// #[inject] macro is indicate that dependency can be constructed using this +// function #[inject] -impl ConstService { - pub fn new(number: i32) -> Self { - ConstService { number } +impl<'a> ConstService<'a> { + pub fn new(config: &'a ConstServiceConfig) -> Self { + ConstService { config } } } -// Derive macro can be used when all fields implement `Dependency` trait, but +// Derive macro can be used when all fields implement `Dependency` trait, but // we recommend using the #[inject] macro it in production code instead. #[derive(Dependency)] -struct Controller { - number_service: ConstService, +struct Controller<'a> { + number_service: &'a ConstService<'a>, } -fn main() { +#[test] +fn test() { // Create `ServiceProvider` struct that store itself all dependencies let sp = ServiceProvider::new() // Add dependency with `Singleton` lifetime. More about lifetimes see above. @@ -90,10 +97,10 @@ fn main() { // .fork() method creates a local mutable scope with self parent immutable `ServiceProvider`. .fork() // Add an instance of `i32` that will be used when `ConstService` will be initialized. - .add_instance(10); + .add_instance(ConstServiceConfig { config_value: 10 }); // Get dependency from `ServiceProvider` - let controller: Controller = scope.resolve(); - assert_eq!(controller.number_service.number, 10); + let controller: Controller = scope.resolve(); // fails here on resolve() + assert_eq!(controller.number_service.config.config_value, 10); } ``` diff --git a/teloc/tests/example.rs b/teloc/tests/example.rs new file mode 100644 index 0000000..be245fc --- /dev/null +++ b/teloc/tests/example.rs @@ -0,0 +1,46 @@ +use teloc::*; + +struct ConstServiceConfig { + config_value: i32, +} + +// Declare your structs +struct ConstService<'a> { + config: &'a ConstServiceConfig, +} + +// #[inject] macro is indicate that dependency can be constructed using this +// function +#[inject] +impl<'a> ConstService<'a> { + pub fn new(config: &'a ConstServiceConfig) -> Self { + ConstService { config } + } +} + +// Derive macro can be used when all fields implement `Dependency` trait, but +// we recommend using the #[inject] macro it in production code instead. +#[derive(Dependency)] +struct Controller<'a> { + number_service: &'a ConstService<'a>, +} + +#[test] +fn test() { + // Create `ServiceProvider` struct that store itself all dependencies + let sp = ServiceProvider::new() + // Add dependency with `Singleton` lifetime. More about lifetimes see above. + .add_singleton::() + // Add dependency with `Transient` lifetime. More about lifetimes see above. + .add_transient::(); + // Fork `ServiceProvider`. It creates a new `ServiceProvider` which will have + // access to the dependencies from parent `ServiceProvider`. + let scope = sp + // .fork() method creates a local mutable scope with self parent immutable `ServiceProvider`. + .fork() + // Add an instance of `i32` that will be used when `ConstService` will be initialized. + .add_instance(ConstServiceConfig { config_value: 10 }); + // Get dependency from `ServiceProvider` + let controller: Controller = scope.resolve(); // fails here on resolve() + assert_eq!(controller.number_service.config.config_value, 10); +}