abscissa icon indicating copy to clipboard operation
abscissa copied to clipboard

Better dependency injection syntax

Open tarcieri opened this issue 5 years ago • 1 comments

The current dependency injection syntax is pretty gnarly and looks like this:

#[derive(Component, Debug)]
#[component(inject = "init_foo(depname::Foo)")]
#[component(inject = "init_bar(depname::Bar)")]
pub struct MyComponent {
    pub fn init_foo(&mut self, foo: &mut depname::Foo) -> Result<(), FrameworkError> {
        [...[
    }

    pub fn init_bar(&mut self, bar: &mut depname::Bar) -> Result<(), FrameworkError> {
        [...]
    }
}

It'd be nice to be able to use an attribute macro on the "injector" functions instead. Something like this:

#[derive(Component, Debug)]
#[inject(init_foo, init_bar)]
pub struct MyComponent {
    #[inject]
    pub fn init_foo(&mut self, foo: &mut depname::Foo) -> Result<(), FrameworkError> {
        [...[
    }

    #[inject]
    pub fn init_bar(&mut self, bar: &mut depname::Bar) -> Result<(), FrameworkError> {
        [...]
    }
}

It'd be nice if the #[inject] annotations on individual functions were all that were required, but AFAICT, the #[inject(init_foo, init_bar)] is still needed to thunk between them.

tarcieri avatar Aug 06 '19 01:08 tarcieri

it would also be nice to have a single method that accepts multiple dependencies: inject = init(crate::Foo, crate::Bar)

toadzky avatar Nov 20 '21 00:11 toadzky