diff --git a/docs/introduction/key-concepts.md b/docs/dev/key-concepts.md similarity index 100% rename from docs/introduction/key-concepts.md rename to docs/dev/key-concepts.md diff --git a/docs/migration/from-that-depends.md b/docs/migration/from-that-depends.md index 2ebc9d5..3ddf197 100644 --- a/docs/migration/from-that-depends.md +++ b/docs/migration/from-that-depends.md @@ -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" @@ -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) ``` @@ -51,11 +52,11 @@ 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) @@ -63,7 +64,7 @@ If you need to integrate with some framework, then install `modern-di-*`. 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, @@ -95,7 +96,7 @@ Usage examples: container = Container(groups=[Dependencies]) - + app = fastapi.FastAPI() modern_di_fastapi.setup_di(app, container) ``` @@ -111,7 +112,7 @@ Usage examples: container = Container(groups=[Dependencies]) - + app = Litestar( route_handlers=[...], plugins=[modern_di_litestar.ModernDIPlugin(container)], @@ -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` diff --git a/mkdocs.yml b/mkdocs.yml index e767afc..d17139a 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -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 @@ -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