mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-10 16:12:26 +03:00
Add use cases example
This commit is contained in:
parent
ec1fd7414c
commit
c03b97d8ea
9
examples/miniapps/use_cases/README.rst
Normal file
9
examples/miniapps/use_cases/README.rst
Normal file
|
@ -0,0 +1,9 @@
|
|||
Dependency Injector Use Cases example
|
||||
=====================================
|
||||
|
||||
Instructions for running
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
python run.py prod example@example.com
|
||||
python run.py test example@example.com
|
30
examples/miniapps/use_cases/containers.py
Normal file
30
examples/miniapps/use_cases/containers.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
"""Dependency injection containers for 'Use Cases' example application."""
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
|
||||
from example.adapters import SmtpEmailSender, EchoEmailSender
|
||||
from example.use_cases import SignupUseCase
|
||||
|
||||
|
||||
class Adapters(containers.DeclarativeContainer):
|
||||
"""Adapters container."""
|
||||
|
||||
email_sender = providers.Singleton(SmtpEmailSender)
|
||||
|
||||
|
||||
class TestAdapters(containers.DeclarativeContainer):
|
||||
"""Adapters container.
|
||||
|
||||
This container is used for testing purposes.
|
||||
"""
|
||||
|
||||
email_sender = providers.Singleton(EchoEmailSender)
|
||||
|
||||
|
||||
class UseCases(containers.DeclarativeContainer):
|
||||
"""Use cases container."""
|
||||
|
||||
adapters = providers.DependenciesContainer()
|
||||
|
||||
signup = providers.Factory(SignupUseCase,
|
||||
email_sender=adapters.email_sender)
|
1
examples/miniapps/use_cases/example/__init__.py
Normal file
1
examples/miniapps/use_cases/example/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
"""Example top-level package."""
|
25
examples/miniapps/use_cases/example/adapters.py
Normal file
25
examples/miniapps/use_cases/example/adapters.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
"""Example adapters package."""
|
||||
|
||||
|
||||
class EmailSender(object):
|
||||
"""Abstract email sender."""
|
||||
|
||||
def send(self, to, body):
|
||||
"""Send email to specified email."""
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class SmtpEmailSender(object):
|
||||
"""SMTP email sender uses SMTP protocol for sending emails."""
|
||||
|
||||
def send(self, to, body):
|
||||
"""Send email to specified email."""
|
||||
# Send email via SMTP
|
||||
|
||||
|
||||
class EchoEmailSender(object):
|
||||
"""Echo email sender prints emails to stdout."""
|
||||
|
||||
def send(self, to, body):
|
||||
"""Send email to specified email."""
|
||||
print('Sending email to "{0}", body = "{1}"'.format(to, body))
|
22
examples/miniapps/use_cases/example/use_cases.py
Normal file
22
examples/miniapps/use_cases/example/use_cases.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
"""Example use cases package."""
|
||||
|
||||
|
||||
class UseCase(object):
|
||||
"""Abstract use case."""
|
||||
|
||||
def execute(self):
|
||||
"""Execute use case handling."""
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class SignupUseCase(object):
|
||||
"""Sign up use cases registers users."""
|
||||
|
||||
def __init__(self, email_sender):
|
||||
"""Initializer."""
|
||||
self.email_sender = email_sender
|
||||
|
||||
def execute(self, email):
|
||||
"""Execute use case handling."""
|
||||
print('Sign up user {0}'.format(email))
|
||||
self.email_sender.send(email, 'Welcome, "{}"'.format(email))
|
19
examples/miniapps/use_cases/run.py
Normal file
19
examples/miniapps/use_cases/run.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
"""Run 'Use Cases' example application."""
|
||||
|
||||
import sys
|
||||
|
||||
from containers import Adapters, TestAdapters, UseCases
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
environment, email = sys.argv[1:]
|
||||
|
||||
if environment == 'prod':
|
||||
adapters = Adapters()
|
||||
elif environment == 'test':
|
||||
adapters = TestAdapters()
|
||||
|
||||
use_cases = UseCases(adapters=adapters)
|
||||
|
||||
use_case = use_cases.signup()
|
||||
use_case.execute(email)
|
Loading…
Reference in New Issue
Block a user