使用facebookgo创建多个实例

What is the correct way to use the facebookgo graph to instantiate multiple instances of a service/ dependency? The example in the documentation shows tight coupling between the instantiation with the variable. However, if my dependency is "stateful" such that I can't use a singleton across multiple services, how do I get facebookgo to give me new instances everytime?

func main() {
  var g inject.Graph
  var s service.Impl
  if err := g.Provide(&inject.Object{Value: library.NewDependency()},
    &inject.Object{Value: &s}); err != nil {
    fmt.Println("err in g.Provide: ", err)
  }
  if err := g.Populate(); err != nil {
    fmt.Println("err in g.Populate: ", err)
  }
  s.Feature()

  var s2 service.Impl
}

Assume that service and library are some packages with some implementations. "inject" is facebookgo/inject and service.Impl depends on library.Dependency. Now, how do I resolve s2?

If you are using Dargo for injection services have scopes. For example Singleton and PerLookup. In your case you can bind a service in the PerLookup scope and then it'll get created every time it is looked up or injected.

By default things are bound into the Singleton scope, so for something to be in the PerLookup scope you have to explicitly say so in the bind statement:

import "github.com/jwells131313/dargo/ioc"    

ioc.CreateAndBind(Example2LocatorName, func(binder ioc.Binder) error {
    // binds the echo service into the locator in Singleton scope
    binder.BindWithCreator(EchoServiceName, newEchoService)

    // binds the logger service into the locator in PerLookup scope
    binder.BindWithCreator(LoggerServiceName, newLogger).InScope(ioc.PerLookup)

    return nil
})