init
This commit is contained in:
17
internal/wire/injector.go
Normal file
17
internal/wire/injector.go
Normal file
@@ -0,0 +1,17 @@
|
||||
//go:build wireinject
|
||||
// +build wireinject
|
||||
|
||||
package wire
|
||||
|
||||
import (
|
||||
"github.com/dungnt11/todoms_golang/internal/controllers"
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
// InitializeControllers khởi tạo tất cả các controllers
|
||||
func InitializeControllers() (*controllers.Controllers, error) {
|
||||
wire.Build(
|
||||
AppSet,
|
||||
)
|
||||
return nil, nil
|
||||
}
|
49
internal/wire/wire.go
Normal file
49
internal/wire/wire.go
Normal file
@@ -0,0 +1,49 @@
|
||||
// Package wire chứa các định nghĩa cho dependency injection sử dụng Google Wire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"github.com/dungnt11/todoms_golang/global"
|
||||
"github.com/dungnt11/todoms_golang/internal/controllers"
|
||||
"github.com/dungnt11/todoms_golang/internal/repositories"
|
||||
"github.com/dungnt11/todoms_golang/internal/services"
|
||||
"github.com/google/wire"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RepositorySet là tập hợp các provider cho repositories
|
||||
var RepositorySet = wire.NewSet(
|
||||
repositories.NewUserRepository,
|
||||
wire.Bind(new(repositories.IUserRepository), new(*repositories.UserRepository)),
|
||||
repositories.NewProductRepository,
|
||||
wire.Bind(new(repositories.IProductRepository), new(*repositories.ProductRepository)),
|
||||
)
|
||||
|
||||
// ServiceSet là tập hợp các provider cho services
|
||||
var ServiceSet = wire.NewSet(
|
||||
services.NewUserService,
|
||||
wire.Bind(new(services.IUserService), new(*services.UserService)),
|
||||
services.NewProductService,
|
||||
wire.Bind(new(services.IProductService), new(*services.ProductService)),
|
||||
)
|
||||
|
||||
// ControllerSet là tập hợp các provider cho controllers
|
||||
var ControllerSet = wire.NewSet(
|
||||
controllers.NewUserController,
|
||||
wire.Bind(new(controllers.IUserController), new(*controllers.UserController)),
|
||||
controllers.NewProductController,
|
||||
wire.Bind(new(controllers.IProductController), new(*controllers.ProductController)),
|
||||
controllers.NewControllers,
|
||||
)
|
||||
|
||||
// ProvideDB cung cấp instance của *gorm.DB
|
||||
func ProvideDB() *gorm.DB {
|
||||
return global.Mdb
|
||||
}
|
||||
|
||||
// AppSet là tập hợp tất cả các provider
|
||||
var AppSet = wire.NewSet(
|
||||
RepositorySet,
|
||||
ServiceSet,
|
||||
ControllerSet,
|
||||
ProvideDB,
|
||||
)
|
28
internal/wire/wire_gen.go
Normal file
28
internal/wire/wire_gen.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Code generated by Wire. DO NOT EDIT.
|
||||
|
||||
//go:generate go run -mod=mod github.com/google/wire/cmd/wire
|
||||
//go:build !wireinject
|
||||
// +build !wireinject
|
||||
|
||||
package wire
|
||||
|
||||
import (
|
||||
"github.com/dungnt11/todoms_golang/internal/controllers"
|
||||
"github.com/dungnt11/todoms_golang/internal/repositories"
|
||||
"github.com/dungnt11/todoms_golang/internal/services"
|
||||
)
|
||||
|
||||
// Injectors from injector.go:
|
||||
|
||||
// InitializeControllers khởi tạo tất cả các controllers
|
||||
func InitializeControllers() (*controllers.Controllers, error) {
|
||||
db := ProvideDB()
|
||||
userRepository := repositories.NewUserRepository(db)
|
||||
userService := services.NewUserService(userRepository)
|
||||
userController := controllers.NewUserController(userService)
|
||||
productRepository := repositories.NewProductRepository(db)
|
||||
productService := services.NewProductService(productRepository)
|
||||
productController := controllers.NewProductController(productService)
|
||||
controllersControllers := controllers.NewControllers(userController, productController)
|
||||
return controllersControllers, nil
|
||||
}
|
Reference in New Issue
Block a user