boost-ext / di

C++14 Dependency Injection Library
https://boost-ext.github.io/di
1.13k stars 137 forks source link

Potential misunderstanding of the problem which [Boost].DI solves #471

Open MarvLelgemann opened 4 years ago

MarvLelgemann commented 4 years ago

I am not sure if I misunderstand the problem which [Boost].DI solves.

I was trying to use it to avoid passing along dependencies through chains of constructors. E.g. Component A needs to create Component B at runtime, but Component B has a dependency on Service A (shared with other components). I thought I could avoid that Component A has to keep a reference to Service A, but let [Boost].DI inject Service A into Component B automagically.

My expectation was, that when I create an injector instance at startup (e.g. in main) to instantiate a static object tree, this injector would share state (has a reference to the shared Service A) with another injector instance which I create in some class (e.g. a factory) later at runtime. This didn't work for me, Service A was always constructed twice (even though the constructor parameter is a std::shared_ptr). If I would need to pass around the injector to achieve that, I could also pass around the dependencies (or a factory which carries the dependencies).

Is this a use case where [Boost].DI can help? Maybe I just overlooked something, do I need to use one of the extensions to do that?