Crate silhouette

source ·
Expand description

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.

Usage

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()?;

Features

  • nightly - Automatically resolves types that implement Default. Requires the nightly compiler.

Modules

  • A static interface for the service container.

Structs

Enums

  • An error that can occur when interacting with the container.