| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package infrastructure
- import (
- "git.sxidc.com/go-framework/baize/infrastructure/database"
- "git.sxidc.com/go-framework/baize/infrastructure/database/data_service"
- "git.sxidc.com/go-framework/baize/infrastructure/database/operations"
- )
- type Config struct {
- DatabaseConfig
- }
- type DatabaseConfig struct {
- Operations *operations.Config
- DataService *data_service.Config
- }
- type Infrastructure struct {
- dbOperations *operations.Operations
- dataService *data_service.DataService
- }
- func NewInfrastructure(config Config) *Infrastructure {
- i := new(Infrastructure)
- if config.DatabaseConfig.Operations != nil {
- op, err := operations.NewOperations(config.DatabaseConfig.Operations)
- if err != nil {
- panic("创建数据库操作失败" + err.Error())
- }
- i.dbOperations = op
- }
- if config.DatabaseConfig.DataService != nil {
- dataService, err := data_service.NewDataService(config.DatabaseConfig.DataService)
- if err != nil {
- panic("创建数据服务失败" + err.Error())
- }
- i.dataService = dataService
- }
- return i
- }
- func DestroyInfrastructure(i *Infrastructure) {
- if i == nil {
- return
- }
- if i.dataService != nil {
- err := data_service.DestroyDataService(i.dataService)
- if err != nil {
- panic("销毁数据服务失败" + err.Error())
- }
- }
- if i.dbOperations != nil {
- err := operations.DestroyOperation(i.dbOperations)
- if err != nil {
- panic("销毁数据库操作失败" + err.Error())
- }
- }
- return
- }
- const (
- DBExecutorOperations = "operations"
- DBExecutorDataService = "data_service"
- )
- func (i Infrastructure) ChooseDBExecutor(dataExecutorType string) database.Executor {
- switch dataExecutorType {
- case DBExecutorOperations:
- return i.DBOperations()
- case DBExecutorDataService:
- return i.DataService()
- default:
- return i.DBOperations()
- }
- }
- func (i Infrastructure) DBOperations() *operations.Operations {
- return i.dbOperations
- }
- func (i Infrastructure) DataService() *data_service.DataService {
- return i.dataService
- }
|