global_services_injection.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import 'package:domain/constants/api_constants.dart';
  2. import 'package:domain/repository/auth_repository.dart';
  3. import 'package:domain/repository/job_repository.dart';
  4. import 'package:domain/repository/job_sg_repository.dart';
  5. import 'package:domain/repository/labour_repository.dart';
  6. import 'package:domain/repository/labour_sg_repository.dart';
  7. import 'package:domain/repository/other_repository.dart';
  8. import 'package:plugin_basic/basic_export.dart';
  9. import 'package:plugin_basic/dio_interceptors/interceptor_auth_dio.dart';
  10. import 'package:plugin_basic/dio_interceptors/interceptor_status_code_dio.dart';
  11. import 'package:plugin_basic/service/http_provider_injection.dart';
  12. import 'package:plugin_basic/service/user_service.dart';
  13. import 'package:plugin_platform/http/http_provider.dart';
  14. import 'package:shared/utils/log_utils.dart';
  15. /*
  16. 全局服务 Service 的初始化入口
  17. 1. 默认注入网络请求Provider与Domain的Repository
  18. 2. 留下高阶函数的入口,方便加入其他的全局服务
  19. 全局单例服务的注入,在 main.dart 入口就可直接调用,注意手动调用
  20. */
  21. class GlobalServicesInjection {
  22. /// init
  23. static void init({void Function()? additionalDependencies}) {
  24. Log.d('----ConfigServicesInjection------start-----');
  25. //全局单例的 HttpProvider 用于发起网络请求 (根据不同的选择国家注入不同的域名)
  26. HttpProviderInjection.putHttpProviderByCountry();
  27. //全局单例的用户数据仓库
  28. Get.put(AuthRepository(httpProvider: Get.find()));
  29. //其他的数据仓库注入
  30. Get.lazyPut(() => JobRepository(httpProvider: Get.find()));
  31. Get.lazyPut(() => JobSGRepository(httpProvider: Get.find()));
  32. Get.lazyPut(() => LabourRepository(httpProvider: Get.find()));
  33. Get.lazyPut(() => LabourSGRepository(httpProvider: Get.find()));
  34. Get.lazyPut(() => OtherRepository(httpProvider: Get.find()));
  35. // 用户信息服务(用户信息相关业务类)
  36. Get.put(UserService(Get.find()));
  37. // 调用额外的依赖注入逻辑(如果提供了)
  38. if (additionalDependencies != null) {
  39. additionalDependencies();
  40. }
  41. Log.d('----ConfigServicesInjection------end-----');
  42. }
  43. }