Skip to content

Retrieve Named/Provided Dependency in DI Graph #919

Answered by abhinav
kevinrobayna asked this question in Q&A
Discussion options

You must be logged in to vote

Hello! You can use the fx.Populate API to request objects from the container.

var x MyFooer
app := fxtest.New(t,
  opts(),
  fx.Populate(&x),
)
defer app.RequireStart().RequireStop()

result := x.Foo()

Note that the argument must be a pointer to the type of value you're looking up.
So if the value inside the container is a *Foo, the pointer must be a **Foo.

var foo *Foo
fxtest.New(t,
  fx.Provide(NewFoo()), // returns *Foo
  fx.Populate(&foo), // **Foo
)

You can provide any number of pointers to fill to fx.Populate.

Pointers will be filled when fx.New/fxtest.New returns.
However, we recommend calling app.Start/app.RequireStart before using them,
because the object might have a dependency …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kevinrobayna
Comment options

Answer selected by sywhang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants