Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nintha/autowired-rs
rust dependency injection
https://github.com/nintha/autowired-rs
autowired dependency-injection rust
Last synced: about 1 month ago
JSON representation
rust dependency injection
- Host: GitHub
- URL: https://github.com/nintha/autowired-rs
- Owner: nintha
- License: apache-2.0
- Created: 2021-03-13T14:01:28.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-07-05T01:35:36.000Z (over 3 years ago)
- Last Synced: 2024-11-07T01:07:00.266Z (about 2 months ago)
- Topics: autowired, dependency-injection, rust
- Language: Rust
- Homepage:
- Size: 34.2 KB
- Stars: 12
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Autowired
[![crates.io](https://img.shields.io/crates/v/autowired.svg)](https://crates.io/crates/autowired)
[![docs.rs](https://docs.rs/autowired/badge.svg)](https://docs.rs/autowired)Rust dependency injection project, inspired by `Spring IOC`.
## Add Dependency
```toml
[dependencies]
autowired="0.1"
```## Usage
Just derive your struct with the marco `Component`, you can use the singleton component everywhere.
```rust
#[derive(Default, Component)]
struct Bar {
name: String,
age: u32,
}fn main() {
// central registration in the beginning of the program
setup_submitted_beans();// create `bar` via Default::default
let bar: Autowired = Autowired::new();assert_eq!(String::default(), bar.name);
assert_eq!(u32::default(), bar.age);
}
```Define custom component initialization logic
```rust
struct Goo { pub list: Vec }#[autowired::bean]
fn build_goo() -> Goo {
Goo { list: vec!["hello".to_string()] }
}fn main() {
// central registration in the beginning of the program
setup_submitted_beans();let goo = Autowired::::new();
assert_eq!("hello", goo.list[0])
}
```## Lazy components
By default, components are registered with `setup_submitted_beans`.
Use `#[bean(lazy)]` to register components lazily. The lazy components will be registered when be used.```rust
use std::sync::Arc;
use autowired::{ LazyComponent, setup_submitted_beans, bean, Autowired};#[allow(dead_code)]
#[derive(Default, LazyComponent)]
struct Bar {
name: Arc,
age: u32,
}#[allow(dead_code)]
struct Goo { pub list: Vec }#[bean(lazy)]
fn build_goo() -> Goo {
Goo { list: vec!["hello".to_string()] }
}#[test]
fn lazy() {
setup_submitted_beans();assert!(!autowired::exist_component::());
assert!(!autowired::exist_component::());let bar = Autowired::::new();
assert!( bar.name.is_empty());let goo = Autowired::::new();
assert_eq!("hello", goo.list[0]);assert!(autowired::exist_component::());
assert!(autowired::exist_component::());
}
```## Option components
Functional bean constructor can return `Option` with attribute `#[bean(option)]`.
if return value is `None`, this bean will not be submitted.
if you like, this feature can work with lazy components, `#[bean(option, lazy)]`.
```rust
#[allow(dead_code)]
struct Bar {
name: String,
}/// return `None`, this bean will not be submitted
#[bean(option)]
fn build_bar_none() -> Option {
None
}#[allow(dead_code)]
struct Goo {
pub list: Vec,
}#[bean(option)]
fn build_goo_some() -> Option {
Some(Goo { list: vec!["hello".to_string()] })
}#[test]
fn option() {
setup_submitted_beans();assert!(!autowired::exist_component::());
assert!(autowired::exist_component::());let goo = Autowired::::new();
assert_eq!("hello", goo.list[0]);
}```