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

Type dependencies #20

Closed
amitkanfer opened this issue May 5, 2016 · 12 comments
Closed

Type dependencies #20

amitkanfer opened this issue May 5, 2016 · 12 comments
Assignees
Labels

Comments

@amitkanfer
Copy link

amitkanfer commented May 5, 2016

Hello there,
Thanks for the very cool project.

Question 1:
I've been using the version you've published in the past, using the INJECT macro in order to declare dependencies of a type.

So for example:

builder.registerType< Car>(CREATE(new Car(INJECT(IDriver))))->as< ICar>()->singleInstance();

in this example, i need to be familiar only with the IDriver interface.

I see that you changed this implementation and now the above should look something like this:

builder.registerType< Car>().with< IDriver, Driver >().as< ICar>().singleInstance();

right?
But now it seems like i need to be familiar with both IDriver and Driver

Am i missing something?

Question 2:
Does the new version compiles faster comparing to the previous release?

Thanks alot,
Amit

@ybainier
Copy link
Owner

ybainier commented May 9, 2016

Hi Amit,

The new version of Hypodermic takes you by the hand. Basically, when you wrote

builder.registerType< Driver >().as< IDriver >();

builder.registerType< Car >(CREATE(new Car(INJECT(IDriver))))
       .as< ICar >()
       .singleInstance();

now you can write

builder.registerType< Driver >().as< IDriver >();
builder.registerType< Car >().as< ICar >().singleInstance();

because you no longer need to describe the look of the constructor of Car.

More specifically, you only have to register types when you need to configure them. That is, their lifetime, or when they have to be known as an interface, or they have a very specific constructor not accepting instance types that Hypodermic can inject.

As a last example, if IDriver didn’t exist, well, if Car was accepting a Driver, you could only describe Car

builder.registerType< Car >().as< ICar >().singleInstance();

and if Car didn’t need to be known as ICar or be a single instance, you could just resolve Car without registering anything (doing so is harmless of course).

If you have any further questions, feel free to get back to me.

Thanks for the cheerings, it is always appreciated.

@amitkanfer
Copy link
Author

amitkanfer commented May 10, 2016

Thanks Yohan,

I tried following your guidelines, and i'm getting this error:

ContainerBuilder.h:27:81:   required by substitution of 'template<class T> typename
Hypodermic::RegistrationDescriptorBuilder::ForTypeConstruction<T>::Type&
Hypodermic::ContainerBuilder::registerType() [with T = FG::S2CMessageDispatcher]'

static assertion failed: Could not autowire T: you should consider registering T either by
providing an instance or an instance factory

on this line:

builder.registerType< S2CMessageDispatcher >().as< IS2CMessageDispatcher >().singleInstance();

This is the class:

class S2CMessageDispatcher : public IS2CMessageDispatcher

as you can see - it's implementing IS2CMessageDispatcher.

Can you help me understand what's the issue?

Thanks,
Amit

@ybainier
Copy link
Owner

ybainier commented May 10, 2016

Can you show me the ctor of your class? (Is it public?)

@amitkanfer
Copy link
Author

It looks something like this:

    S2CMessageDispatcher(std::shared_ptr<IA>a_handler,
                         std::shared_ptr<IB> b_handler,
                         std::shared_ptr<IC> c_handler,
                         std::shared_ptr<ID> d_manager,

.....
with ~40 dependencies like this, all interfaces.

@amitkanfer
Copy link
Author

amitkanfer commented May 10, 2016

I assume the order in which i register the types doesn't matter, right?

@ybainier
Copy link
Owner

ybainier commented May 10, 2016

It doesn't.

@ybainier
Copy link
Owner

ybainier commented May 10, 2016

Well, this is it. Have a look at Config.h

Hypodermic is configured to inject up to 20 dependencies by default. You might be able to raise it to whatever the number you want.

@amitkanfer
Copy link
Author

amitkanfer commented May 10, 2016

That solved it, thanks.
One more thing..

i had a registration like this:

builder.registerType< CommandlineParser >
(
    [=](IComponentContext&)
    {
        return new CommandlineParser(argc, argv);
    }
).as< ICommandlineParser >().singleInstance();

How should i implement it now?

@amitkanfer
Copy link
Author

amitkanfer commented May 10, 2016

Is it equivelent to this:

builder.registerInstanceFactory
(
    [=](Container&)
    {
        return std::make_shared< CommandlineParser >(argc, argv);
    }
).singleInstance();

If i try this - i'm getting this compilation error:

/home/amitkanfer/dev/unified_fireglass/vb/3rd_party/Hypodermic/
ProvidedInstanceFactoryRegistrationDescriptor.h:65:50: error: this context->m_instanceFactory =
m_instanceFactory;

thanks alot :)

@ybainier
Copy link
Owner

ybainier commented May 10, 2016

Yes, so I should investigate... What kind of unit test can I build to make this fails like your code?

@ybainier
Copy link
Owner

ybainier commented May 10, 2016

Thank you very much for pointing out this issue. You are all set. Thanks again

@amitkanfer
Copy link
Author

Thanks alot!

@ybainier ybainier self-assigned this May 10, 2016
@ybainier ybainier added the bug label May 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants