Roslyn analyzers and code fixers to enforce ServiceKit best practices in Unity projects. These analyzers help developers write better, more maintainable code when using ServiceKit's dependency injection framework.
ServiceKit.Analyzers provides compile-time validation of dependency injection patterns, ensuring:
- Proper interface-based injection for better testability and AOT compatibility
- Correct field visibility and modifiers for injected services
- Proper cancellation token usage in async injection chains
- Consistent coding patterns across your ServiceKit implementation
Severity: Warning
Category: ServiceKit.Usage
Enforces that members decorated with [InjectService] attribute should be interface types rather than concrete classes. This promotes loose coupling, better testability, and AOT-friendly code.
// ❌ Bad - Using concrete class
[InjectService] private MyService _service;
// ✅ Good - Using interface
[InjectService] private IMyService _service;The code fixer automatically suggests all interfaces implemented by the concrete type, allowing you to quickly change the member type to an appropriate interface.
Severity: Warning
Category: ServiceKit.Usage
Ensures that fields with [InjectService] attribute follow ServiceKit's requirements:
- Must be
private(to avoid Unity inspector exposure) - Must be instance fields (not
static) - Must be mutable (not
readonly) - Must not have
[SerializeField]attribute
// ❌ Bad - Public, static, readonly, or serialized
[InjectService] public IMyService Service;
[InjectService] static IMyService _service;
[InjectService] readonly IMyService _service;
[SerializeField][InjectService] private IMyService _service;
// ✅ Good - Private, instance, mutable field
[InjectService] private IMyService _service;Automatically corrects field modifiers to make them private, instance, mutable, and removes any [SerializeField] attributes.
Severity: Warning
Category: ServiceKit.Async
Ensures that async injection chains specify a destroy cancellation token to properly handle Unity object lifecycle. This prevents memory leaks and ensures clean shutdown when GameObjects are destroyed.
// ❌ Bad - Missing cancellation token
await _locator.InjectServicesAsync(gameObject)
.ExecuteAsync();
// ✅ Good - With cancellation token
await _locator.InjectServicesAsync(gameObject)
.WithCancellation(destroyCancellationToken)
.ExecuteAsync();
// ✅ Also good - Using convenience method
await _locator.InjectServicesAsync(gameObject)
.ExecuteWithCancellationAsync(destroyCancellationToken);Offers two fixes:
- Add
.WithCancellation(destroyCancellationToken)to the chain - Replace
.ExecuteAsync()with.ExecuteWithCancellationAsync(destroyCancellationToken)
Severity: Info
Category: ServiceKit.Async
Suggests using the convenience method ExecuteWithCancellationAsync instead of the longer .WithCancellation().ExecuteAsync() pattern for cleaner, more consistent code.
// ⚠️ Works but verbose
await _locator.InjectServicesAsync(gameObject)
.WithCancellation(destroyCancellationToken)
.ExecuteAsync();
// ✅ Preferred - Cleaner syntax
await _locator.InjectServicesAsync(gameObject)
.ExecuteWithCancellationAsync(destroyCancellationToken);Automatically refactors the code to use the convenience method, removing the .WithCancellation() call and replacing .ExecuteAsync() with .ExecuteWithCancellationAsync().
For local development and testing:
dotnet build
Output: bin/Debug/netstandard2.0/ServiceKit.Analyzers.dll
For production release:
dotnet build -c Release
Output: bin/Release/netstandard2.0/ServiceKit.Analyzers.dll
To copy the analyzer DLL to a specific output directory:
dotnet build -c Release -o <output-directory>
The analyzer DLL can then be referenced by projects that need ServiceKit analysis.
These analyzers are designed to work seamlessly with ServiceKit's dependency injection system in Unity. They help enforce best practices such as:
- Interface-based injection: Promotes testability and allows for easy mocking/substitution
- Proper field configuration: Ensures injected fields work correctly with ServiceKit's injection mechanism
- Lifecycle management: Ensures proper cancellation token usage to prevent memory leaks
- Code consistency: Promotes use of standardized patterns across your codebase
- Target Framework: .NET Standard 2.0 for broad compatibility
- Language Version: Latest C# with nullable reference types enabled
- Dependencies: Microsoft.CodeAnalysis.CSharp 4.10.0
- Package Type: Development dependency (no runtime impact)
