Skip to content

Commit

Permalink
v1.1.0 🍇
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoPrevato authored Jan 31, 2021
1 parent 0540a0c commit 1de2a10
Show file tree
Hide file tree
Showing 7 changed files with 719 additions and 346 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.9] - 2021-01-31 :grapes:
- Adds support to resolve class attributes annotations
- Changes how classes without an `__init__` method are handled
- Updates links to the GitHub organization, [Neoteroi](https://github.com/Neoteroi)

## [1.0.9] - 2020-11-08 :octocat:
- Completely migrates to GitHub Workflows
- Improves build to test Python 3.6 and 3.9
Expand Down
62 changes: 47 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
![Build](https://github.com/RobertoPrevato/rodi/workflows/Build/badge.svg)
![Build](https://github.com/Neoteroi/rodi/workflows/Build/badge.svg)
[![pypi](https://img.shields.io/pypi/v/rodi.svg)](https://pypi.python.org/pypi/rodi)
[![versions](https://img.shields.io/pypi/pyversions/rodi.svg)](https://github.com/RobertoPrevato/rodi)
[![codecov](https://codecov.io/gh/RobertoPrevato/rodi/branch/master/graph/badge.svg?token=VzAnusWIZt)](https://codecov.io/gh/RobertoPrevato/rodi)
[![license](https://img.shields.io/github/license/RobertoPrevato/rodi.svg)](https://github.com/RobertoPrevato/rodi/blob/master/LICENSE)
[![versions](https://img.shields.io/pypi/pyversions/rodi.svg)](https://github.com/Neoteroi/rodi)
[![codecov](https://codecov.io/gh/Neoteroi/rodi/branch/master/graph/badge.svg?token=VzAnusWIZt)](https://codecov.io/gh/Neoteroi/rodi)
[![license](https://img.shields.io/github/license/Neoteroi/rodi.svg)](https://github.com/Neoteroi/rodi/blob/master/LICENSE)

# Implementation of dependency injection for Python 3

**Features:**
* types resolution by signature types annotations (_type hints_)
* types resolution by constructor parameter names and aliases (_convention over configuration_)
* unintrusive: builds objects graph **without** the need to change classes source code (unlike some other Python implementations of dependency injection, like _[inject](https://pypi.org/project/Inject/)_)
* types resolution by class annotations (_type hints_) (new in version 1.1.0 :star:)
* types resolution by constructor parameter names and aliases (_convention over
configuration_)
* unintrusive: builds objects graph **without** the need to change classes
source code (unlike some other Python implementations of dependency
injection, like _[inject](https://pypi.org/project/Inject/)_)
* minimum overhead to obtain services, once the objects graph is built
* support for singletons, transient and scoped services
* support for singletons, transient, and scoped services

This library is freely inspired by .NET Standard `Microsoft.Extensions.DependencyInjection` implementation (_ref. [MSDN, Dependency injection in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1), [Using dependency injection in a .Net Core console application](https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/)_).
This library is freely inspired by .NET Standard
`Microsoft.Extensions.DependencyInjection` implementation (_ref. [MSDN,
Dependency injection in ASP.NET
Core](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1),
[Using dependency injection in a .Net Core console
application](https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/)_).

## Installation

Expand All @@ -22,16 +31,30 @@ pip install rodi
```

## De gustibus non disputandum est
This library is designed to work both by type hints in constructor signatures, and by constructor parameter names (convention over configuration), like described in below diagram. It can be useful for those who like type hinting, those who don't, and those who like having both options.
This library is designed to work both by type hints in constructor signatures,
and by constructor parameter names (convention over configuration), like
described in below diagram. It can be useful for those who like type hinting,
those who don't, and those who like having both options.

![Usage option](https://raw.githubusercontent.com/RobertoPrevato/rodi/master/documentation/rodi-design-taste.png "Usage option")
![Usage
option](https://raw.githubusercontent.com/Neoteroi/rodi/master/documentation/rodi-design-taste.png
"Usage option")

## Minimum overhead
`rodi` works by inspecting ____init____ methods **once** at runtime, to generate functions that return instances of desired types. Validation steps, for example to detect circular dependencies or missing services, are done when building these functions, so additional validation is not needed when activating services.
`rodi` works by inspecting ____init____ methods **once** at
runtime, to generate functions that return instances of desired types.
Validation steps, for example to detect circular dependencies or missing
services, are done when building these functions, so additional validation is
not needed when activating services.

For this reason, services are first registered inside an instance of _Container_ class, which implements a method _build_provider()_ that returns an instance of _Services_. The service provider is then used to obtain desired services by type or name. Inspection and validation steps are done only when creating an instance of service provider.
For this reason, services are first registered inside an instance of
_Container_ class, which implements a method _build_provider()_ that
returns an instance of _Services_. The service provider is then used to obtain
desired services by type or name. Inspection and validation steps are done only
when creating an instance of service provider.

![Classes](https://raw.githubusercontent.com/RobertoPrevato/rodi/master/documentation/classes.png "Classes")
![Classes](https://raw.githubusercontent.com/Neoteroi/rodi/master/documentation/classes.png
"Classes")

In the example below, a singleton is registered by exact instance.

Expand All @@ -50,7 +73,16 @@ assert cat.name == "Celine"
## Service life style:
* singleton - instantiated only once per service provider
* transient - services are instantiated every time they are required
* scoped - instantiated only once per service resolution (root call, e.g. once per web request)
* scoped - instantiated only once per service resolution (root call, e.g. once
per web request)

## Usage in BlackSheep
`rodi` is used in the [BlackSheep](https://www.neoteroi.dev/blacksheep/) web
framework to implement [dependency
injection](https://www.neoteroi.dev/blacksheep/dependency-injection/) for
request handlers. Refer to the framework's documentation for more information
on how dependency injection is

# Documentation
For documentation and examples, please refer to the [wiki in GitHub, https://github.com/RobertoPrevato/rodi/wiki](https://github.com/RobertoPrevato/rodi/wiki).
For documentation and examples, please refer to the [wiki in GitHub,
https://github.com/Neoteroi/rodi/wiki](https://github.com/Neoteroi/rodi/wiki).
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pytest-cov
pytest-asyncio
flake8
mypy
dataclasses==0.8; python_version < '3.7'
Loading

0 comments on commit 1de2a10

Please sign in to comment.