Analyze C# code quality patterns in Sitecore 10.x projects
Analyzes C# code for Sitecore 10.x anti-patterns, dependency injection issues, and static context usage.
/plugin marketplace add twofoldtech-dakota/claude-marketplace/plugin install twofoldtech-dakota-sitecore-classic-analyzer-plugins-sitecore-classic-analyzer@twofoldtech-dakota/claude-marketplaceAnalyze C# code patterns, dependency injection, and Sitecore-specific anti-patterns.
Search for direct service location instead of constructor injection:
// Anti-pattern to detect
ServiceLocator.ServiceProvider.GetService<IMyService>()
DependencyResolver.Current.GetService<IMyService>()
Verify proper DI registration via IServicesConfigurator:
// Good pattern
public class RegisterDependencies : IServicesConfigurator
{
public void Configure(IServiceCollection services)
{
services.AddTransient<IMyService, MyService>();
}
}
Check for:
Flag usage of static Sitecore context in async code:
// Dangerous in async - context may be null
Sitecore.Context.Item
Sitecore.Context.Database
Sitecore.Context.Site
Analyze controller implementations:
// Good pattern
public class NavigationController : Controller
{
private readonly INavigationService _service;
public NavigationController(INavigationService service)
{
_service = service;
}
public ActionResult Index()
{
var item = RenderingContext.Current.Rendering.Item;
// ...
}
}
Check for:
If Glass Mapper is detected, check for:
// Anti-pattern: Lazy loading in loop
foreach (var item in items)
{
var children = item.Children; // N+1 query
}
// Good: Eager load or use Content Search
Verify repository implementations:
Check for missing null checks on Sitecore item access:
// Risky - item could be null
var title = Sitecore.Context.Item["Title"];
// Better
var item = Sitecore.Context.Item;
if (item == null) return;
var title = item["Title"];
| Code | Severity | Issue | Detection |
|---|---|---|---|
| CQ-001 | Critical | Service Locator pattern | ServiceLocator. or DependencyResolver.Current |
| CQ-002 | Critical | Static Context in async | Sitecore.Context.* in async method |
| CQ-003 | Warning | Missing null checks | Item field access without null check |
| CQ-004 | Warning | Business logic in controller | Controller method >20 lines |
| CQ-005 | Warning | Raw database queries | Database.GetItem() without caching consideration |
| CQ-006 | Info | Missing interface | Service class without interface |
| CQ-007 | Info | Glass Mapper lazy loading | .Children or .Parent access in loops |
Grep: ServiceLocator\.ServiceProvider
Grep: DependencyResolver\.Current
Grep: Sitecore\.Context\. (in async methods)
Glob: **/Controllers/**/*.cs
For each controller:
- Check constructor for DI
- Count lines per action method
- Check for datasource null handling
Grep: IServicesConfigurator
Grep: services\.Add(Transient|Scoped|Singleton)
Grep: IGlassHtml
Grep: ISitecoreContext
Grep: \.Children
Grep: \.Parent
## Code Quality Analysis
### Summary
- **Controllers Analyzed**: 15
- **Services Analyzed**: 23
- **Issues Found**: 8
### Critical Issues
#### [CQ-001] Service Locator Pattern
**Location**: `src/Feature/Search/code/Services/SearchService.cs:45`
**Code**:
```csharp
var logger = ServiceLocator.ServiceProvider.GetService<ILogger>();
Fix: Inject ILogger via constructor
Location: src/Feature/Navigation/code/Controllers/NavigationController.cs:23
Issue: BuildNavigation method is 45 lines
Fix: Extract to INavigationService