Skip to content

Add Decorate<TDecorated, TDecorator> support#325

Merged
kzu merged 9 commits into
mainfrom
cursor/add-decorate-marker-f70c
Jun 29, 2026
Merged

Add Decorate<TDecorated, TDecorator> support#325
kzu merged 9 commits into
mainfrom
cursor/add-decorate-marker-f70c

Conversation

@kzu

@kzu kzu commented Jun 28, 2026

Copy link
Copy Markdown
Member

Fixes #215

Summary

  • Add Decorate<TDecorated, TDecorator>() as a source-generator marker extension.
  • Add Decorate<TDecorated, TDecorator>(object? key) for decorating keyed service registrations by key.
  • Generate decoration partials that replace existing unkeyed/keyed descriptors in place while preserving lifetimes.
  • Allow decorator constructors to take the decorated service plus additional DI dependencies.
  • Add diagnostics for missing [Service], incompatible lifetimes, and constructors without exactly one decorated-service parameter.
  • Cover runtime decoration and generator diagnostics with tests.

Validation

  • dotnet test src/CodeAnalysis.Tests/CodeAnalysis.Tests.csproj -m:1
  • dotnet test src/DependencyInjection.Tests/DependencyInjection.Tests.csproj -m:1

Slack Thread

Open in Web Open in Cursor 

@CLAassistant

CLAassistant commented Jun 28, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@kzu

kzu commented Jun 28, 2026

Copy link
Copy Markdown
Member Author

55 passed 55 passed

🧪 Details on Ubuntu 24.04.4 LTS

from retest v1.1.0 on .NET 10.0.9 with 💜 by @devlooped

@kzu kzu marked this pull request as ready for review June 28, 2026 22:43
cursoragent and others added 9 commits June 28, 2026 20:30
Co-authored-by: Daniel Cazzulino <daniel@cazzulino.com>
Co-authored-by: Daniel Cazzulino <daniel@cazzulino.com>
Co-authored-by: Daniel Cazzulino <daniel@cazzulino.com>
Co-authored-by: Daniel Cazzulino <daniel@cazzulino.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Showcase the Decorate feature in the sample app with a stopwatch timer decorator on the echo command handler, and document usage in the readme including keyed decoration.
…eyed paths

- [Service] is no longer required on TDecorator for Decorate<TDecorated, TDecorator>.
  Lifetime compatibility validation is now only performed when the decorator
  actually declares [Service] (or keyed variant) attributes.

- Reduced duplication between keyed and non-keyed code:
  - Runtime helpers: unified DecorateDescriptors (now handles key=null for
    non-keyed) and GetDecoratedCore (single implementation of the
    Instance/Factory/ImplementationType materialization logic).
  - Generator: extracted BuildDecoratorArgs helper to eliminate duplicated
    constructor argument emission for CreateDecoratorN / CreateKeyedDecoratorN.

- Cleanups:
  - Removed DecoratorMustBeService (DDI006) diagnostic and the associated
    generator test (ErrorIfDecoratorIsNotService).
  - Removed legacy DecorateKeyedDescriptors forwarding method; generator now
    emits calls to the unified DecorateDescriptors overload.
  - Updated readme documentation to reflect that [Service] on the decorator
    is optional.
  - Added NoErrorIfDecoratorHasNoServiceAttribute (generator) and
    DecorateWorksWithDecoratorThatHasNoServiceAttribute (end-to-end) tests.
  - Added the README 'Decorating Services' example as
    DecorateServiceReadmeExample in GenerationTests.

- Existing behavior preserved when [Service] is present on the decorator
  (lifetime checks, suppression via IsDecoratorServiceAlias, etc.).
Add regression test verifying that constructor-injected IEnumerable<TDecorated>
receives properly decorated instances for each underlying registration after
Decorate<TDecorated, TDecorator>() is applied.
@kzu kzu force-pushed the cursor/add-decorate-marker-f70c branch from 7837506 to ec4ee62 Compare June 29, 2026 09:16
@kzu kzu merged commit 2e3a2c2 into main Jun 29, 2026
4 checks passed
@kzu kzu deleted the cursor/add-decorate-marker-f70c branch June 29, 2026 09:20
@kzu kzu changed the title Add source-generated Decorate marker Add Decorate<TDecorated, TDecorator> support Jun 29, 2026
@kzu kzu added the enhancement New feature or request label Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Decorate extension method

3 participants