1 unstable release

0.1.0 Dec 20, 2023

#37 in #dependency-injection

Download history 5/week @ 2024-07-14 2/week @ 2024-07-21 1/week @ 2024-07-28 2/week @ 2024-08-25 1/week @ 2024-09-01 1/week @ 2024-09-15 16/week @ 2024-09-22 33/week @ 2024-09-29 2/week @ 2024-10-06

52 downloads per month
Used in 3 crates (via adrift_core)

MIT license

23KB
457 lines

Silhouette

A simple dependency injection library for Rust

crates.io download count badge docs.rs

About Silhouette

Silhouette implements a simple service container in Rust for dependency injection. It not only provides a Container struct for local usage, but also a static interface (under facade::Container) for easily managing dependencies throughout your application. It's heavily inspired by Laravel's Service Container.

Getting Started

use silhouette::facade::Container;

struct DBPool {}
struct DBConnection {}

// will always use the same pool
Container::singleton(&|_| DBPool::new())?;

// will resolve a new connection each time
Container::bind(&|container| -> DBConnection {
    let shared_pool = container.resolve::<DBPool>().unwrap();

    shared_pool.get_conn()
})?;

// somewhere else in your app...
let connection: DBConnection = Container::resolve()?;

Refer to the documentation on docs.rs for detailed usage instructions.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Dependencies

~245–700KB
~16K SLoC