Skip to content

Service Scopes - #40

Merged
devtronic merged 12 commits into
mainfrom
feature/scoped-services
Nov 12, 2026
Merged

devtronic merged 12 commits into
mainfrom
feature/scoped-services

Conversation

@devtronic

Copy link
Copy Markdown
Member

Breaking

  • The package catalyst_builder is now a pure code generator and must be added as a dev dependency.

  • The runtime (annotations, interfaces and the ServiceContainer) moved to the new package catalyst_builder_runtime, which is added as a regular dependency:

    dependencies:
      catalyst_builder_runtime: ^7.0.0
    dev_dependencies:
      build_runner: ^2.4.0
      catalyst_builder: ^7.0.0
  • package:catalyst_builder/catalyst_builder.dart no longer exports the ServiceContainer;
    import it from package:catalyst_builder_runtime/catalyst_builder_runtime.dart.

  • The generated service container plugin now references catalyst_builder_runtime instead of
    catalyst_builder_contracts. Regenerate with dart run build_runner build.

  • The generated plugin now implements provideKnownServices() without a container
    parameter and emits (c) => MyService(...) factory lambdas that receive the resolving
    container. Generated files must be regenerated with dart run build_runner build.

Features

  • The service graph is now validated at build time. Circular dependencies and captive
    dependencies (singleton depending on scoped) abort the build as errors; unknown
    required dependency types and duplicate exposeAs are reported as warnings. The new
    validation builder option (strict | warn | off, default warn) controls this.
    See the README for configuration.
  • @Service(lifetime: ServiceLifetime.scoped) is passed through to the generated plugin.
    @Preload on a scoped service emits a build warning and is ignored.

Fixes

  • The preflight builder now falls back to ServiceLifetime.singleton (the annotation
    default) instead of transient when the lifetime of a @Service annotation cannot
    be read.
  • The preflight builder now awaits writing the preflight JSON file.
  • Required constructor parameters with a nullable type are now resolved correctly.
    The generator emits an explicit non-nullable type argument
    (c.tryResolveOrGetParameter<Logger>('logger') instead of relying on inference of
    the nullable type, which never matched the registered service). Parameters whose
    type cannot be referenced by name still fall back to inference, now with a build
    warning. The preflight data gained a TypeRef for each resolvable parameter type.

Assisted by Claude Code

@devtronic
devtronic force-pushed the feature/scoped-services branch 3 times, most recently from 4ae2fc3 to 3f9458e Compare November 12, 2026 07:44
- Renamed catalyst_builder_contracts to catalyst_builder_runtime (v7.0.0)
- Moved ServiceContainer from catalyst_builder into catalyst_builder_runtime
- catalyst_builder (v7.0.0) is now a pure code generator for dev_dependencies
- Updated builder, example, third_party_dependency, CI, recipes and docs
- Runtime and builder now share the same version number
Behavior is unchanged: the generated code is byte-identical.
The preflight builder now falls back to `ServiceLifetime.singleton` as the annotation default.
The generator emitted resolveOrGetParameter<Logger?> for required parameters whose type was nullable. The nullable Type never matched the registered service and null passed the 'is Logger?' parameter check, so resolution threw although the service was registered.
Constructor parameters now carry a TypeRef extracted by the preflight builder; the generator emits an explicit non-nullable type argument:
- tryResolveOrGetParameter<Logger>('logger') for required nullable and
- resolveOrGetParameter<Logger>(...) otherwise.
Unresolvable types keep inference and log a build warning.
- ServiceDescriptor factories receive the resolving container:
  - ServiceDescriptor(const Service(), (c) => MyService(c.resolve<Foo>())) and
  - produce(container) invokes them with it.
- ServiceContainerPlugin.provideKnownServices() no longer takes the container parameter.
- enhance() creates a child container linked via _parent instead of
  - copying descriptor maps. Lookups walk child -> parent; singletons are
  - built and cached by their owner container, transients by the
  - resolving container. Manual tagged registrations are inherited,
  - plugins are never applied to children.
- The generator emits parameterless provideKnownServices() and  (c) => ... factory lambdas.
- ServiceLifetime.scoped: one instance per scope, resolved by the
  innermost scope; resolving from the root throws ScopeRequiredException.
- createScope() on AbstractServiceContainer; enhance() was removed. New ServiceScope interface with
  parent/isDisposed/dispose.
- Disposable interface: instances created in a scope are disposed in
  - reverse creation order on dispose(); root dispose() disposes the root
  - singletons. Dispose is idempotent, collects errors and rethrows the
  - first one. Using a disposed scope throws ScopeDisposedException.
- Circular dependency detection via a shared resolving stack on the
  - root; throws CircularDependencyException with the offending path.
- Preflight warns when @preload is used on a scoped service.
- The example demonstrates two scopes with asserts.
The plugin builder validates all collected services before generating
code:
- circular dependencies (error, with the path)
- captive dependencies: singleton -> scoped (error)
- required parameters of an unknown type (warning; the type may come
  from parameters, register() or another plugin)
- duplicate exposeAs (warning, the last registration wins)
The new 'validation' builder option (strict | warn | off, default: warn) controls whether warnings abort the build, are logged, or validation is skipped. Documented in the README and CHANGELOG.
Complete 6.x -> 7.x migration guide in packages/catalyst_builder/UPGRADE.md:
TL;DR checklist, dependency and import migration, regeneration, detailed breaking changes with before/after code, new features and troubleshooting.
All 'After' snippets were compile checked against the final API.
Links added to the root README, both package READMEs and the changelogs.
@devtronic
devtronic force-pushed the feature/scoped-services branch from 055d950 to 5f38d14 Compare November 12, 2026 07:59
@devtronic
devtronic merged commit 6aaefe4 into main Nov 12, 2026
2 checks passed
@devtronic
devtronic deleted the feature/scoped-services branch November 12, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant