Code Monkey home page Code Monkey logo

coi's Introduction

coi

Build Status docs.rs crates.io

Dependency Injection in Rust

The goal of this crate is to provide a simple dependency injection framework that is easy to use. Performance is not an initial concern, but might be later on as the crate matures.

Example

use coi::{container, Inject};
use std::sync::Arc;

// Inherit `Inject` on all traits you'd like to inject
pub trait Trait1: Inject {
    fn describe(&self) -> &'static str;
}

// derive `Inject` on all structs that will provide the implementation
#[derive(Inject)]
#[coi(provides dyn Trait1 with Impl1)]
struct Impl1;

// actually impl the trait
impl Trait1 for Impl1 {
    fn describe(&self) -> &'static str {
        "I'm impl1!"
    }
}

pub trait Trait2: Inject {
    fn deep_describe(&self) -> String;
}

#[derive(Inject)]
#[coi(provides dyn Trait2 with Impl2::new(trait1))]
struct Impl2 {
    // inject dependencies by Arc<dyn SomeTrait>
    #[coi(inject)]
    trait1: Arc<dyn Trait1>,
}

impl Impl2 {
    fn new(trait1: Arc<dyn Trait1>) -> Self {
        Self { trait1 }
    }
}

impl Trait2 for Impl2 {
    fn deep_describe(&self) -> String {
        format!("I'm impl2! and I have {}", self.trait1.describe())
    }
}

// It even works on structs
#[derive(Debug, Inject)]
#[coi(provides JustAStruct with JustAStruct)]
pub struct JustAStruct;

fn main() {
    // Then construct your container with the helper `container!` macro
    let container = container!{
        trait1 => Impl1Provider,
        trait2 => Impl2Provider; scoped,
        struct => JustAStructProvider; singleton
    };

    // And resolve away!
    let trait2 = container
        .resolve::<dyn Trait2>("trait2")
        .expect("Should exist");
    println!("Deep description: {}", trait2.as_ref().deep_describe());
    let a_struct = container
        .resolve::<JustAStruct>("struct")
        .expect("Should exist");
    println!("Got struct! {:?}", a_struct);
}

Name

The name coi comes from an inversion of the initialism IoC (Inversion of Control).

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

SPDX-License-Identifier: MIT OR Apache-2.0

coi's People

Contributors

dispersia avatar nashenas88 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

tobni

coi's Issues

Cannot add field that's not Arc<...>

Currently it's not possible to add a field that's not Arc<...>. This makes sense for fields that are #[inject], but fields that are not #[inject] are also rejected, which should not occur.

The following is rejected, when it should not be:

#[derive(Inject)]
#[provides(Struct with Struct::new())]
struct Struct {
    a: usize,
}

impl Struct {
    fn new() -> Self {
        Self { a: 0 }
    }
}

Possibly redundant code in the readme?

Is this code in the README.md redundant?

       let mut container = container!{
            trait1 => Impl1,
            trait2 => Impl2.scoped,
            struct => JustAStruct.singleton
        };
        let mut container = ContainerBuilder::new()
            .register("trait1", Impl1Provider)
            .register_as("trait2", Registration::Scoped(Impl2Provider))
            .register_as("struct", Registration::Singleton(JustAStructProvider))
            .build();

I've looked at the test cases and they only contain the the first way to construct the container.

Since the code in the readme might be the first impression of coi, I think this should be adressed.

Scope resolution is broken when dependencies are not all scoped

A regression test for this was added

#[test]
fn scoped_registration_provides_same_instance_regardless_of_nesting_order() {
let unique_provider = UniqueProvider::new();
let container = container! {
id => unique_provider.scoped,
hold => HolderProvider.transient,
dep3 => Impl3Provider.scoped,
};
let scoped_container = container.scoped();
let dep3 = scoped_container.resolve::<dyn Dep3>("dep3").unwrap();
let (id1, id2) = dep3.get_ids();
assert_eq!(
id1, id2,
"If the ids are different, they were resolved in different scopes!"
);
}

Add support for generics

It would be nice to have some injected structs that are generic, and the providers would be generic as well.

Example:

#[derive(Inject)]
#[provides(Pool<T> with Pool::new())]
struct Pool<T>(RealPoolType<T>);

impl<T> Pool<T> {
    fn new() -> Self {
      let pool = // construct pool with T
      Self(pool)
    }
}

let container = container! {
    pool => PoolProvider<Tls>
};

let pool = container.resolve::<Pool<Tls>>("pool");

Allow #[inject] attribute on unnamable fields

Currently, tuples and tuple structs cannot be used with #[derive(Inject)] or #[inject]. It should be possible for the #[inject] attribute to get parameters, one of which could be the name to use when injecting. For example:

#[derive(Inject)]
#[provides(dyn Trait1 with Impl1(unnamed))]
struct Impl1(#[inject(unnamed)] Arc<dyn Dep>);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.