Skip to content

Add source generator for aggregate services (closes #13)#24

Merged
tillig merged 12 commits into
developfrom
feature/issue-13-source-gen
Jun 21, 2026
Merged

Add source generator for aggregate services (closes #13)#24
tillig merged 12 commits into
developfrom
feature/issue-13-source-gen

Conversation

@tillig

@tillig tillig commented Jun 20, 2026

Copy link
Copy Markdown
Member

Implements build-time aggregate service generation (#13). A C# source generator emits concrete backing implementations for aggregate service interfaces, replacing the Castle DynamicProxy at runtime for the common case while keeping the proxy as a fallback. This makes aggregate services trimming/NativeAOT safe and significantly faster, with no usage changes — consumers update the package reference and recompile.

How it works

The generator ships inside the existing package under analyzers/dotnet/cs/, so there's nothing extra to reference. It runs in the consumer's compilation, scans for the existing registration/creation call sites (RegisterAggregateService<T>() / (typeof), AggregateServiceGenerator.CreateInstance<T> / (typeof)), and for each statically-visible interface emits:

  • a backing class implementing the interface (eager property resolution, TypedParameter forwarding for methods, throwing setters/void methods — mirroring ResolvingInterceptor exactly), and
  • a [ModuleInitializer] that registers a factory with a new GeneratedAggregateServiceRegistry.

At runtime CreateInstance consults the registry first and falls back to Castle DynamicProxy when the generator couldn't see the interface statically (runtime-computed Type, generic pass-through helpers, etc.). The generated path also fixes the >16-parameter generic method limitation (#11).

AOT/trimming

  • Runtime library is marked IsAotCompatible (net7.0+); the closed-aggregate generated path is genuinely trim/AOT-clean.
  • The Castle fallback, open-generic construction, and ref/out methods are individually annotated [RequiresUnreferencedCode]/[RequiresDynamicCode] so warnings surface at the consumer's call site only when those scenarios are used.
  • An AGSVC001 info diagnostic flags interfaces that fall back to the proxy.
  • Requires Autofac 9.3.0+ (first release with the matching AOT annotations).
  • A NativeAOT smoke-test app validates the generated path compiles warning-clean and runs correctly (verified end-to-end with a published native binary).

Scope / known limitations

  • Open generic aggregates and ref/out parameter methods are JIT-only (not AOT-safe) and use the dynamic proxy fallback.
  • Documentation (readthedocs) is intentionally deferred to a follow-up.

Tests

  • Autofac.Extras.AggregateService.Test — 57 runtime tests (behavior parity, fallback, parameter modifiers, constraints, lifetime scopes).
  • Autofac.Extras.AggregateService.SourceGenerator.Test — 23 generator snapshot + behavior tests (Verify-based).
  • bench/ — BenchmarkDotNet comparison (generated vs proxy: ~2× faster, ~half the allocations).
  • AotSmokeTest — NativeAOT publish + execution gate.

@codecov

codecov Bot commented Jun 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.29617% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.88%. Comparing base (8899832) to head (6d27583).

Files with missing lines Patch % Lines
...SourceGenerator/AggregateServiceSourceGenerator.cs 89.33% 3 Missing and 5 partials ⚠️
...AggregateService.SourceGenerator/EquatableArray.cs 80.76% 1 Missing and 4 partials ⚠️
...ggregateService.SourceGenerator/DiscoveryResult.cs 66.66% 0 Missing and 4 partials ⚠️
...s.AggregateService.SourceGenerator/LocationInfo.cs 76.92% 1 Missing and 2 partials ⚠️
...s.AggregateService.SourceGenerator/ModelBuilder.cs 98.47% 0 Missing and 2 partials ⚠️
...teService.SourceGenerator/AggregateServiceModel.cs 94.44% 0 Missing and 1 partial ⚠️
...Extras.AggregateService.SourceGenerator/Emitter.cs 99.54% 0 Missing and 1 partial ⚠️
...as.AggregateService.SourceGenerator/MemberModel.cs 94.44% 0 Missing and 1 partial ⚠️
...AggregateService.SourceGenerator/ParameterModel.cs 87.50% 0 Missing and 1 partial ⚠️
...egateService.SourceGenerator/TypeParameterModel.cs 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop      #24      +/-   ##
===========================================
+ Coverage    94.54%   95.88%   +1.33%     
===========================================
  Files            4       16      +12     
  Lines          110      680     +570     
  Branches        20      127     +107     
===========================================
+ Hits           104      652     +548     
- Misses           1        5       +4     
- Partials         5       23      +18     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

tillig added 4 commits June 20, 2026 11:42
- Add UTF-8 BOM to files missing it (CHARSET format errors)
- Rename ModelBuilder static fields to _camelCase (IDE1006)
- Add InternalsVisibleTo so the generator test project can reach
  the internal model types
- Add ModelEqualityTests + EmitterTests and extra generator behavior
  tests, raising generator coverage from ~81% to ~98%
…r code

- Add tests for unsupported member shapes (nested type, indexer,
  write-only property, event) and the notnull constraint
- Remove dead ref/out arms in BuildParameterModifier (ref/out methods
  bail out in TryBuildMethod before reaching it)
Generator coverage now ~99%.
- Resolve the source generator assembly path for packing via GetTargetPath
  on the project reference instead of a hard-coded bin path; fail the build
  with a clear message if the assembly is missing rather than packing nothing.
- Add a [GlobalSetup] guard to AggregateServiceBenchmark that verifies the
  'generated' and 'proxied' aggregates resolve to genuinely distinct
  implementations, so the comparison can't silently collapse.
@tillig tillig merged commit 6336137 into develop Jun 21, 2026
12 checks passed
@tillig tillig deleted the feature/issue-13-source-gen branch June 21, 2026 06:00
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