Skip to content
Merged
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ You have to pass a factory function `func` that returns an instance of an implem
### Overwriting registrations

If you try to register a type more than once you will fail with an assertion in debug mode because normally this is not needed and probably a bug.
If you really have to overwrite a registration, then you can by setting the property `allowReassignment==true`.
If you really have to overwrite a registration, then you can by setting the property `allowReassignment = true`.

### Skip Double registrations

If you try to register a type more than once and when `allowReassignment = false` you will fail with an assertion in debug mode.
If you want to just skip this double registration silently without an error, then you can by setting the property `skipDoubleRegistration = true`.

### Testing if a Singleton is already registered

Expand Down
6 changes: 6 additions & 0 deletions lib/get_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:collection';

import 'package:async/async.dart';
import 'package:collection/collection.dart' show IterableExtension;
import 'package:meta/meta.dart';

part 'get_it_impl.dart';

Expand Down Expand Up @@ -151,6 +152,11 @@ abstract class GetIt {
/// If you really need to you can disable the asserts by setting[allowReassignment]= true
bool allowReassignment = false;

/// By default it's throws error when [allowReassignment]= false. and trying to register same type
/// If you really need, you can disable the Asserts / Errror by setting[skipDoubleRegistration]= true
@visibleForTesting
bool skipDoubleRegistration = false;

/// Till V7.6.7 GetIt didn't allow to register multiple instances of the same type.
/// if you want to register multiple instances of the same type you can enable this
/// and use `getAll()` to retrieve all instances of that parent type
Expand Down
22 changes: 20 additions & 2 deletions lib/get_it_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ class _GetItImplementation implements GetIt {
@override
bool allowReassignment = false;

/// By default it's throws error when [allowReassignment]= false. and trying to register same type
/// If you really need, you can disable the Asserts / Errror by setting[skipDoubleRegistration]= true
@visibleForTesting
@override
bool skipDoubleRegistration = false;

/// Is used by several other functions to retrieve the correct [_ServiceFactory]
_ServiceFactory<T, dynamic, dynamic>?
_findFirstFactoryByNameAndTypeOrNull<T extends Object>(
Expand Down Expand Up @@ -1027,19 +1033,31 @@ class _GetItImplementation implements GetIt {
if (instanceName != null) {
throwIf(
existingTypeRegistration.namedFactories.containsKey(instanceName) &&
!allowReassignment,
!allowReassignment &&
!skipDoubleRegistration,
ArgumentError(
'Object/factory with name $instanceName and '
'type $T is already registered inside GetIt. ',
),
);

/// skip double registration
if (skipDoubleRegistration && !allowReassignment) {
return;
}
} else {
if (existingTypeRegistration.factories.isNotEmpty) {
throwIfNot(
allowReassignment ||
GetIt.allowRegisterMultipleImplementationsOfoneType,
GetIt.allowRegisterMultipleImplementationsOfoneType ||
skipDoubleRegistration,
ArgumentError('Type $T is already registered inside GetIt. '),
);

/// skip double registration
if (skipDoubleRegistration && !allowReassignment) {
return;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ environment:
dependencies:
async: ^2.11.0
collection: ^1.17.1
meta: ^1.11.0

dev_dependencies:
lint: ^2.1.2
Expand Down
57 changes: 57 additions & 0 deletions test/skip_double_registration_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:get_it/get_it.dart';
import 'package:test/test.dart';

void main() {
test(' Throws ArgumentError ', () async {
final getIt = GetIt.instance;
getIt.allowReassignment = false;
getIt.skipDoubleRegistration = false;
getIt.reset();
getIt.registerSingleton<DataStore>(MockDataStore());

expect(
() => getIt.registerSingleton<DataStore>(RemoteDataStore()),
throwsArgumentError,
);
});

test(' replaces dependency safely ', () async {
final getIt = GetIt.instance;
getIt.reset();
getIt.allowReassignment = true;
getIt.skipDoubleRegistration = false;
getIt.registerSingleton<DataStore>(MockDataStore());
getIt.registerSingleton<DataStore>(RemoteDataStore());

expect(getIt<DataStore>(), isA<RemoteDataStore>());
});

test(' Ignores Double registration error ', () async {
final getIt = GetIt.instance;
getIt.reset();
getIt.allowReassignment = false;
getIt.skipDoubleRegistration = true;
getIt.registerSingleton<DataStore>(MockDataStore());
final remoteDataStore = RemoteDataStore();
getIt.registerSingleton<DataStore>(remoteDataStore);

expect(getIt<DataStore>(), isA<MockDataStore>());
});

test(' does not care about [skipDoubleRegistration] varibale ', () async {
final getIt = GetIt.instance;
getIt.reset();
getIt.allowReassignment = true;
getIt.skipDoubleRegistration = true;
getIt.registerSingleton<DataStore>(MockDataStore());
getIt.registerSingleton<DataStore>(RemoteDataStore());

expect(getIt<DataStore>(), isA<RemoteDataStore>());
});
}

abstract class DataStore {}

class RemoteDataStore implements DataStore {}

class MockDataStore implements DataStore {}