Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
64 changes: 54 additions & 10 deletions docs/migration/from-that-depends.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ If you need to integrate with some framework, then install `modern-di-*`.

## 2. Migrate dependencies graph
1. Use `modern-di.Group` instead of `that-depends.BaseContainer`.
2. Add scopes to all providers.
- Most of the providers will be `APP` scope (which is the default).
- ContextResource becomes `ContextProvider`.
3. Resources and singletons become factories with cache_settings passed.
2. Add scopes to providers.
- Most of the providers will be `APP` scope which is the default value.
- SQLAlchemy orm session becomes Factory of `REQUEST`-scope with cache_settings and finalizer to close connections.
- Repositories depending on ORM-session becomes factories of `REQUEST`-scope.

=== "that-depends"

Expand All @@ -43,6 +43,7 @@ If you need to integrate with some framework, then install `modern-di-*`.
class Dependencies(BaseContainer):
database_engine = providers.Resource(create_sa_engine, settings=settings.cast)
session = providers.ContextResource(create_session, engine=database_engine.cast)

decks_service = providers.Factory(repositories.DecksService, session=session)
cards_service = providers.Factory(repositories.CardsService, session=session)
```
Expand All @@ -51,19 +52,19 @@ If you need to integrate with some framework, then install `modern-di-*`.

```python
from modern_di import Group, Scope, providers

from app.repositories import CardsRepository, DecksRepository
from app.resources.db import close_sa_engine, close_session, create_sa_engine, create_session


class Dependencies(Group):
database_engine = providers.Factory(
creator=create_sa_engine, cache_settings=providers.CacheSettings(finalizer=close_sa_engine)
)
session = providers.Factory(
scope=Scope.REQUEST, creator=create_session, cache_settings=providers.CacheSettings(finalizer=close_session)
)

decks_repository = providers.Factory(
scope=Scope.REQUEST,
creator=DecksRepository,
Expand Down Expand Up @@ -95,7 +96,7 @@ Usage examples:


container = Container(groups=[Dependencies])

app = fastapi.FastAPI()
modern_di_fastapi.setup_di(app, container)
```
Expand All @@ -111,7 +112,7 @@ Usage examples:


container = Container(groups=[Dependencies])

app = Litestar(
route_handlers=[...],
plugins=[modern_di_litestar.ModernDIPlugin(container)],
Expand Down Expand Up @@ -162,3 +163,46 @@ Usage examples:
objects = await decks_service.list()
return schemas.Decks(items=objects)
```

## 5. More info about providers replacements:

- `Singleton` becomes `Factory` of `APP`-scope with cache_settings
```python
# that-depends
some_singleton = providers.Singleton(SomeClass)

# modern-di
some_factory = providers.Factory(
creator=SomeClass,
cache_settings=providers.CacheSettings()
)
```

- `Resource` becomes `Factory` of `APP`-scope with cache_settings and finalizer
```python
# that-depends
some_resource = providers.Resource(create_resource) # async or sync generator

# modern-di
some_factory = providers.Factory(
scope=Scope.APP,
creator=create_resource, # sync function
cache_settings=providers.CacheSettings(finalizer=close_resource), # async or sync function
)
```

- `List` and `Dict` becomes `Factory` which accepts some dependencies and returns list or dict accordingly
```python
# that-depends
some_list = providers.List(provider1, provider2)

# modern-di
def build_list(dep1: SomeType1, dep2: SomeType2) -> list:
return [dep1, dep2]

some_factory = providers.Factory(creator=build_list)
```

- `ContextResource` usually similar to `Resource` becomes `Factory` with `cache_settings` and `finalizer`. The difference is in scope which is usually `REQUEST`.

- `Factory` stays `Factory` without `cache_settings`
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ nav:
- Quick-Start: index.md
- Introduction:
- Resolving: introduction/resolving.md
- Key Concepts: introduction/key-concepts.md
- Providers:
- Factories: providers/factories.md
- Context: providers/context.md
Expand All @@ -22,6 +21,7 @@ nav:
- To 2.x: migration/to-2.x.md
- From that-depends: migration/from-that-depends.md
- Development:
- Key Concepts: dev/key-concepts.md
- Contributing: dev/contributing.md
- Decisions: dev/decisions.md

Expand Down
Loading