Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get by className #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions lib/src/auto_injector_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ abstract class Injector {
/// to replace an instance with a mock in tests.
/// <br>
/// When [key] is provided it will search the instance that have the same key
T get<T>({ParamTransform? transform, String? key});
T get<T>({ParamTransform? transform, String? key, String? className});

/// Request an instance by [Type]
/// <br>
/// [transform]: Transform a param. This can be used for example
/// to replace an instance with a mock in tests.
/// <br>
/// When [key] is provided it will search the instance that have the same key
T call<T>({ParamTransform? transform, String? key}) {
return get<T>(transform: transform, key: key);
T call<T>({ParamTransform? transform, String? key, String? className}) {
return get<T>(transform: transform, key: key, className: className);
}

/// Register a factory instance.
Expand Down Expand Up @@ -146,7 +146,7 @@ abstract class AutoInjector extends Injector {
/// to replace an instance with a mock in tests.
/// <br>
/// When [key] is provided it will search the instance that have the same key
T? tryGet<T>({ParamTransform? transform, String? key});
T? tryGet<T>({ParamTransform? transform, String? key, String? className});

/// Inherit all instances and transforms from other AutoInjector object.
/// ```dart
Expand All @@ -160,25 +160,25 @@ abstract class AutoInjector extends Injector {
/// Checks if the instance record exists.
/// <br>
/// When [key] is provided it will search the instance that have the same key
bool isAdded<T>({String? key});
bool isAdded<T>({String? key, String? className});

/// checks if the instance registration is as singleton.
/// <br>
/// When [key] is provided it will search the instance that have the same key
bool isInstantiateSingleton<T>({String? key});
bool isInstantiateSingleton<T>({String? key, String? className});

/// Removes the singleton instance.<br>
/// This does not remove it from the registry tree.
/// <br>
/// When [key] is provided it will search the instance that have the same key
T? disposeSingleton<T>({String? key});
T? disposeSingleton<T>({String? key, String? className});

/// Replaces an instance record with a concrete instance.<br>
/// This function should only be used for unit testing.<br>
/// Any other use is discouraged.
/// <br>
/// When [key] is provided it will search the instance that have the same key
void replaceInstance<T>(T instance, {String? key});
void replaceInstance<T>(T instance, {String? key, String? className});

/// Informs the container that the additions
/// are finished and the injector is ready to be used.<br>
Expand Down Expand Up @@ -233,11 +233,11 @@ class AutoInjectorImpl extends AutoInjector {
}

@override
T get<T>({ParamTransform? transform, String? key}) {
T get<T>({ParamTransform? transform, String? key, String? className}) {
_checkAutoInjectorIsCommitted();

try {
final className = T.toString();
className ??= T.toString();
T? instance;

if (key == null) {
Expand Down Expand Up @@ -270,9 +270,9 @@ class AutoInjectorImpl extends AutoInjector {
}

@override
T? tryGet<T>({ParamTransform? transform, String? key}) {
T? tryGet<T>({ParamTransform? transform, String? key, String? className}) {
try {
return get<T>(transform: transform, key: key);
return get<T>(transform: transform, key: key, className: className);
} catch (e) {
return null;
}
Expand Down Expand Up @@ -335,27 +335,29 @@ class AutoInjectorImpl extends AutoInjector {
}

@override
bool isAdded<T>({String? key}) => (key == null) //
? _hasBindByClassName(T.toString())
bool isAdded<T>({String? key, String? className}) => (key == null) //
? _hasBindByClassName(className ?? T.toString())
: _hasBindByKey(key);

@override
bool isInstantiateSingleton<T>({String? key}) {
final className = T.toString();
bool isInstantiateSingleton<T>({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<T>({String? key}) {
final response = (key == null) ? _disposeSingletonByClasseName(T.toString()) : _disposeSingletonByKey(key);
T? disposeSingleton<T>({String? key, String? className}) {
final response = (key == null)
? _disposeSingletonByClasseName(className ?? T.toString())
: _disposeSingletonByKey(key);
return response as T?;
}

@override
void replaceInstance<T>(T instance, {String? key}) {
final className = T.toString();
void replaceInstance<T>(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) {
Expand Down
106 changes: 106 additions & 0 deletions test/src/auto_injector_with_type_as_string_test.dart
Original file line number Diff line number Diff line change
@@ -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<TestDatasource>();
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<TestDatasource>();

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<TestDatasource>());
expect(injector.isAdded(className: 'TestDatasource'), true);
expect(
injector.isInstantiateSingleton(className: 'TestDatasource'),
false,
);
});
}

class TestDatasource {}