infrastructure.go 1.5 KB

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