-
Notifications
You must be signed in to change notification settings - Fork 45
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
Dependency Injection: Scopes #9
Labels
Milestone
Comments
The snippet above is not all that is needed. Consider following use-case: @scope(Scopes.SINGLETON)
class ConsoleMailer(IMailer):
def __init__(self, container, session):
self.container = container
def send_to_user(self, body: str, ...):
print("Sending email to current user")
print(body)
@scope(Scopes.SESSION)
class SmtpMailer(IMailer):
def __init__(self, container):
self.container = container
def send_to_user(self, body: str, ...):
session = self.container.find_by_interface(ISession)
email.send(to=session.user.email, body=body, ...)
# app.py
Container.register_by_interface(IMailer, SmptMailer)
# ctl.py
Container.register_by_interface(IMailer, ConsoleMailer) So:
|
I have flagged this issue as Important: goals of incoming version (see roadmap) will need session scope and request scope, at least in the context of CLI. |
Note: the request scope in the context of CLI is just a singleton scope, as the process running CLI command finishes at the end of the request. I See two solutions:
|
lhaze
added a commit
that referenced
this issue
Dec 26, 2018
* added Scopes.SINGLETON * done some DRYing of the DI Container code * added some docstrings
lhaze
added a commit
that referenced
this issue
Dec 26, 2018
* added Scopes.SINGLETON * done some DRYing of the DI Container code * added some docstrings
jakos
pushed a commit
that referenced
this issue
Dec 31, 2018
* added Scopes.SINGLETON * done some DRYing of the DI Container code * added some docstrings
11 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Follows #1
When a constructor is registered in the DI Container, registrar can define a scope of the instance created by the constructor. I think of them as an Enum of objects (functions? objects with state?), that know when they want to create a new instance and when to return the old one.
I think of them as something like the code below:
The text was updated successfully, but these errors were encountered: