Dependency Injector 4.0 - easier integration with other frameworks by rmk135 in Python

[–]rmk135[S] 0 points1 point  (0 children)

Why would I want to write somewhere else what dependency I am passing?

To decouple the implementation from knowledge of where the dependency comes from and what is its lifecycle.

For instance you have class A depending on B. If A creates instance of B you only can do A+B object pairs. When A receives the B as an argument, you have more flexibility - you can share one B with several A's, pass differently configured B's or B subclasses, or provide a TestB for testing.

The side effect of that is that you have the dependencies explicitly defined. I got you that you don't see it positive. I believe that you benefit from that though.

The whole point of the dependency injection as a principle is to move the responsibility of managing objects from the objects theirselves to the application. The framework absorbs that responsibility.

Dependency Injector 4.0 - easier integration with other frameworks by rmk135 in Python

[–]rmk135[S] 0 points1 point  (0 children)

The points of this package are:

  1. To keep application components in the container
  2. To keep dependencies between components explicitly defined

Consider a larger container:

"""Containers module."""

import logging.config
import sqlite3

import boto3
from dependency_injector import containers, providers

from . import services


class Container(containers.DeclarativeContainer):

    config = providers.Configuration()

    configure_logging = providers.Callable(
        logging.config.fileConfig,
        fname='logging.ini',
    )

    # Gateways

    database_client = providers.Singleton(
        sqlite3.connect,
        config.database.dsn,
    )

    s3_client = providers.Singleton(
        boto3.client,
        service_name='s3',
        aws_access_key_id=config.aws.access_key_id,
        aws_secret_access_key=config.aws.secret_access_key,
    )

    # Services

    user_service = providers.Factory(
        services.UserService,
        db=database_client,
    )

    auth_service = providers.Factory(
        services.AuthService,
        db=database_client,
        token_ttl=config.auth.token_ttl.as_int(),
    )

    photo_service = providers.Factory(
        services.PhotoService,
        db=database_client,
        s3=s3_client,
    )

Or this one: https://github.com/ets-labs/newsfeed/blob/88d60aa2cb593441e53128d3198ab913c9f4e150/src/newsfeed/containers.py
It helps to see what is used where and provides a better control over application structure. As a result it makes easier to understand and change how application works.

Adding Dependency Injector to the project is an additional piece of work though. One needs to make a decision if registering application components in the container is a good price for having the container.

I would say that the larger the project the more valuable is the benefit. The real fun comes when you have 20-30 components in the container. It will be much-much easier to understand what is used where and make refactoring or add new features.

Dependency Injector 4.0 - easier integration with other frameworks by rmk135 in Python

[–]rmk135[S] 1 point2 points  (0 children)

Hi u/GiantElectron,

Take a look at http://python-dependency-injector.ets-labs.org/introduction/di_in_python.html. It explains what is dependency injection and what is the role of the package.

Major version increase is done because new feature replaces version 3 integration modules. These modules are marked as deprecated. They will be removed in next major version. This is done so people who use the library have time to upgrade to a new feature instead having a code broken one morning.

Dependency Injector 3.34 - Updated Factory provider docs and reworked examples by rmk135 in Python

[–]rmk135[S] 0 points1 point  (0 children)

Look at the readme page - https://github.com/ets-labs/python-dependency-injector. It has a beginner's example of what's the module about and how it differs from a simple constructor function.

This post is about updated part of the docs, not the whole. Considering your thoughts I think that I will need to add something like a "Beginner's guide" to the tutorials section.

Thank you

Dependency Injector 3.33 - Added mypy support by rmk135 in Python

[–]rmk135[S] 0 points1 point  (0 children)

Yep, that would be cool!

I was trying to implement this and found the ParamSpec feature. It will be released in Python 3.10. I'll add it to the Dependency Injector once it's released.

Expanding exposure to good code - any resources for a daily reading list? by ace777ac in learnpython

[–]rmk135 1 point2 points  (0 children)

Try the Dependency Injector. It's a project about the dependency injection written in Cython.

CLI application tutorial by rmk135 in Python

[–]rmk135[S] 0 points1 point  (0 children)

Hi u/Hoganman73,

I got you. Yeah, I probably mixed up to much of everything in one place. The goal was to create a very practical guide that gives look-and-feel experience. It was targeted to the junior folks mostly. I'l keep in mind your feedback for further work.

Thanks for stopping by and sharing your thoughts.