Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kanstantsin-chernik opened this issue Feb 14, 2018 · 2 comments

Comments

@kanstantsin-chernik
Copy link
Collaborator

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

@kris-jusiak
Copy link
Collaborator

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.

@andreasdamm
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants