4 releases

new 0.3.14 Dec 9, 2024
0.3.12 Oct 31, 2024
0.3.9 Jul 25, 2024
0.3.8 Feb 21, 2024
0.2.7 Jun 22, 2023

#1050 in Web programming

Download history 392/week @ 2024-09-30 23/week @ 2024-10-07 25/week @ 2024-10-14 144/week @ 2024-10-28 45/week @ 2024-11-04 4/week @ 2024-11-18 130/week @ 2024-12-02

137 downloads per month
Used in fama

MIT license

66KB
1K SLoC

Busybody

Busybody is a service container and dependency injector for rust application.

Busybody as a service container

Service container example
use busybody::*;

#[derive(Debug)]
struct Config {
  hostname: String
}

fn main() {
  let container = ServiceContainerBuilder::new()
  .service(Config{ hostname: "http://localhost".into() }) // Will be wrapped in Service<T> ie: Arc<T>
  .register(600i32) // left as it is, i32
  .build();

  let config = container.get::<Config>().unwrap(); // When "some", will return Service<Config>
  let max_connection = container.get_type::<i32>().unwrap(); // When "some", will return i32

  println!("config: {:#?}", &config);
  println!("hostname: {:#?}", &config.hostname);
  println!("max connection: {}", max_connection);
}

Busybody as a dependency injector

Service Resolver
use busybody::*;

#[derive(Debug, Clone)]
struct Config {
  hostname: String
}

#[tokio::main]
async fn main() {
// Whenever an instance of Config is needed
// this closure will be called
helpers::resolver(|_container| {
  Box::pin(async {
    Config {
       hostname: "127.0.0.1".to_string(),
    }
  })
});

let _config: Config = helpers::get_type().unwrap(); // Resolve an instance of Config

helpers::resolve_and_call(send_invoices).await; // Resolve all the parameters of "send_invoices" and call it.
}

async fn send_invoices(config: Config) {
println!("sending invoices to: {}", &config.hostname);
}
Dependency injection example
use busybody::*;

#[derive(Debug)]
struct Config {
  hostname: String
}

#[busybody::async_trait]
impl busybody::Injectable for Config { // implementing "Injectable" makes your type callable by the injector 

    async fn inject(_: &ServiceContainer) -> Self {
       Self {
           hostname: "localhost".into()
       }
    }
}


#[tokio::main]
async fn main() {
  let config = helpers::provide::<Config>().await;

  println!("config: {:#?}", &config);
  println!("hostname: {:#?}", &config.hostname);
}
Dependency injection: singleton example
use busybody::*;

#[derive(Debug)]
struct Config {
  hostname: String
}

#[busybody::async_trait]
impl busybody::Injectable for Config { // implementing "Injectable" makes your type injectable by the injector

    async fn inject(_: &ServiceContainer) -> Self {
       Self {
           hostname: "localhost".into()
       }
    }
}


#[tokio::main]
async fn main() {
  let config = helpers::singleton::<Config>().await;

  println!("config: {:#?}", &config);
  println!("hostname: {:#?}", &config.hostname);
}
Dependency injection: call a function/closure passing it all the require arguments
use busybody::{helpers, RawType, Service, ServiceContainerBuilder};

#[tokio::main]
async fn main() {
    // 1. Setup the container
    _ = ServiceContainerBuilder::new()
        .register(200) // Register an i32 value that is not wrapped in Service<T>
        .service(400) // Register an i32 value that is wrapped in Service<T>
        .build();

    // 2. `inject_and_call` calls the provided function/closure, injecting all of it's required parameters
    //     inject_and_call takes a function/closure that expects 0 to 17 arguments
    //     The function **must** be async
    let double_result = helpers::inject_and_call(double).await;
    println!("200 double is: {}", double_result);

    // 3. Same as above but we are making use of "RawType<T>"
    //    RawType<T> tries to find an instance of the specified type. If none exist,
    //    it uses the `default` associate method to create a default instance of the Type.
    //    This means, the "T" in RawType must implement the `Default` trait.
    let sum = helpers::inject_and_call(|raw_i32: RawType<i32>, service_i32: Service<i32>| async {
        raw_i32.into_inner() + *service_i32.into_inner()
    })
    .await;
    println!("Service<200> + RawType<400> = {}", sum);
}

// 4. Function is taken an I32.
async fn double(count: i32) -> i32 {
    count * 2
}

Examples

The examples folder contains simple and full examples. If none of the examples are helpful, please reach out with your use case and I try to provide one.

Feedback

If you find this crate useful, please star the repository. Submit your issues and recommendations as well.

License

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Dependencies

~2.9–9.5MB
~82K SLoC