• 控制反转 依赖注入 main函数


    通过依赖注入、服务定位实现控制反转

    Go kit - Frequently asked questions https://gokit.io/faq/

    Dependency Injection — Why is func main always so big?

    Go kit encourages you to design your services as multiple interacting components, including several single-purpose middlewares. Experience has taught us that the most comprehensible, maintainable, and expressive method of defining and wiring up the component graph in a microservice is through an explicit and declarative composition in a large func main.

    Inversion of control is a common feature of other frameworks, implemented via Dependency Injection or Service Locator patterns. But in Go kit, you should wire up your entire component graph in your func main. This style reinforces two important virtues. By strictly keeping component lifecycles in main, you avoid leaning on global state as a shortcut, which is critical for testability. And if components are scoped to main, the only way to provide them as dependencies to other components is to pass them explicitly as parameters to constructors. This keeps dependencies explicit, which stops a lot of technical debt before it starts.

    As an example, let’s say we have the following components:

    • Logger
    • TodoService, implementing the Service interface
    • LoggingMiddleware, implementing the Service interface, requiring Logger and concrete TodoService
    • Endpoints, requiring a Service interface
    • HTTP (transport), requiring Endpoints

    Your func main should be wired up as follows:

    logger := log.NewLogger(...)
    
    var service todo.Service    // interface
    service = todo.NewService() // concrete struct
    service = todo.NewLoggingMiddleware(logger)(service)
    
    endpoints := todo.NewEndpoints(service)
    transport := todo.NewHTTPTransport(endpoints)
    

    At the cost of having a potentially large func main, composition is explicit and declarative. For more general Go design tips, see Go best practices, six years in.

  • 相关阅读:
    PyQT5速成教程-4 Qt Designer实战[上]
    使用Qt Designer进行布局
    PyCharm+QT Designer整合
    Qt Designer 的使用
    Python | 一行命令生成动态二维码
    Python 标准库、第三方库
    Python list列表的常用操作方法
    Python+Selenium 自动化测试获取测试报告内容并发送邮件
    ”Tomcat version 5.5 only supports J2EE 1.2, 1.3, and 1.4 Web modules“解决方案
    linux查找符合条件的文件并删除
  • 原文地址:https://www.cnblogs.com/rsapaper/p/13639948.html
Copyright © 2020-2023  润新知