koval

Crates.iokoval
lib.rskoval
version0.5.1
sourcesrc
created_at2022-01-28 18:09:42.585871
updated_at2022-01-30 22:35:07.102357
descriptionA very minimal IOC container
homepagehttps://github.com/Dewyer/koval
repositoryhttps://github.com/Dewyer/koval
max_upload_size
id523187
size11,700
(Dewyer)

documentation

README

Koval

A very simple IOC framework for rust. Runtime, no macros. You can bind types to types and containers to containers to abstract implementation choices.

Usage

use koval::{Container, Injectable, InjectionError};

pub trait FooServiceTrait: Send + Sync {
    fn foo_function(&self) -> bool;
}

pub type IFooService = Arc<dyn FooServiceTrait>;

pub struct FooServiceImpl {}

impl FooServiceTrait for FooServiceImpl {
    fn foo_function(&self) -> bool {
        true
    }
}

impl Injectable<IFooService> for FooServiceImpl {
    fn resolve_injectable(_: &Container) -> Result<IFooService, InjectionError> {
        Ok(Arc::new(FooServiceImpl {}))
    }
}

fn main()  {
    let container = Container::new()
        .bind_singleton::<IFooService, FooServiceImpl>()
        .build()
        .expect("Container failed to build");
    
    let foo_instance = container.resolve::<IFooService>().unwrap();

    assert_eq!(foo_instance.foo_function(), true);
}
Commit count: 9

cargo fmt