You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The existing getAll method returns a list of all services registered under a particular type, but there is not currently a way to get all of the registered services that match a particular type.
The impetus for this issue was this question. While that particular use case would be better resolved using scopes, their naive implementation was to register all of their singletons under a custom Disposable type so they could later use getAll to iterate over them and trigger disposal (not the right way to do this, I know, but stay with me here). The problem with this implementation was that they would have to fetch their services by name and manually cast them to the service type they wanted, which is problematic for several reasons. A "solution" that remained in the spirit of their approach, however, would require a way to query all registered singletons and find the ones that extended Disposable, not just the ones that were registered under Disposable.
A more realistic use case for this might be the following:
abstractinterfaceclassIOutput {
voidwrite(String message);
}
classConsoleOutputimplementsIOutput {
@overridevoidwrite(String message) {
// Write to console
}
}
classFileOutputimplementsIOutput {
@overridevoidwrite(String message) {
// Write to local file
}
voidclearFile() {
// Truncate local file
}
}
classRemoteLoggingOutputimplementsIOutput {
@overridevoidwrite(String message) {
// Write to remote logging service
}
voidsetTarget(String url) {
// Configure the target of the logging service
}
}
...
voidinitGetIt() {
final getIt =GetIt.I;
// Ideal
getIt.registerSingleton(ConsoleOutput());
getIt.registerSingleton(FileOutput());
getIt.registerSingleton(RemoteLoggingOutput());
// Not ideal for this use case
getIt.registerSingleton<IOutput>(ConsoleOutput(), instanceName:'console');
getIt.registerSingleton<IOutput>(FileOutput(), instanceName:'file');
getIt.registerSingleton<IOutput>(RemoteLoggingOutput(), instanceName:'remoteLogging');
}
Using the first approach, I can access the individual services by type and type safety would be maintained without my needing to worry about getting them by name and casting them to the type I want.
// This is goodGetIt.I.get<FileOutput>().clearFile();
GetIt.I.get<RemoteLoggingOutput>().setTarget('https://api.example.com/log');
// This is verbose and error-prone
(GetIt.I.get<IOutput>(instanceName:'file') asFileOutput).clearFile();
(GetIt.I.get<IOutput>(instanceName:'remoteLogging') asRemoteLoggingOutput).setTarget('https://api.example.com/log');
But if I wanted to write something to all of the services, there's no way to do that with the first approach. I would expect to be able to do something like this:
The existing
getAll
method returns a list of all services registered under a particular type, but there is not currently a way to get all of the registered services that match a particular type.The impetus for this issue was this question. While that particular use case would be better resolved using scopes, their naive implementation was to register all of their singletons under a custom
Disposable
type so they could later usegetAll
to iterate over them and trigger disposal (not the right way to do this, I know, but stay with me here). The problem with this implementation was that they would have to fetch their services by name and manually cast them to the service type they wanted, which is problematic for several reasons. A "solution" that remained in the spirit of their approach, however, would require a way to query all registered singletons and find the ones that extendedDisposable
, not just the ones that were registered underDisposable
.A more realistic use case for this might be the following:
Using the first approach, I can access the individual services by type and type safety would be maintained without my needing to worry about getting them by name and casting them to the type I want.
But if I wanted to write something to all of the services, there's no way to do that with the first approach. I would expect to be able to do something like this:
The text was updated successfully, but these errors were encountered: