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 all from one type #354

Merged
merged 2 commits into from
Feb 19, 2024
Merged

Get all from one type #354

merged 2 commits into from
Feb 19, 2024

Conversation

escamoteur
Copy link
Collaborator

No description provided.

@escamoteur escamoteur merged commit 89f5e2e into master Feb 19, 2024
2 checks passed
/// 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
static bool allowRegisterMultipleImplementationsOfoneType = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this wholly necessary? What was the behaviour previously? Would it error or fail silently? If it would error, I don't think this is necessary as it wouldn't break/change any already extant behaviour. If the latter, then I guess it's debatable.

I don't generally think it's a good idea to have functionality dictated by flags in properties.

@escamoteur
Copy link
Collaborator Author

escamoteur commented Feb 20, 2024 via email

@hughesjs
Copy link
Contributor

hughesjs commented Feb 20, 2024

In the past it would throw and I argue that in 90% of cases it's a bug if you try to register more than one instance of one type. It would be a breaking change otherwise which I try to prevent. Am 20. Feb. 2024, 15:55 +0100 schrieb James H @.>:

@hughesjs commented on this pull request. In lib/get_it.dart: > @@ -151,6 +151,11 @@ abstract class GetIt { /// If you really need to you can disable the asserts by setting[allowReassignment]= true bool allowReassignment = 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 + static bool allowRegisterMultipleImplementationsOfoneType = false; Is this wholly necessary? What was the behaviour previously? Would it error or fail silently? If it would error, I don't think this is necessary as it wouldn't break/change any already extant behaviour. If the latter, then I guess it's debatable. I don't generally think it's a good idea to have functionality dictated by flags in properties. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you modified the open/close state.Message ID: @.
>

I don't think this use-case is quite as esoteric as you think. That being said, maybe there's another way to handle this than a property if you do want to maintain the behaviour.

Normally, I'd suggest implementing a derived class which allows the extended behaviour here, but I don't know how well that would play with the global static approach that get it has. Maybe we stick a method on on the static class such as .enableRegisteringMultipleInstances() which substitutes the old implementation with the extended one?

It's a bit more verbose than what's here, but I think it would be easier to debug and reason with. You could also add a note to the exception pointing people towards using the new implementation if it was intentional.

@hughesjs
Copy link
Contributor

hughesjs commented Feb 20, 2024

Another issue with the current implementation, I think, would be what happens if this is set to true and later changed to false? Should it clear out multiple registrations or just prevent more? It's not clear.

@escamoteur
Copy link
Collaborator Author

escamoteur commented Feb 20, 2024 via email

@hughesjs
Copy link
Contributor

In that case... Can we set it through a method that fails if you try to set it more than once

@escamoteur
Copy link
Collaborator Author

escamoteur commented Feb 20, 2024 via email

@hughesjs
Copy link
Contributor

And make the prop private?

@escamoteur
Copy link
Collaborator Author

escamoteur commented Feb 20, 2024 via email

@hughesjs
Copy link
Contributor

Sounds great :)

@@ -971,9 +1056,13 @@ class _GetItImplementation implements GetIt {
}
}

final typeRegistration = registrationScope.typeRegistrations
.putIfAbsent(T, () => _TypeRegistration());
Copy link
Contributor

@tpucci tpucci Feb 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this new feature.
Here we miss the generic type T for it to work 🙂

-  .putIfAbsent(T, () => _TypeRegistration());
+  .putIfAbsent(T, () => _TypeRegistration<T>());

final factories = factoryToRemove.registeredIn.factories;
if (factories.contains(factoryToRemove)) {
factories.remove(factoryToRemove);
if (factories.isEmpty) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@escamoteur @spydon

As per my understanding line 1273 is the root cause for #358

there is a chance name factory exist but, just factories are empty. in this case factoryToRemove should not be removed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, good find!

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

Successfully merging this pull request may close these issues.

None yet

5 participants