From 849f37e9134d2fa3b338606021ef031eb99f05e3 Mon Sep 17 00:00:00 2001 From: James Hughes Date: Wed, 17 Apr 2024 15:53:33 +0100 Subject: [PATCH 1/3] Added documentation for getAll --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index 3ede967..3e23098 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,40 @@ void registerLazySingleton(FactoryFunc func) You have to pass a factory function `func` that returns an instance of an implementation of `T`. Only the first time you call `get()` this factory function will be called to create a new instance. After that, you will always get the same instance returned. +### Registering multiple implementations + +There are certain circumstances where you might wish to register multiple implementations of the same interface and then get a list of all of the relevant implementations later on. For instance, you might have a modular design where each module registers an interface defining a page and then all of these get injected into your navigation bar in your main layout without your layout needing to know about each module. + +> [!NOTE] +> To avoid this being a breaking change, this is an optional feature, to enable this you need to call: + +```dart +getIt.enableRegisteringMultipleInstancesOfOneType(); +``` + +Then, you just register your classes as you normally would: + +```dart +getIt.registerLazySingleton( + () => ImplA(), +); +getIt.registerLazySingleton( + () => ImplB(), +); +``` + +Then, later on you can fetch all instances of this interface by calling: + +```dart +final Iterable instances = getIt.getAll(); +``` + +There is also an `async` implementation available for this: + +```dart +final Iterable instances = await getIt.getAllAsync(); +``` + ### 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. From edd3bb9a9b6463293ee6f025b03f760916163702 Mon Sep 17 00:00:00 2001 From: James Hughes Date: Wed, 17 Apr 2024 15:56:49 +0100 Subject: [PATCH 2/3] Tweaked formatting for note --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3e23098..ec275cc 100644 --- a/README.md +++ b/README.md @@ -157,10 +157,10 @@ There are certain circumstances where you might wish to register multiple implem > [!NOTE] > To avoid this being a breaking change, this is an optional feature, to enable this you need to call: - -```dart -getIt.enableRegisteringMultipleInstancesOfOneType(); -``` +> +>```dart +>getIt.enableRegisteringMultipleInstancesOfOneType(); +>``` Then, you just register your classes as you normally would: From 65ab55d40a8b1d1f5d3cc56c5bd4e6f60fa7879e Mon Sep 17 00:00:00 2001 From: James H Date: Sat, 20 Apr 2024 14:41:42 +0100 Subject: [PATCH 3/3] Addressed PR comments --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ec275cc..83672b7 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ You have to pass a factory function `func` that returns an instance of an implem There are certain circumstances where you might wish to register multiple implementations of the same interface and then get a list of all of the relevant implementations later on. For instance, you might have a modular design where each module registers an interface defining a page and then all of these get injected into your navigation bar in your main layout without your layout needing to know about each module. > [!NOTE] -> To avoid this being a breaking change, this is an optional feature, to enable this you need to call: +> To avoid this being a breaking change and to prevent you from erroneously re-registering a type without expecting this behaviour, to enable this you need to call: > >```dart >getIt.enableRegisteringMultipleInstancesOfOneType(); @@ -178,7 +178,7 @@ Then, later on you can fetch all instances of this interface by calling: ```dart final Iterable instances = getIt.getAll(); ``` - +The returned `Iterable` will then contain all registered instances of the requested interface with or without an instance name. There is also an `async` implementation available for this: ```dart