Open d-enk opened 2 months ago
1- I think you should just call do.Invoke[I]
instead of do.InvokeAs[i]
, since your not performing interface matching here. I will improve the error message.
2- I'm not sure to understand the issue here. Your example output seems valid to me.
While i review comments on #45, I'm linking your issue to https://github.com/samber/do/pull/45#issuecomment-1993706391
To describe the feedback from @mbark:
*MyServiceImplem
IMyService
*MyServiceMock
via an override for test purposes.Problem: do.InvokeAs
will pick a random implementation of IMyService
Possible solutions:
do.OverrideNamedValue(i, do.NameOf[IMyService](), NewMyServiceMock)
-> not developer friendlydo.AsNamed()
-> might trigger error on override@d-enk suggested reporting an error when multiple services match the interface.
Also, I would point out that since services injected in IoC containers are opaque, invoking an interface instead of a struct might lead to loading the wrong service if method signatures overlap. In that situation, an explicit alias is safer: do.AsNamed()
Another option, maybe you just need a separate method that returns a list of all match?
And in InvokeAs
allow only one
looks like related https://github.com/samber/do/issues/33
to fix 1 problem https://github.com/samber/do/issues/81
interface
(can't castnil
)import "github.com/samber/do/v2"
func main() { type I interface{}
}
// panic: DI: could not find service
*main.I
, available services:*main.I
I create a function that would temporarily delete InvokedAs result by OverrideNamedValue and tried to check the next not exist.
But I came across both 1 problem and https://github.com/samber/go-type-to-string/issues/2