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

Question about binding #113

Closed
banka50 opened this issue Sep 16, 2015 · 2 comments
Closed

Question about binding #113

banka50 opened this issue Sep 16, 2015 · 2 comments

Comments

@banka50
Copy link

banka50 commented Sep 16, 2015

Hi, Krzysztof!
Can I do something like this:

struct CommandA {};
struct HandlerA {};

struct CommandB {};
struct HandlerB {};

auto injector = di::make_injector(
di::bind<CommandA, HandlerA>
, di::bind<CommandB, HandlerB>
);

auto handler = injector.create();

P.S. You wrote excellent library ;)

@krzysztof-jusiak
Copy link
Collaborator

Hi Alexey,

I'm not sure if I do understand the question correctly, so if you could elaborate a bit more I will definitely try to answer it more accurately.

Anyway, in your example Command{A, B} and Handler{A, B} are not related at all and, therefore, DI won't be able to create appropriate Commands, but what you can write is following:

struct CommandA {};
struct HandlerA : CommandA {
     // HandlerA(some other depenencies...) // additionally Di will take care of creating of all them
}; // new

struct CommandB {};
struct HandlerB : CommandB {}; // new

auto injector = di::make_injector(
      di::bind<CommandA>.to<HandlerA> // notice new binding interface with 'to'
    , di::bind<CommandB>.to<HandlerB>
);

auto handlerA = injector.create<std::unique_ptr<CommandA>>(); // will give you HandlerA
auto handlerB = injector.create<std::unique_ptr<CommandB>>(); // will give you HandlerB

Hope that helps a bit.

P.S. Cheers :)

@banka50
Copy link
Author

banka50 commented Sep 21, 2015

Ok. Thanks :)

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

No branches or pull requests

2 participants