Add source generator for aggregate services (closes #13)#24
Merged
Conversation
- 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:TypedParameterforwarding for methods, throwing setters/void methods — mirroringResolvingInterceptorexactly), and[ModuleInitializer]that registers a factory with a newGeneratedAggregateServiceRegistry.At runtime
CreateInstanceconsults the registry first and falls back to Castle DynamicProxy when the generator couldn't see the interface statically (runtime-computedType, generic pass-through helpers, etc.). The generated path also fixes the >16-parameter generic method limitation (#11).AOT/trimming
IsAotCompatible(net7.0+); the closed-aggregate generated path is genuinely trim/AOT-clean.ref/outmethods are individually annotated[RequiresUnreferencedCode]/[RequiresDynamicCode]so warnings surface at the consumer's call site only when those scenarios are used.AGSVC001info diagnostic flags interfaces that fall back to the proxy.Scope / known limitations
ref/outparameter methods are JIT-only (not AOT-safe) and use the dynamic proxy fallback.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.