作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
facebookgo 的正确使用方法是什么?图来实例化一个服务/依赖的多个实例?文档中的示例显示了实例化与 variable 之间的紧密耦合。 .但是,如果我的依赖关系是“有状态的”,以至于我不能在多个服务中使用单例,我如何让 facebookgo 每次都给我新的实例?
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
}
假设服务和库是一些具有一些实现的包。 "inject"是 facebookgo/inject 和 service.Impl 依赖于 library.Dependency。现在,我该如何解析 s2?
最佳答案
如果您使用 Dargo对于注入(inject)服务有作用域。例如 Singleton 和 PerLookup。在您的情况下,您可以在 PerLookup 范围内绑定(bind)一个服务,然后每次查找或注入(inject)时都会创建它。
默认情况下,事物被绑定(bind)到 Singleton 范围内,因此对于 PerLookup 范围内的事物,您必须在绑定(bind)语句中明确说明:
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
})
关于go - 使用 facebookgo 创建多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48313616/
facebookgo 的正确使用方法是什么?图来实例化一个服务/依赖的多个实例?文档中的示例显示了实例化与 variable 之间的紧密耦合。 .但是,如果我的依赖关系是“有状态的”,以至于我不能在多
我是一名优秀的程序员,十分优秀!