boost-ext / di

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

[Question] inject injector #478

Closed hellozyemlya closed 4 years ago

hellozyemlya commented 4 years ago

Is that possible to inject injector?

kanstantsin-chernik commented 4 years ago

Yes, although the approach is not perfect (mostly due to C++ rigidness). Take a look at lazy extension for example: https://github.com/boost-experimental/di/blob/cpp14/extension/include/boost/di/extension/injections/lazy.hpp

krzysztof-jusiak commented 4 years ago

Yeah, as @kanstantsin-chernik mentions, it's a bit clunky but you can do it with

struct ctor {
  using boost_di_inject__ = inject<self<T>>;

  template <class TInjector>
  explicit ctor(const TInjector &);
};

Although, please be aware that ServiceLocator is an anti-pattern and it's much better to inject dependencies instead :+1:

You can read more about it here -> https://boost-experimental.github.io/di/