What is a Pythonic way for Dependency Injection?

后端 未结 9 2143
终归单人心
终归单人心 2020-12-02 06:57

Introduction

For Java, Dependency Injection works as pure OOP, i.e. you provide an interface to be implemented and in your framework code accept an instance of a c

9条回答
  •  执念已碎
    2020-12-02 07:50

    Some time ago I wrote dependency injection microframework with a ambition to make it Pythonic - Dependency Injector. That's how your code can look like in case of its usage:

    """Example of dependency injection in Python."""
    
    import logging
    import sqlite3
    
    import boto.s3.connection
    
    import example.main
    import example.services
    
    import dependency_injector.containers as containers
    import dependency_injector.providers as providers
    
    
    class Platform(containers.DeclarativeContainer):
        """IoC container of platform service providers."""
    
        logger = providers.Singleton(logging.Logger, name='example')
    
        database = providers.Singleton(sqlite3.connect, ':memory:')
    
        s3 = providers.Singleton(boto.s3.connection.S3Connection,
                                 aws_access_key_id='KEY',
                                 aws_secret_access_key='SECRET')
    
    
    class Services(containers.DeclarativeContainer):
        """IoC container of business service providers."""
    
        users = providers.Factory(example.services.UsersService,
                                  logger=Platform.logger,
                                  db=Platform.database)
    
        auth = providers.Factory(example.services.AuthService,
                                 logger=Platform.logger,
                                 db=Platform.database,
                                 token_ttl=3600)
    
        photos = providers.Factory(example.services.PhotosService,
                                   logger=Platform.logger,
                                   db=Platform.database,
                                   s3=Platform.s3)
    
    
    class Application(containers.DeclarativeContainer):
        """IoC container of application component providers."""
    
        main = providers.Callable(example.main.main,
                                  users_service=Services.users,
                                  auth_service=Services.auth,
                                  photos_service=Services.photos)
    

    Here is a link to more extensive description of this example - http://python-dependency-injector.ets-labs.org/examples/services_miniapp.html

    Hope it can help a bit. For more information please visit:

    • GitHub https://github.com/ets-labs/python-dependency-injector
    • Docs http://python-dependency-injector.ets-labs.org/

提交回复
热议问题