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: inject profile #2705

Merged
merged 2 commits into from
Jan 11, 2024

Conversation

dbluhm
Copy link
Contributor

@dbluhm dbluhm commented Jan 10, 2024

This PR adds the Profile to the injection context. This enables creating a ClassProvider for a class that takes a profile in its init method:

class ExamplePluggable:
    def __init__(self, profile: Profile):
        self.profile = profile
    def do_something(self):
        self.profile.notify("some:event", {"with": "data"})

async def setup(context: InjectionContext):
    context.injector.bind_provider(
        ExamplePluggable,
        ClassProvider(ExamplePluggable, ClassProvider.Inject(Profile))
    )

# ... Somewhere else ...
plugged_in = profile.inject(ExamplePluggable)
plugged_in.do_something()

It's a common pattern to create a class with a profile instance. This will make it easier to turn some of these components into pluggable components. For example, the VcLdpManager. At the moment, it's created directly where it's needed:

manager = VcLdpManager(self.profile)
# do something with manager

With this change, I can create an (override-able) class provider for VcLdpManager, which changes the above into:

manager = self.profile.inject(VcLdpManager)
# do something with manager

The only way this could be done previously was by binding providers in the Profile init method itself. This made it more difficult to make more components pluggable.

Side note: it's critical that the profile instance be injected as a weak reference or it will never be "finalized" (garbage collected) in multitenant contexts.

Copy link

sonarcloud bot commented Jan 10, 2024

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@dbluhm
Copy link
Contributor Author

dbluhm commented Jan 10, 2024

Feedback on any unforeseen consequences of this are welcome; given that this pattern was kind of present in those profile init bind provider calls, I think it should be fine.

@dbluhm
Copy link
Contributor Author

dbluhm commented Jan 10, 2024

Sorry, mixed up the assignee and reviewer fields... 😅

@dbluhm dbluhm requested a review from jamshale January 10, 2024 15:06
@usingtechnology usingtechnology removed their request for review January 10, 2024 21:34
Copy link
Contributor

@jamshale jamshale left a comment

Choose a reason for hiding this comment

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

I can't comment on unforeseen consequences, but it looks good to me.

@swcurran swcurran merged commit 2a18657 into openwallet-foundation:main Jan 11, 2024
8 checks passed
@dbluhm dbluhm deleted the feature/injected-profile branch January 11, 2024 16:30
@jamshale jamshale mentioned this pull request Feb 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.

6 participants