infrastructure.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package infrastructure
  2. import (
  3. "git.sxidc.com/go-framework/baize/infrastructure/database"
  4. "git.sxidc.com/go-framework/baize/infrastructure/database/data_service"
  5. "git.sxidc.com/go-framework/baize/infrastructure/database/operations"
  6. )
  7. type Config struct {
  8. DatabaseConfig
  9. }
  10. type DatabaseConfig struct {
  11. Operations *operations.Config
  12. DataService *data_service.Config
  13. }
  14. type Infrastructure struct {
  15. dbOperations *operations.Operations
  16. dataService *data_service.DataService
  17. }
  18. func NewInfrastructure(config Config) *Infrastructure {
  19. i := new(Infrastructure)
  20. if config.DatabaseConfig.Operations != nil {
  21. op, err := operations.NewOperations(config.DatabaseConfig.Operations)
  22. if err != nil {
  23. panic("创建数据库操作失败" + err.Error())
  24. }
  25. i.dbOperations = op
  26. }
  27. if config.DatabaseConfig.DataService != nil {
  28. dataService, err := data_service.NewDataService(config.DatabaseConfig.DataService)
  29. if err != nil {
  30. panic("创建数据服务失败" + err.Error())
  31. }
  32. i.dataService = dataService
  33. }
  34. return i
  35. }
  36. func DestroyInfrastructure(i *Infrastructure) {
  37. if i == nil {
  38. return
  39. }
  40. if i.dataService != nil {
  41. err := data_service.DestroyDataService(i.dataService)
  42. if err != nil {
  43. panic("销毁数据服务失败" + err.Error())
  44. }
  45. }
  46. if i.dbOperations != nil {
  47. err := operations.DestroyOperation(i.dbOperations)
  48. if err != nil {
  49. panic("销毁数据库操作失败" + err.Error())
  50. }
  51. }
  52. return
  53. }
  54. const (
  55. DBExecutorOperations = "operations"
  56. DBExecutorDataService = "data_service"
  57. )
  58. func (i Infrastructure) ChooseDBExecutor(dataExecutorType string) database.Executor {
  59. switch dataExecutorType {
  60. case DBExecutorOperations:
  61. return i.DBOperations()
  62. case DBExecutorDataService:
  63. return i.DataService()
  64. default:
  65. return i.DBOperations()
  66. }
  67. }
  68. func (i Infrastructure) DBOperations() *operations.Operations {
  69. return i.dbOperations
  70. }
  71. func (i Infrastructure) DataService() *data_service.DataService {
  72. return i.dataService
  73. }