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

feat: Create scoped container that inherit a parent container's registrations #81

Closed
wants to merge 1 commit into from

Conversation

ruicraveiro
Copy link

Hi,

I am proposing this PR to add the only feature I am missing from Kiwi. It is the need to create a scoped container, but one that reuses all the parent's registrations. The idea is to emulate the feature I used in Unity in the past, http://unitycontainer.org/api/Unity.IUnityContainer.html#Unity_IUnityContainer_CreateChildContainer, but keeping Kiwi's existing API as unchanged as possible.

The core of the PR is the scoped constructor, to which I added an optional parent parameter. When given a parent, the constructor will create a new _nameProviders map, which is essentially a deep clone of the parent's map:

  KiwiContainer.scoped([KiwiContainer? parent])
      : _namedProviders = parent == null
            ? Map<String?, Map<Type, _Provider<Object>>>()
            : parent._namedProviders
                .map((key, value) => MapEntry(key, {...value}));

The idea is for the scoped container to be a fork of the parent's registrations. After construction, all further modifications to the parent are not propagated to the scoped container. No modification to the scoped container should have an impact on the parent container. I added tests to check those behaviours.

Thank you for considering this PR.

Kind Regards,

Rui

)));
});

// test('instances should be resolved', () {
Copy link
Collaborator

Choose a reason for hiding this comment

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

do not keep commented tests

@@ -16,19 +16,6 @@ void main() {
expect(c1, c2);
});

test('KiwiContainer.scope should be a different object', () {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why did you remove this one? I would assume this test should still be working.

@vanlooverenkoen
Copy link
Collaborator

Sorry for the long wait on the review. Forgot about this package, but I am back at it again.

@PhilipSA
Copy link

@vanlooverenkoen Would love to have this but the original author seems to have forgotten about it. How could we proceed?

@vanlooverenkoen
Copy link
Collaborator

@PhilipSA you could fork the repo that is linked to this pull request and I will review that. And close this one

@gbtb16
Copy link
Owner

gbtb16 commented Jan 15, 2024

Hi @ruicraveiro, your implementation was used as a basis in PR #101. I'm going to close this PR since the changes are already in the master. Thanks for your contribution!

@gbtb16 gbtb16 closed this Jan 15, 2024
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.

4 participants