Created
June 24, 2020 09:43
-
-
Save iurisilvio/830a48a34e284a8a7370acadf60eca12 to your computer and use it in GitHub Desktop.
Django IoC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [1]: from django.conf import settings | |
...: from django.utils.module_loading import import_string | |
...: | |
...: | |
...: class ServiceFactory: | |
...: def __init__(self, django_setting): | |
...: self._django_setting = django_setting | |
...: | |
...: def __call__(self, import_str=None): | |
...: if import_str is None: | |
...: import_str = getattr(settings, self._django_setting) | |
...: return import_string(import_str) | |
...: | |
...: | |
...: # setting SNS_BACKEND='adapters.sns_adapter.sns' | |
...: get_sns_service = ServiceFactory('SNS_BACKEND') | |
...: | |
In [2]: get_sns_service() | |
Out[2]: <module 'adapters.sns_adapter.sns' from '/code/adapters/sns_adapter/sns.py'> | |
In [3]: get_sns_service('adapters.sns_adapter.dummy') | |
Out[3]: <module 'adapters.sns_adapter.dummy' from '/code/adapters/sns_adapter/dummy.py'> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment