4 ms·
I wish Rust had a good dependency injection library. More specifically, a library that solves the problem of having long chains of constructors calling each oth
by Kinrany 2y ago
I wish Rust had a good dependency injection library. More specifically, a library that solves the problem of having long chains of constructors calling each other.
The two main use cases are routing frameworks and tests.
Axum already has this, but it is married to HTTP a bit too much.
- tcfhgj 2y agoWhat do you need a DI library for? What's the problem with constructor injection of the dependencies using traits?
- Fluorescence 2y agoI'm curious how mutability is meant to work e.g. in C# I might have a service that caches costly results in memory that I share across a number of other services operating in the same thread. Accessing something that might update it's internal cache needs to be mutable so i) this need for mutability is viral up the call chain ii) we can't share mutable references... so it's going to be a pain in the butt and need to sidestep compile guarantees somehow. Having an out of the box best solution for common scenarios like this would be nice to see at least.
- tcfhgj 2y agoThe solution is interior mutability - the concrete solution depends on the requirements (e.g. throughput)
- solidninja 2y agoThe problem is manual wiring (as always). It is fairly convenient to declare the source of your dependencies (somewhere around main) and have them be automatically wired in the sub-component graph, all without having to write out the chains of code to call constructor parameters. Also simplifies refactoring, as compile-time DI is mostly done on type and not on name or parameter position.