runtime_injector icon indicating copy to clipboard operation
runtime_injector copied to clipboard

Allow injectors to have their provider map modified after being built

Open TehPers opened this issue 3 years ago • 0 comments

Suppose a config is reloaded during execution of a program. This would allow services to be reconfigured based on the changes in that config. Currently, it's somewhat possible using factories:

struct FooFactory {
    injector: Injector,
    config: Svc<Mutex<Config>>
}

impl FooFactory {
    pub fn new(injector: Injector, config: Svc<Mutex<Config>>) -> Self {
        FooFactory {
            injector,
            config,
        }
    }

    pub fn get(&self) -> InjectResult<Svc<dyn Foo>> {
        match self.config.lock().unwrap().foo_impl {
            FooImpl::Bar => self.injector.get::<Svc<Bar>>().map(|bar| bar as Svc<dyn Foo>),
            // ...
        }
    }
}

This is a lot of work to reconfigure just a single service based on a config reloaded at runtime. It might be better to instead expose some functions to mutate the provider map in existing Injectors.

TehPers avatar Mar 31 '21 21:03 TehPers