A Rust Dependency Injection (DI) library focused on simplicity and composability. Key features include:
This library enables efficient management of complex projects with numerous nested structures by assembling various application components, such as configurations, database connections, payment service clients, Kafka connections, and more. My DI keeps dependency management organized, easy to read, and expandable, providing a solid foundation for the growth of your project.
Simply add the dependency to your Cargo.toml:
toml
[dependencies]
mydi = "0.1.1"
Approaches using separate mechanisms for DI are common in other languages like Java and Scala, but not as widespread in Rust. To understand the need for this library, let's look at an example without My DI and one with it. Let's build several structures (Rust programs sometimes consist of hundreds of nested structures) in plain Rust.
```rust struct A { x: u32 }
impl A { pub fn new(x: u32) -> Self { Self { x } } }
struct B { x: u64 }
impl A { pub fn new(x: u64) -> Self { Self { x } } }
struct C { x: f32 }
impl A { pub fn new(x: f32) -> Self { Self { x } } }
struct D { a: A, b: B, c: C }
impl D { pub fn new(a: A, b: B, c: C) -> Self { Self { a, b, c } } pub fn run(self) { todo!() } }
fn main() { let a = A::new(1); let b = B::new(2); let c = C::new(3f64); let d = D::new(a, b, c); d.run() } ```
As you can see, we write each argument in at least 4 places:
Now let's try to simplify all this with My DI:
```rust use mydi::{InjectionBinder, Component};
struct A { x: u32 }
struct B { x: u64 }
struct C { x: f32 }
struct D { a: A, b: B, c: C }
impl D { pub fn run(self) { todo!() } }
fn main() -> Result<(), Box
As a result, we reduced the amount of code, removed unnecessary duplication, and left only the essential code. We also opened ourselves up to further code refactoring (which we will discuss in the following sections):
The library resolves dependencies at runtime, as otherwise, it would be impossible to implement features like cyclic dependencies and arbitrary initialization order. This means that dependency resolution needs to be checked somehow, and for this purpose, a test should be added. This is done very simply. To do this, you just need to call the verify method. In general, it's enough to call it after the final assembly of dependencies. For example, like this:
```rust
use mydi::{InjectionBinder, Component};
fn build_dependencies(config: MyConfig) -> InjectionBinder<()> { todo!() }
mod testdependenciesbuilding { use std::any::TypeId; use seaorm::DatabaseConnection; use crate::{builddependencies, config}; use std::collections::HashSet;
#[test]
fn test_dependencies() {
let cfg_path = "./app_config.yml";
let app_config = config::parse_config(&cfg_path).unwrap();
let modules = build_dependencies(app_config);
let initial_types = HashSet::from([ // types that will be resolved somewhere separately, but for the purposes of the test, we add them additionally
TypeId::of::<DatabaseConnection>(),
TypeId::of::<reqwest::Client>()
]);
// the argument true means that in the errors, we will display not the full names of the structures, but only the final ones
// if you are interested in the full ones, you should pass false instead
modules.verify(initial_types, true).unwrap();
}
} ```
How to organize a project with many dependencies? It may depend on your preferences, but I prefer the following folder structure:
- main.rs
- modules
-- mod.rs
-- dao.rs
-- clients.rs
-- configs.rs
-- controllers.rs
-- services.rs
-- ...
This means that there is a separate folder with files for assembling dependencies, each responsible for its own set of services in terms of functionality. Alternatively, if you prefer, you can divide the services not by functional purpose, but by domain areas:
- main.rs
- modules
-- mod.rs
-- users.rs
-- payments.rs
-- metrics.rs
-- ...
Both options are correct and will work, and which one to use is more a matter of taste.
In each module, its own InjectionBinder
will be assembled, and in main.rs, there will be something like:
```rust
use mydi::{InjectionBinder, Component};
struct MyApp {}
impl MyApp { fn run(&self) { todo!() } }
fn mergedependencies() -> Result
fn main() -> Result<(), Box
```
So, how will the modules themselves look? This may also depend on personal preferences. I prefer to use configurations as specific instances.
```rust use mydi::InjectionBinder;
pub fn builddependencies(appconfigpath: &str,
kafkaconfigpath: &str) -> Result
Ok(result)
} ```
Meanwhile, the module for controllers might be assembled like this:
```rust
use mydi::{InjectionBinder, Component};
pub fn build_dependencies() -> InjectionBinder<()> {
InjectionBinder::new()
.inject::
Note the .void()
at the end. After each component is added to the InjectionBinder
, it changes its internal
type to the one that was passed. Therefore, to simplify working with types, it makes sense to convert to the type ()
,
and that's what the .void()
method is used for.
To add dependencies, the best way is to use the derive macro Component:
```rust use mydi::{InjectionBinder, Component};
struct A { x: u32, y: u16, z: u8, } ```
It will generate the necessary ComponentMeta
macro, and after that, you can add dependencies through the inject
method:
rust
fn main() -> Result<(), Box<dyn std::error::Error>> {
let injector = InjectionBinder::new()
.instance(1u32)
.instance(2u16)
.instance(3u8)
.inject::<A>()
.build()?;
todo!()
}
In some cases, using macros may be inconvenient, so it makes sense to use functions instead.
For this, use the inject_fn
method:
```rust use mydi::{InjectionBinder, Component};
struct A { x: u32, }
struct B { a: A, x: u32, }
struct C { b: B, a: A, x: u64, }
fn main() -> Result<(), Box
let x = inject.get::<C>()?;
} ```
Take note of the parentheses in the arguments. The argument here accepts a tuple. Therefore, for 0 arguments, you need
to write
the arguments like this |()|
, and for a single argument, you need to write the tuple in this form |(x, )|
.
To add a default value, you can use the directive #[component(...)]
.
Currently, there are only 2 available options: #[component(default)]
and #[component(default = my_func)]
,
where my_func is a function in the scope. #[component(default)]
will substitute the value as
Default::default()
For example, like this:
```rust
struct A { #[component(default)] x: u32, #[component(default = custom_default)] y: u16, z: u8, }
fn custom_default() -> u16 { todo!() } ```
Note that custom_default is called without parentheses ()
. Also, at the moment, calls from nested modules
are not supported, meaning foo::bar::custom_default
will not work. To work around this limitation,
simply use use
to bring the function call into scope.
As a result of dependency assembling, an injector is created, from which you can obtain the dependencies themselves. Currently, there are 2 ways to get values: getting a single dependency and getting a tuple.
```rust
use mydi::{InjectionBinder, Component};
struct A {}
struct B {}
fn main() -> Result<(), Box
let a: A = injector.get()?; // getting a single value
let (a, b): (A, B) = injector.get_tuple()?; // getting a tuple
todo!()
} ```
Currently, tuples up to dimension 18 are supported.
Generics in macros are also supported, but with the limitation that they must implement
the Clone
trait and have a 'static
lifetime:
```rust
use mydi::{InjectionBinder, Component};
struct A
fn main() -> Result<(), Box
let a: A = injector.get::<A<u32>>()?;
todo!()
} ```
In some complex situations, there is a need to assemble circular dependencies. In a typical situation, this leads to an exception and a build error. But for this situation, there is a special Lazy type.
It is applied simply by adding it to the inject method:
```rust use mydi::{InjectionBinder, Component, Lazy};
struct A { x: Lazy }
struct B { x: A, y: u32 }
fn main() -> Result<(), Box
let a: A = injector.get::<A>()?;
todo!()
} ```
Also, it's worth noting that nested lazy types are prohibited
In some cases, it makes sense to abstract from the type and work with Arc
For example, like this:
```rust use mydi::{InjectionBinder, Component, erase};
pub struct A { x: u32, }
trait Test { fn x(&self) -> u32; }
impl Test for A { fn x(&self) -> u32 { self.x } }
fn main() -> Result<(), Box
What's happening here? auto
is simply adding a new dependency based on the previous type without adding
it to the InjectionBinder's type. In other words, you could achieve the same effect by
writing .inject_fn(|(x, )| -> Arc<dyn Test> { Arc::new(x) })
,
but doing so would require writing a lot of boilerplate code, which you'd want to avoid.
Why might we need to work with dyn traits
?
One reason is to abstract away from implementations and simplify the use of mocks, such as those from the (
mockall)[https://github.com/asomers/mockall] library.
But if you need to use something like Box
instead of Arc
, you need to use the library (
dyn-clone)[https://github.com/dtolnay/dyn-clone]
```rust use mydi::{InjectionBinder, Component}; use dyn_clone::DynClone;
pub struct A { x: u32, }
trait Test: DynClone { fn x(&self) -> u32; }
dynclone::clonetrait_object!(Test);
impl Test for A { fn x(&self) -> u32 { self.x } }
fn main() -> Result<(), Box
Since we store type information inside InjectionBinder, we can automatically create implementations for the type T
for containers Arc
```rust
struct MyStruct {}
struct MyNestedStruct {
mystructbox: Box
fn main() -> Result<(), Box
Also, if there is a Component annotation, then the type inside Arc<...> can be passed directly to the inject method.
For example, like this:
.inject<Box<MyStruct>>
It is important to note that the original type will still be available and will not be removed.
In some situations, it is necessary to use multiple instances of the same type, but by default, the assembly will fail with an error if two identical types are passed. However, this may sometimes be necessary, for example, when connecting to multiple Kafka clusters, using multiple databases, etc. For these purposes, you can use generics or tagging.
Example using generics:
```rust
struct MyService
fn main() -> Result<(), Box
You can also use tagging. For this purpose, there is a special Tagged structure that allows you to wrap structures in tags. For example, like this:
```rust // This type will be added to other structures
struct MyKafkaClient {}
// These are tags, they do not need to be created, the main thing is that there is information about them in the type struct Tag1;
struct Tag2;
struct Service1 {
kafka_client: Tagged
struct Service2 {
kafka_client: Tagged
fn main() -> Result<(), Box
The Tagged type implements std::ops::Deref, which allows you to directly call methods of the nested object through it.
Current implementation limitations:
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Any contribution is welcome. Just write tests and submit merge requests on GitLab. All others merge requests will be closed with a request to add a merge request on GitLab.