inject.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import 'dart:async';
  2. import 'package:get_it/get_it.dart';
  3. import 'package:lszlgl/main.dart';
  4. /// 依赖注入管理
  5. ///
  6. /// ```
  7. /// 简单使用
  8. /// 添加注入:
  9. /// var user = Inject.put<UserBean>(UserBean(), dispose: (v) => v.dispose());
  10. /// 添加注入(懒加载):
  11. /// Inject.putLazy(UserBean(), dispose: (v) => v.dispose());
  12. ///
  13. /// 获取实例:
  14. /// var user = Inject.get<UserBean>();
  15. ///
  16. /// 移除注入:
  17. /// Inject.remove<UserBean>();
  18. /// ```
  19. class Inject {
  20. Inject._();
  21. /// 添加依赖注入
  22. /// 注入后得到实例化对象, 也可以通过[get]方法得到对象.
  23. /// * [instance] 实例化对象
  24. /// * [name] 注入多个同类型对象时,可以使用name来区分
  25. /// * [dispose] 移除时回调方法
  26. /// * [allowReassignment] 是否可以重复注册, 默认不可重复注册, 返回之前已有实例
  27. ///
  28. static T put<T extends Object>(
  29. T instance, {
  30. String? name,
  31. FutureOr Function(T)? dispose,
  32. bool allowReassignment = false,
  33. }) {
  34. GetIt.I.allowReassignment = true;
  35. // 不可重复注册, 已注册,则返回之前实例
  36. if (!allowReassignment && GetIt.I.isRegistered<T>(instanceName: name)) {
  37. logger.d('Inject put:$T 已被注册');
  38. return get<T>()!;
  39. }
  40. logger.d('Inject put:$T');
  41. GetIt.I.registerSingleton<T>(
  42. instance,
  43. instanceName: name,
  44. dispose: dispose,
  45. );
  46. return instance;
  47. }
  48. /// 添加依赖注入 懒加载
  49. /// 注入后不会立即实例化对象, 当调用[get]时才会实例化.
  50. /// * [factoryFunc] 实例化对象回调方法
  51. /// * [name] 注入多个同类型对象时,可以使用name来区分
  52. /// * [dispose] 移除时回调方法
  53. static void putLazy<T extends Object>(
  54. T Function() factoryFunc, {
  55. String? name,
  56. FutureOr Function(T)? dispose,
  57. bool allowReassignment = false,
  58. }) {
  59. GetIt.I.allowReassignment = true;
  60. if (!allowReassignment && GetIt.I.isRegistered<T>(instanceName: name)) {
  61. logger.d('Inject putLazy:$T 已被注册');
  62. return;
  63. }
  64. logger.d('Inject putLazy:$T');
  65. GetIt.I.registerLazySingleton<T>(
  66. factoryFunc,
  67. instanceName: name,
  68. dispose: dispose,
  69. );
  70. }
  71. /// 移除依赖注入
  72. /// * [instance] 通过实例移除
  73. /// * [name] 通过名称移除
  74. /// * [dispose] 移除时回调方法
  75. static FutureOr remove<T extends Object>({
  76. Object? instance,
  77. String? name,
  78. FutureOr Function(T)? dispose,
  79. }) {
  80. logger.d('Inject remove:$T');
  81. return GetIt.I.unregister(
  82. instance: instance,
  83. instanceName: name,
  84. disposingFunction: dispose,
  85. );
  86. }
  87. /// 获取唯一实例
  88. /// Model model = GdInject.get<Model>();
  89. static T? get<T extends Object>({String? name}) {
  90. logger.d('Inject get:$T');
  91. return GetIt.I.get<T>(instanceName: name);
  92. }
  93. /// 获取唯一实例,如果未注入依赖,将自动注入依赖
  94. static T getAndPut<T extends Object>(
  95. T instance, {
  96. String? name,
  97. FutureOr Function(T)? dispose,
  98. }) {
  99. // 已注册
  100. if (GetIt.I.isRegistered<T>(instanceName: name)) {
  101. return get<T>(name: name)!;
  102. }
  103. return put<T>(instance, name: name, dispose: dispose);
  104. }
  105. }