GithubHelp home page GithubHelp logo

Type dependencies about hypodermic HOT 12 CLOSED

ybainier avatar ybainier commented on June 25, 2024
Type dependencies

from hypodermic.

Comments (12)

ybainier avatar ybainier commented on June 25, 2024

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.

from hypodermic.

amitkanfer avatar amitkanfer commented on June 25, 2024

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

from hypodermic.

ybainier avatar ybainier commented on June 25, 2024

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

from hypodermic.

amitkanfer avatar amitkanfer commented on June 25, 2024

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.

from hypodermic.

amitkanfer avatar amitkanfer commented on June 25, 2024

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

from hypodermic.

ybainier avatar ybainier commented on June 25, 2024

It doesn't.

from hypodermic.

ybainier avatar ybainier commented on June 25, 2024

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.

from hypodermic.

amitkanfer avatar amitkanfer commented on June 25, 2024

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?

from hypodermic.

amitkanfer avatar amitkanfer commented on June 25, 2024

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 :)

from hypodermic.

ybainier avatar ybainier commented on June 25, 2024

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

from hypodermic.

ybainier avatar ybainier commented on June 25, 2024

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

from hypodermic.

amitkanfer avatar amitkanfer commented on June 25, 2024

Thanks alot!

from hypodermic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.