go - Golang 中的工厂函数

标签 go dependency-injection inversion-of-control factory

我有一个类似于下面所示的服务对象,它通过 HTTP 公开:

type ComputeService struct {

}

func (svc ComputeService) Compute(userType string, data Data) (Result, error) {
   // rate limit based on userType (both check and increment counter)
   // if not rate limited, compute and return result
}

现在,如您所见,需要根据 userType 进行速率限制。 。速率限制器本身的实现根据 userType 进行更改。我正在考虑使用 userType 解决速率限制器的两种方法.

// First approach: for each request new ComputeService object is
// created after resolving RateLimiter using userType in the handler
// itself
type ComputeService struct {
    RateLimiter RateLimiter
}

// Second approach: ComputeService object is initialized during startup
// and only RateLimiter is resolved with every Compute() call by calling
// the provider function
type ComputeService struct {
    RateLimiterProvider func(userType string) RateLimiter
}

两者都是可测试的。哪一个更可取?我倾向于第二种方法,因为使用它意味着处理程序将纯粹是读取请求、委托(delegate)给服务、写出响应,而在第一种方法中,处理程序将包含解决速率限制器实现的额外步骤。

最佳答案

如果您使用的是 Dargo 这样的 DI 系统您可以使用其 Provider 注入(inject)在运行时动态选择实现。

在这种情况下,您的服务将如下所示:

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

type RateLimiter interface {
}

type UserType1RateLimiter struct {
}

type UserType2RateLimiter struct {
}

type ComputeService struct {
    RateLimiterProvider ioc.Provider `inject:"RateLimiter"`
}

func (svc ComputeService) Compute(userType string, data Data) (Result, error) {
    // rate limit based on userType (both check and increment counter)
    // if not rate limited, compute and return result
    raw, err := svc.RateLimiterProvider.QualifiedBy(userType).Get()
    if err != nil {
        return nil, err
    }

    limiter := raw.(RateLimiter)
    //...
}  

这是将它们绑定(bind)到 ServiceLocator 中的方法:

func initializer() {
    serviceLocator, _ = ioc.CreateAndBind("ExampleLocator", func(binder ioc.Binder) error {
        binder.Bind("RateLimiter", UserType1RateLimiter{}).QualifiedBy("UserType1")
        binder.Bind("RateLimiter", UserType2RateLimiter{}).QualifiedBy("UserType2")
        binder.Bind("ComputeService", ComputeService{})
        return nil
    })
}

这仅适用于您使用 Dargo 之类的东西时,但它对您的情况仍然可能有用。

如果你不使用 Dargo,在我看来这是一个意见问题,尽管我个人会选择第二种方法

关于go - Golang 中的工厂函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52297859/

相关文章:

go - 无法在反射中设置结构的值

c# - 使用 Windsor CaSTLe 从目录中检索 DLL

dependency-injection - 什么是理解 IoC 和 DI 的好类比?

email - 如何从 Google Cloud 通过 G-suite 发送电子邮件

http - 对每个请求使用相同的 header

go - 如何使用 Pipes 分配额外的 ExraFile 以拥有多个输出流?

android - Dagger2 注入(inject)大量 Activity/fragment/服务(可能获得大量 NPE)

java - 依赖注入(inject)在 Spring 中是如何工作的?

c# - ASP.NET Core 通用主机 (HostBuilder) 尝试激活时无法解析类型的服务...

generics - CaSTLe Windsor Ioc 解析 web.config 中的通用类