di icon indicating copy to clipboard operation
di copied to clipboard

There is no way to instantiate named binding via injector as a root

Open kanstantsin-chernik opened this issue 7 years ago • 2 comments

auto injector = di::make_injector(
      di::bind<interface>().named(SomeName).to<impelentation1>(),
      di::bind<interface>().to<impelentation2>()
);
auto intf = injector->create<interface>();

In such case there is no way to ask injector for particular named version of interface

kanstantsin-chernik avatar Feb 14 '18 16:02 kanstantsin-chernik

Right, that's was by design. Anyway, this issue will go away with DIv2 where named parameters using macros will go away (including .named).

Instead, a strong typedef named library will be used (any). Also, provided as extension::named in DIv2 but it's [Boost].DI main responsibility, though.

Example:

auto injector = di::make_injector(
      di::bind<named<interface, SomeName>>().to<impelentation1>(),
      di::bind<interface>().to<impelentation2>()
);
auto intf = injector->create<named<interface, SomeName>>();

With the strong typedefs, it might the named part might be also rewritten more clearly.

using NamedInterface = named<interface, class SomeName>;

and then

Example::Example(std::unique_ptr<NamedInterface>);

Finally, with DIv2 BOOST_DI_NAMED and BOOST_DI_INJECT will go away.

krzysztof-jusiak avatar Feb 15 '18 09:02 krzysztof-jusiak

Being able to call create with a name is a common use-case when binding to a factory (e.g. shared_factory from extensions). The injector class already has a create_successful_impl method which deals with di::named -- can that be exposed through a special create method?

andreasdamm avatar Feb 16 '20 19:02 andreasdamm