#配置 #流程 #依赖注入 #键字符串

instancebuilder

管理依赖注入的便捷方式

2 个不稳定版本

0.2.0 2024年7月30日
0.1.1 2024年3月27日

#586Rust 模式

Download history 14/week @ 2024-05-20 2/week @ 2024-05-27 6/week @ 2024-06-03 127/week @ 2024-07-29

每月 127 次下载

MIT 许可证

9KB
88

instancebuilder

Rust 中依赖注入的便捷解决方案。

安装

cargo add instancebuilder

示例

简单

use ::std::convert::Infallible;
use ::instancebuilder::{Error, InstanceBuilder, FromInstanceBuilder};

// Example set of data of any type. Must implement Send + Sync to be threadsafe.
struct TestConfig {
    key: String,
}

// Your implementation that needs to get build.
struct TestImplementation {
    message: String,
}

impl FromInstanceBuilder for TestImplementation {
    fn try_from_builder(builder: &InstanceBuilder) -> Result<Self, Error> {
        // Put here the code necessary to build the instance
        let config: &TestConfig = builder.data()?;

        Ok(Self {
            message: config.key.clone(),
        })
    }
}

fn main() {
    // Test object to inject. This can be a database pool or a shared instance of something
    let config = TestConfig {
        key: String::from("help me!"),
    };

    let mut  builder = InstanceBuilder::new();
    // Add dependency object to the builder
    builder.insert(config);

    // Build instance of the implementation
    // dependencies are injected by the `FromInstanceBuilder` trait implementation
    let instance = builder.build::<TestImplementation>().unwrap();
}

嵌套依赖

use ::std::convert::Infallible;
use ::instancebuilder::{Error, InstanceBuilder, FromInstanceBuilder};

// Example set of data of any type. Must implement Send + Sync to be threadsafe.
struct TestConfig {
    key: String,
}

// Nested dependent struct
struct InnerTestImplementation {
    message: String,
}

impl FromInstanceBuilder for InnerTestImplementation {
    fn try_from_builder(builder: &InstanceBuilder) -> Result<Self, Error> {
        // Put here the code necessary to build the instance
        // the builder instance contains the initialized data
        let config: &TestConfig = builder.data()?;
        
        Ok(Self {
            message: config.key.clone(),
        })
    }
}

// Outer struct that depends on the nested one
struct OuterTestImplementation {
    inner: InnerTestImplementation,
}

impl FromInstanceBuilder for OuterTestImplementation {
    fn try_from_builder(builder: &InstanceBuilder) -> Result<Self, Error> {
        // Put here the code necessary to build the instance
        Ok(Self {
            // Builds dependency `InnerTestImplementation`
            inner: builder.build()?,
        })
    }
}

fn main() {
    // Test object to inject. This can be a database pool or a shared instance of something
    let config = TestConfig {
        key: String::from("help me!"),
    };

    let mut  builder = InstanceBuilder::new();
    // Add dependency object to the builder
    builder.insert(config);

    // Build instance of the implementation
    // dependencies are injected by the `FromInstanceBuilder` trait implementation
    let instance = builder.build::<OuterTestImplementation>().unwrap();
}

无运行时依赖