Skip to content

Multiple bindings should allow duplicates and merge the bindings #340

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

Open
azureblaze opened this issue Jul 19, 2018 · 2 comments
Open

Multiple bindings should allow duplicates and merge the bindings #340

azureblaze opened this issue Jul 19, 2018 · 2 comments

Comments

@azureblaze
Copy link

Similar to dagger multiple binding
https://google.github.io/dagger/multibindings.html

auto base = [] {
  di::makeInjector(
    di::bind(int []).to({1})
  );
};

auto injector = di::make_injector(
  base(),
  di::bind(int []).to({2});
);
auto s = injector.create<std::set<int>>();
assert(s.size() == 2);
assert(s.count(1) == 1);
assert(s.count(2) == 1);

This allows a client to inject event listeners into a library.
for example,

class Interface{};
using Factory = function<unique_ptr<Interface>()>;
struct FactoryEntry{
  stirng key;
  Factory factory;
};
struct CreateWithStringFactory{
  map<string,  Factory> factories;
  CreateWithStringFactory(set<FactoryEntry> entries) {
    for(auto entry : entries){
      map[entry.name] = entry.factory
    }
  }
  
  unique_ptr<Interface> create(const string name&) {
    return map[name]();
  }
};

auto libraryModule = []() { 
  return di::make_injector(
      di::bind<Factory []>.to(FactoryEntry{"foo", di::extension::assisted_injection<Foo>()})
  );
};

...

auto injector = di::make_injector(
  libraryModule (),
  di::bind<Factory []>.to(FactoryEntry{"bar", di::extension::assisted_injection<Bar>()})
);

auto createWithStringFactory = di.create<CreateWithStringFactory>();

assert(dynamic_cast<Bar>(createWithStringFactory.create("bar")) != nullptr);
@kanstantsin-chernik
Copy link
Collaborator

The idea is cool but it is C++. I am not sure it is possible :)
May be Kris know some magic for it though.

@dwkimm01
Copy link

dwkimm01 commented Jan 15, 2021

The idea is cool but it is C++. I am not sure it is possible :)
May be Kris know some magic for it though.

I believe I was able to get it working for the runtime_provider. Sorry my code needs a lot of cleanup and I can try to do that if there is interest. The runtime_provider_additions.cpp test shows the usage.

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