zihantype / rudi Goto Github PK
View Code? Open in Web Editor NEWRudi - an out-of-the-box dependency injection framework for Rust -- Rudi,一个开箱即用的 Rust 依赖注入框架
License: Apache License 2.0
Rudi - an out-of-the-box dependency injection framework for Rust -- Rudi,一个开箱即用的 Rust 依赖注入框架
License: Apache License 2.0
这种方式实现的耦合性太高,没有基于trait。可使用anymap2基于trait进行注入
In current version, If I need to inject a i32 for a struct field without specified dependency name, I need to type #[di()]
Here's my usecase:
#[Singleton]
async fn Number() -> i32 {
452
}
#[Singleton(async)]
struct Config {
#[di()]
number: i32,
#[di(default = 123)]
test: u32,
}
It would be better to write it just as #[di]
.
Meanwhile, if I don't need to specify test
to 123
, I can just remove the #[di(default = 123)], and derive Default
for Config
, tell the provider to use u32::default() for initializing this field
The better syntax may be like this:
#[Singleton]
async fn Number() -> i32 {
452
}
#[derive(Default)]
#[Singleton(async)]
struct Config {
#[di]
number: i32,
test: u32,
}
看到inventory 这里说了另外一种插件机制的实现方式。
没有别的意思,就是单处好奇,这两种有啥区别?
此外 我写了一个ioc的玩具,但是用了很多unsafe,想问问 有啥继续的价值吗?
https://github.com/lagudomeze/ioc/blob/poc/examples/hello.rs
求联系方式。
try_resolve_xxx
or try_get_xxx
to get the instanceExample code:
use rudi::Context;
use rudi_macro::Singleton;
#[Singleton(eager_create)]
fn Run() {
println!("Run()");
}
#[test]
fn singleton_owned() {
Context::auto_register();
Context::options().eager_create(false).auto_register();
Context::options().eager_create(false).allow_only_single_eager_create(false).auto_register();
}
Anticipation:
Only the first one that defaults to true executes the Run, the rest of the ones specified as false do not execute anything with eager_create
The code in the red box in the figure and is seen to validate the eager_create configuration.
I also have a question for you.
Why is the function call in the red box in the above picture called twice in flash, is there any special mechanism? I'm sorry I don't have a thorough understanding of the project's source code, so I'd like to ask
When inject a specific field from a struct, for example, I think it would be better to code like this:
// Register async function and specify name
#[Singleton(name = "number")]
async fn Number() -> u32 {
42
}
#[derive(Debug, Clone)]
#[Singleton(name = "config")] // Register async constructor and specify name
struct Config{
#[di(name = "number")] // Specify the name of the dependency
max_user: u32,
}
#[derive(Debug)]
struct Bar(i32);
impl Bar {
fn into_debug(self) -> Rc<dyn Debug> {
Rc::new(self)
}
}
#[Transient(binds = [Self::into_debug])] // Bind the implementation of the `Debug` trait and the trait object of the `Debug` trait
impl Bar {
async fn new(#[di(name = "config.max_user")] max_user: u32) -> Bar { // Register async constructor
Bar(max_user)
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.