Phymnary.SugarPot.DependencyInjection.Roslyn
1.0.0
dotnet add package Phymnary.SugarPot.DependencyInjection.Roslyn --version 1.0.0
NuGet\Install-Package Phymnary.SugarPot.DependencyInjection.Roslyn -Version 1.0.0
<PackageReference Include="Phymnary.SugarPot.DependencyInjection.Roslyn" Version="1.0.0" />
<PackageVersion Include="Phymnary.SugarPot.DependencyInjection.Roslyn" Version="1.0.0" />
<PackageReference Include="Phymnary.SugarPot.DependencyInjection.Roslyn" />
paket add Phymnary.SugarPot.DependencyInjection.Roslyn --version 1.0.0
#r "nuget: Phymnary.SugarPot.DependencyInjection.Roslyn, 1.0.0"
#:package Phymnary.SugarPot.DependencyInjection.Roslyn@1.0.0
#addin nuget:?package=Phymnary.SugarPot.DependencyInjection.Roslyn&version=1.0.0
#tool nuget:?package=Phymnary.SugarPot.DependencyInjection.Roslyn&version=1.0.0
Phymnary.SugarPot.DependencyInjection.Roslyn
Roslyn analyzer + incremental source generator for convention-based dependency registration.
This project works with Phymnary.SugarPot.DependencyInjection attributes to generate IServiceCollection registration code at compile time.
What this project provides
- Incremental generator:
AutoRegisterGenerator - Analyzer:
AutoRegisterAnalyzer - Diagnostic descriptors for invalid service decorations
Generation model
The generator scans for:
[Service(Lifetime.X)]on implementation classes[Auto]on module classes (must bepartial)
For each [Auto] partial class, it generates registration code that adds all discovered [Service] types to IServiceCollection using the configured lifetime.
Service type resolution rules
For a class decorated with [Service], generated registration chooses service type in this order:
- If
IsSelf = true→ register implementation as itself - Otherwise first implemented interface of the class
- Otherwise first interface from base type
- Otherwise implementation type itself
Lifetime is mapped to:
Lifetime.Singleton→AddSingleton<TService, TImpl>Lifetime.Scoped→AddScoped<TService, TImpl>Lifetime.Transient→AddTransient<TService, TImpl>
Analyzer diagnostics
Current diagnostic:
SPDI0001(Error): class with[Service]cannot be abstract or generic.
Conventions and requirements
[Auto]target class must bepartial(required for source generation).[Service]classes must be concrete, non-generic classes.- Generator relies on attributes from
Phymnary.SugarPot.DependencyInjectionnamespace.
Example
using Phymnary.SugarPot.DependencyInjection;
[Service(Lifetime.Scoped)]
public sealed class UserService : IUserService
{
}
[Auto]
public partial class ApplicationModule
{
}
Generated code will add a scoped registration similar to:
services.AddScoped<IUserService, UserService>();
Project structure
Components/AutoRegister/AutoRegisterGenerator*.cs- generator pipeline + syntax emitComponents/AutoRegister/AutoRegisterAnalyzer.cs- semantic validationDiagnostics/DiagnosticDescriptors.cs- analyzer metadataConstants/GeneratorConstant.cs- shared constants
Dependencies
Microsoft.CodeAnalysis.AnalyzersMicrosoft.CodeAnalysis.CSharp.Workspaces
Local development notes
- Build this project to validate generator/analyzer behavior.
- Validate generated output in consumer projects using
[Auto]and[Service]. - Keep analyzer metadata in sync with
AnalyzerReleases.Shipped.mdandAnalyzerReleases.Unshipped.md.
Target framework
- .NET Standard 2.0 (analyzer/generator compatibility target)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|