diff --git a/lib/src/auto_injector_base.dart b/lib/src/auto_injector_base.dart
index 29a5880..5e30694 100644
--- a/lib/src/auto_injector_base.dart
+++ b/lib/src/auto_injector_base.dart
@@ -20,7 +20,7 @@ abstract class Injector {
/// to replace an instance with a mock in tests.
///
/// When [key] is provided it will search the instance that have the same key
- T get({ParamTransform? transform, String? key});
+ T get({ParamTransform? transform, String? key, String? className});
/// Request an instance by [Type]
///
@@ -28,8 +28,8 @@ abstract class Injector {
/// to replace an instance with a mock in tests.
///
/// When [key] is provided it will search the instance that have the same key
- T call({ParamTransform? transform, String? key}) {
- return get(transform: transform, key: key);
+ T call({ParamTransform? transform, String? key, String? className}) {
+ return get(transform: transform, key: key, className: className);
}
/// Register a factory instance.
@@ -146,7 +146,7 @@ abstract class AutoInjector extends Injector {
/// to replace an instance with a mock in tests.
///
/// When [key] is provided it will search the instance that have the same key
- T? tryGet({ParamTransform? transform, String? key});
+ T? tryGet({ParamTransform? transform, String? key, String? className});
/// Inherit all instances and transforms from other AutoInjector object.
/// ```dart
@@ -160,25 +160,25 @@ abstract class AutoInjector extends Injector {
/// Checks if the instance record exists.
///
/// When [key] is provided it will search the instance that have the same key
- bool isAdded({String? key});
+ bool isAdded({String? key, String? className});
/// checks if the instance registration is as singleton.
///
/// When [key] is provided it will search the instance that have the same key
- bool isInstantiateSingleton({String? key});
+ bool isInstantiateSingleton({String? key, String? className});
/// Removes the singleton instance.
/// This does not remove it from the registry tree.
///
/// When [key] is provided it will search the instance that have the same key
- T? disposeSingleton({String? key});
+ T? disposeSingleton({String? key, String? className});
/// Replaces an instance record with a concrete instance.
/// This function should only be used for unit testing.
/// Any other use is discouraged.
///
/// When [key] is provided it will search the instance that have the same key
- void replaceInstance(T instance, {String? key});
+ void replaceInstance(T instance, {String? key, String? className});
/// Informs the container that the additions
/// are finished and the injector is ready to be used.
@@ -233,11 +233,11 @@ class AutoInjectorImpl extends AutoInjector {
}
@override
- T get({ParamTransform? transform, String? key}) {
+ T get({ParamTransform? transform, String? key, String? className}) {
_checkAutoInjectorIsCommitted();
try {
- final className = T.toString();
+ className ??= T.toString();
T? instance;
if (key == null) {
@@ -270,9 +270,9 @@ class AutoInjectorImpl extends AutoInjector {
}
@override
- T? tryGet({ParamTransform? transform, String? key}) {
+ T? tryGet({ParamTransform? transform, String? key, String? className}) {
try {
- return get(transform: transform, key: key);
+ return get(transform: transform, key: key, className: className);
} catch (e) {
return null;
}
@@ -335,27 +335,29 @@ class AutoInjectorImpl extends AutoInjector {
}
@override
- bool isAdded({String? key}) => (key == null) //
- ? _hasBindByClassName(T.toString())
+ bool isAdded({String? key, String? className}) => (key == null) //
+ ? _hasBindByClassName(className ?? T.toString())
: _hasBindByKey(key);
@override
- bool isInstantiateSingleton({String? key}) {
- final className = T.toString();
+ bool isInstantiateSingleton({String? key, String? className}) {
+ className ??= T.toString();
final data = (key == null) ? layersGraph.getBindByClassName(this, className: className) : layersGraph.getBindByKey(this, bindKey: key);
final bind = data?.value;
return bind?.hasInstance ?? false;
}
@override
- T? disposeSingleton({String? key}) {
- final response = (key == null) ? _disposeSingletonByClasseName(T.toString()) : _disposeSingletonByKey(key);
+ T? disposeSingleton({String? key, String? className}) {
+ final response = (key == null)
+ ? _disposeSingletonByClasseName(className ?? T.toString())
+ : _disposeSingletonByKey(key);
return response as T?;
}
@override
- void replaceInstance(T instance, {String? key}) {
- final className = T.toString();
+ void replaceInstance(T instance, {String? key, String? className}) {
+ className ??= T.toString();
final data = (key == null) ? layersGraph.getBindByClassName(this, className: className) : layersGraph.getBindByKey(this, bindKey: key);
if (data == null) {
diff --git a/test/src/auto_injector_with_type_as_string_test.dart b/test/src/auto_injector_with_type_as_string_test.dart
new file mode 100644
index 0000000..0105374
--- /dev/null
+++ b/test/src/auto_injector_with_type_as_string_test.dart
@@ -0,0 +1,106 @@
+import 'package:auto_injector/auto_injector.dart';
+import 'package:test/test.dart';
+
+void main() {
+ late AutoInjector injector;
+
+ setUp(() {
+ injector = AutoInjector();
+ });
+
+ tearDown(() => injector.disposeRecursive());
+
+ test('AutoInjector: add', () async {
+ expect(injector.isAdded(className: 'TestDatasource'), false);
+ injector.addSingleton(TestDatasource.new);
+ injector.commit();
+
+ expect(injector.isAdded(className: 'TestDatasource'), true);
+ });
+
+ test('AutoInjector: addInstance', () async {
+ expect(injector.isAdded(className: 'String'), false);
+
+ injector.addInstance('Test');
+ injector.commit();
+
+ expect(injector.isAdded(className: 'String'), true);
+ expect(injector.get(className: 'String'), 'Test');
+ });
+
+ test('AutoInjector: addSingleton', () async {
+ expect(injector.isAdded(className: 'TestDatasource'), false);
+ expect(
+ injector.isInstantiateSingleton(className: 'TestDatasource'),
+ false,
+ );
+
+ injector.addSingleton(TestDatasource.new);
+ injector.commit();
+
+ expect(injector.isAdded(className: 'TestDatasource'), true);
+ expect(
+ injector.isInstantiateSingleton(className: 'TestDatasource'),
+ true,
+ );
+
+ final value1 = injector();
+ final value2 = injector(className: 'TestDatasource');
+
+ expect(value1, value2);
+ });
+
+ test('AutoInjector: addLazySingleton', () async {
+ expect(injector.isAdded(className: 'TestDatasource'), false);
+ expect(
+ injector.isInstantiateSingleton(className: 'TestDatasource'),
+ false,
+ );
+
+ injector.addLazySingleton(TestDatasource.new);
+ injector.commit();
+
+ expect(injector.isAdded(className: 'TestDatasource'), true);
+ expect(
+ injector.isInstantiateSingleton(className: 'TestDatasource'),
+ false,
+ );
+ final value1 = injector();
+
+ expect(
+ injector.isInstantiateSingleton(className: 'TestDatasource'),
+ true,
+ );
+
+ final value2 = injector(className: 'TestDatasource');
+
+ expect(value1, value2);
+ });
+
+ test('AutoInjector: disposeSingleton', () async {
+ expect(injector.isAdded(className: 'TestDatasource'), false);
+ expect(
+ injector.isInstantiateSingleton(className: 'TestDatasource'),
+ false,
+ );
+
+ injector.addSingleton(TestDatasource.new);
+ injector.commit();
+
+ expect(injector.isAdded(className: 'TestDatasource'), true);
+ expect(
+ injector.isInstantiateSingleton(className: 'TestDatasource'),
+ true,
+ );
+
+ final instance = injector.disposeSingleton(className: 'TestDatasource');
+ expect(instance, isA());
+ expect(injector.isAdded(className: 'TestDatasource'), true);
+ expect(
+ injector.isInstantiateSingleton(className: 'TestDatasource'),
+ false,
+ );
+ });
+}
+
+class TestDatasource {}