Skip to content

[Question] locating several implementations of a common type? #279

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

Closed
tiloc opened this issue Jun 3, 2022 · 6 comments
Closed

[Question] locating several implementations of a common type? #279

tiloc opened this issue Jun 3, 2022 · 6 comments

Comments

@tiloc
Copy link

tiloc commented Jun 3, 2022

Is there a function or a best practice how I can register several implementations of a common type and then later locate all of them? For instance, I have common type called "Gadget" and several derived types, like "AGadget", "BGadget", "MockGadget", etc. What I want is to register "AGadget" and "BGadget", and then later locate both of them with something like GetIt.I.list() which should return both of them, so I can loop over them and invoke a method on them.

It seems that the registration and get methods in GetIt are only applicable to a single instance per type?

@escamoteur
Copy link
Collaborator

escamoteur commented Jun 3, 2022 via email

@tiloc
Copy link
Author

tiloc commented Jun 3, 2022

My use-case is an app which can simultaneously talk to multiple devices through proprietary protocol drivers. I have bundled each driver into a Dart class with a common parent type. Depending on licensing, regional regulations, demo mode, unit test mode etc. I want to register the applicable devices during startup of the app and then later use the locator in all other places of the app to determine the registered devices. Kind of like this:

main.dart:

if (expensiveDeviceLicensed) {
  GetIt.registerMultiple<DeviceDriver>(ExpensiveDeviceDriver);
}
if (inUnitedStates) {
  GetIt.registerMultiple<DeviceDriver>(AmericanDeviceDriver);
}

later_in_app.dart:

final drivers = GetIt.list<DeviceDriver>();
for(final driver in drivers) {
  driver.talkToDevice();
}

@escamoteur
Copy link
Collaborator

escamoteur commented Jun 3, 2022 via email

@tiloc
Copy link
Author

tiloc commented Jun 3, 2022

yes, it is listening to multiple devices through various drivers fully in parallel.

@escamoteur
Copy link
Collaborator

escamoteur commented Jun 3, 2022 via email

@tiloc
Copy link
Author

tiloc commented Jun 4, 2022

Thanks for sharing your views with me. So yes, I think I will go with the device manager approach for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants