补充简易版本的ioc
This commit is contained in:
parent
7c82afd9e5
commit
e05a1cacd6
@ -6,12 +6,20 @@ import (
|
|||||||
"122.51.31.227/go-course/go18/book/v3/config"
|
"122.51.31.227/go-course/go18/book/v3/config"
|
||||||
"122.51.31.227/go-course/go18/book/v3/exception"
|
"122.51.31.227/go-course/go18/book/v3/exception"
|
||||||
"122.51.31.227/go-course/go18/book/v3/models"
|
"122.51.31.227/go-course/go18/book/v3/models"
|
||||||
|
"122.51.31.227/go-course/go18/skills/ioc"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Book = &BookController{}
|
func GetBookService() *BookController {
|
||||||
|
return ioc.Controller.Get("book_controller").(*BookController)
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
ioc.Controller.Registry("book_controller", &BookController{})
|
||||||
|
}
|
||||||
|
|
||||||
type BookController struct {
|
type BookController struct {
|
||||||
|
ioc.ObjectImpl
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGetBookRequest(bookNumber int) *GetBookRequest {
|
func NewGetBookRequest(bookNumber int) *GetBookRequest {
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestGetBook(t *testing.T) {
|
func TestGetBook(t *testing.T) {
|
||||||
book, err := controllers.Book.GetBook(context.Background(), controllers.NewGetBookRequest(3))
|
book, err := controllers.GetBookService().GetBook(context.Background(), controllers.NewGetBookRequest(3))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -20,7 +20,7 @@ func TestGetBook(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateBook(t *testing.T) {
|
func TestCreateBook(t *testing.T) {
|
||||||
book, err := controllers.Book.CreateBook(context.Background(), &models.BookSpec{
|
book, err := controllers.GetBookService().CreateBook(context.Background(), &models.BookSpec{
|
||||||
Title: "unit test for go controller obj",
|
Title: "unit test for go controller obj",
|
||||||
Author: "will",
|
Author: "will",
|
||||||
Price: 99.99,
|
Price: 99.99,
|
||||||
|
@ -19,7 +19,7 @@ type AddCommentRequest struct {
|
|||||||
func (c *CommentController) AddComment(ctx context.Context, in *AddCommentRequest) (*models.Comment, error) {
|
func (c *CommentController) AddComment(ctx context.Context, in *AddCommentRequest) (*models.Comment, error) {
|
||||||
// 业务处理的细节
|
// 业务处理的细节
|
||||||
// 多个业务模块 进行交互
|
// 多个业务模块 进行交互
|
||||||
book, err := Book.GetBook(ctx, NewGetBookRequest(in.BookNumber))
|
book, err := GetBookService().GetBook(ctx, NewGetBookRequest(in.BookNumber))
|
||||||
|
|
||||||
// if exception.IsApiException(err, exception.CODE_NOT_FOUND) {
|
// if exception.IsApiException(err, exception.CODE_NOT_FOUND) {
|
||||||
|
|
||||||
|
@ -114,7 +114,7 @@ func (h *BookApiHandler) createBook(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
book, err := controllers.Book.CreateBook(ctx.Request.Context(), bookSpecInstance)
|
book, err := controllers.GetBookService().CreateBook(ctx.Request.Context(), bookSpecInstance)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.Failed(ctx, err)
|
response.Failed(ctx, err)
|
||||||
return
|
return
|
||||||
@ -131,7 +131,7 @@ func (h *BookApiHandler) getBook(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
book, err := controllers.Book.GetBook(ctx, controllers.NewGetBookRequest(int(bnInt)))
|
book, err := controllers.GetBookService().GetBook(ctx, controllers.NewGetBookRequest(int(bnInt)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.Failed(ctx, err)
|
response.Failed(ctx, err)
|
||||||
return
|
return
|
||||||
|
5
skills/ioc/README.md
Normal file
5
skills/ioc/README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# ioc
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
# 没有Ioc
|
BIN
skills/ioc/image.png
Normal file
BIN
skills/ioc/image.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 133 KiB |
35
skills/ioc/interface.go
Normal file
35
skills/ioc/interface.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package ioc
|
||||||
|
|
||||||
|
var containers = []Contaienr{
|
||||||
|
Api,
|
||||||
|
Controller,
|
||||||
|
Config,
|
||||||
|
Default,
|
||||||
|
}
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
for _, c := range containers {
|
||||||
|
if err := c.Init(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ioc 容器功能定义
|
||||||
|
type Contaienr interface {
|
||||||
|
Registry(name string, obj Object)
|
||||||
|
Get(name string) Object
|
||||||
|
// 初始化所有已经注册的对象
|
||||||
|
Init() error
|
||||||
|
}
|
||||||
|
|
||||||
|
// 注册对象的约束
|
||||||
|
type Object interface {
|
||||||
|
Init() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type ObjectImpl struct{}
|
||||||
|
|
||||||
|
func (o *ObjectImpl) Init() error {
|
||||||
|
return nil
|
||||||
|
}
|
38
skills/ioc/map.go
Normal file
38
skills/ioc/map.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package ioc
|
||||||
|
|
||||||
|
var Api = NewMapContainer("api")
|
||||||
|
var Controller = NewMapContainer("controller")
|
||||||
|
var Config = NewMapContainer("config")
|
||||||
|
var Default = NewMapContainer("default")
|
||||||
|
|
||||||
|
func NewMapContainer(name string) Contaienr {
|
||||||
|
return &MapContainer{
|
||||||
|
name: name,
|
||||||
|
storage: map[string]Object{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ioc 容器
|
||||||
|
type MapContainer struct {
|
||||||
|
name string
|
||||||
|
storage map[string]Object
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MapContainer) Registry(name string, obj Object) {
|
||||||
|
m.storage[name] = obj
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MapContainer) Get(name string) Object {
|
||||||
|
return m.storage[name]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化所有已经注册的对象
|
||||||
|
func (m *MapContainer) Init() error {
|
||||||
|
for _, v := range m.storage {
|
||||||
|
if err := v.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user