GithubHelp home page GithubHelp logo

Using .with<T>(instance) about hypodermic HOT 2 CLOSED

ybainier avatar ybainier commented on June 25, 2024
Using .with(instance)

from hypodermic.

Comments (2)

ybainier avatar ybainier commented on June 25, 2024

Hi, your example do not compile, you are missing the template parameters.

Assuming you are trying to resolve Baz which should be injected a IFoo, and that you want to resolve Baz as a IBaz:

ContainerBuilder builder;

auto foo = std::make_shared< Foo >();

builder.registerType< Baz >()
           .with< IFoo >(foo)
           .as< IBaz >();

auto container = builder.build();

auto baz = container->resolve< IBaz >();

But you could write it that way too:

ContainerBuilder builder;

auto foo = std::make_shared< Foo >();

builder.registerInstance(foo).as< IFoo >();
builder.registerType< Baz >().as< IBaz >();

auto container = builder.build();

auto baz = container->resolve< IBaz >();

Is this solving your problem?

from hypodermic.

jlhorner1974 avatar jlhorner1974 commented on June 25, 2024

Ah, I see now -- I did have the template types in my post, but I didn't mark the text as code, so they got stripped when I submitted the post.

I see my problem now, and I feel stupid. In my call to resolve(), I passed in Baz as the type instead of IBaz, so of course, it wasn't working.

I fixed my mistake and all works as it should. Thank you for your help.

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.