Inject function is called multiple time on singletons w/ provider
tessig opened this issue · comments
Thorsten Essig commented
injector.Bind(new(MyStruct)).In(dingo.Singleton)
injector.Bind(new(Iface1)).ToProvider(func(s *MyStruct) Iface1 { return s })
injector.Bind(new(Iface2)).ToProvider(func(s *MyStruct) Iface2 { return s })
The Inject function of MyStruct
is called for each requested injection of Iface1
or Iface2
The expected behaviour is to call Inject only once for a singleton.
Bastian commented
It should work with
injector.Bind(new(MyStruct)).In(dingo.Singleton)
injector.Bind(new(Iface1)).To(new(MyStruct))
injector.Bind(new(Iface2)).To(new(MyStruct))
Thorsten Essig commented
In my case I need special logic in a provider, so the working solution is accordingly:
injector.Bind(new(MyStruct)).ToProvider(MyStructProvider).In(dingo.Singleton)
injector.Bind(new(Iface1)).To(new(MyStruct))
injector.Bind(new(Iface2)).To(new(MyStruct))
with MyStructProvider
beeing the function with this logic.
Thanks!