zoukankan      html  css  js  c++  java
  • Gin实现依赖注入

    前言

    依赖注入的好处和特点这里不讲述了,本篇文章主要介绍gin框架如何实现依赖注入,将项目解耦。

    项目结构

    
    ├── cmd          程序入口
    ├── common   通用模块代码
    ├── config       配置文件
    ├── controller API控制器
    ├── docs         数据库文件
    ├── models     数据表实体
    ├── page        页面数据返回实体
    ├── repository 数据访问层
    ├── router       路由
    ├── service     业务逻辑层
    ├── vue-admin Vue前端页面代码
    

    相信很多Java或者.NET的码友对这个项目结构还是比较熟悉的,现在我们就用这个项目结构在gin框架中实现依赖注入。这里主要介绍controller、service和repository。

    数据访问层repository

    1. 首先定义IStartRepo接口,里面包括Speak方法
    //IStartRepo 定义IStartRepo接口
    type IStartRepo interface {
    	Speak(message string) string
    }
    
    1. 实现该接口,这里就不访问数据库了,直接返回数据
    //StartRepo 注入数据库
    type StartRepo struct {
    	Source datasource.IDb `inject:""`
    }
    
    //Speak 实现Speak方法
    func (s *StartRepo) Speak(message string) string {
    	//使用注入的IDb访问数据库
    	//s.Source.DB().Where("name = ?", "jinzhu").First(&user)
    	return fmt.Sprintf("[Repository] speak: %s", message)
    }
    

    这样我们就简单完成了数据库访问层的接口封装

    业务逻辑层service

    1. 定义IStartService接口
    //IStartService 定义IStartService接口
    type IStartService interface {
    	Say(message string) string
    }
    
    1. 实现IStartService接口方法,注入IStartRepo数据访问层
    //StartService 注入IStartRepo
    type StartService struct {
    	Repo repository.IStartRepo `inject:""`
    }
    
    //Say 实现Say方法
    func (s *StartService) Say(message string) string {
    	return s.Repo.Speak(message)
    }
    

    控制器controller

    1. 注入IStartService业务逻辑层,并调用Say方法
    //Index 注入IStartService
    type Index struct {
    	Service service.IStartService `inject:""`
    }
    
    //GetName 调用IStartService的Say方法
    func (i *Index) GetName(ctx *gin.Context) {
    	var message = ctx.Param("msg")
    	ctx.JSON(200, i.Service.Say(message))
    }
    

    注入gin中

    到此三个层此代码已写好,然后我们使用"github.com/facebookgo/inject"Facebook的工具包将它们注入到gin中。

    func Configure(app *gin.Engine) {
    	//controller declare
    	var index controller.Index
    	//inject declare
    	db := datasource.Db{}
    	//Injection
    	var injector inject.Graph
    	err := injector.Provide(
    		&inject.Object{Value: &index},
    		&inject.Object{Value: &db},
    		&inject.Object{Value: &repository.StartRepo{}},
    		&inject.Object{Value: &service.StartService{}},
    	)
    	if err != nil {
    		log.Fatal("inject fatal: ", err)
    	}
    	if err := injector.Populate(); err != nil {
    		log.Fatal("inject fatal: ", err)
    	}
    
    	//database connect
    	err = db.Connect()
    	if err != nil {
    		log.Fatal("db fatal:", err)
    	}
    	v1 := app.Group("/")
    	{
    		v1.GET("/get/:msg", index.GetName)
    	}
    }
    

    有关更多的github.com/facebookgo/inject使用方法请参考文档

    本demo源码地址:https://github.com/Bingjian-Zhu/gin-inject

  • 相关阅读:
    stream流的统计demo
    ResourceBundle 读取文件demo
    spring boot 配置Filter过滤器的两种方式
    java工厂模式demo
    ThreadLocalDemo
    观察者模式Demo
    大数字的计算
    rabbitMQ消息丢失
    CF671E(线段树+单调栈)
    2020集训队作业板刷记录(三)
  • 原文地址:https://www.cnblogs.com/FireworksEasyCool/p/11805148.html
Copyright © 2011-2022 走看看