di
di copied to clipboard
There is no way to instantiate named binding via injector as a root
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
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.
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?