Skip to content
This repository was archived by the owner on Mar 15, 2026. It is now read-only.

kodroi/Seams.Analyzers

Repository files navigation

Seams.Analyzers

CI NuGet

A Roslyn analyzer library that detects code patterns blocking "seams" in legacy code, based on Michael Feathers' Working Effectively with Legacy Code.

Use Cases

1. AI-Assisted Development

When SEAM rules are configured as warnings or errors, AI coding assistants (GitHub Copilot, Claude, Cursor, Windsurf, etc.) read build failures and adapt their code generation to avoid untestable patterns.

How It Works

Developer Request → AI Generation → Analyzer Feedback → AI Iteration → Testable Code
  1. AI generates code with a hard dependency (e.g., new EmailService())
  2. Build fails with SEAM001: Direct instantiation of concrete type 'EmailService'
  3. AI reads this feedback and regenerates using dependency injection
  4. Resulting code is testable from the start
Traditional Workflow With Analyzers
Write code → Analyze → Refactor Configure rules → AI generates → Testable code
Fix problems after creation Prevent problems during generation

Setup

<!-- Directory.Build.props - enforce as errors -->
<PropertyGroup>
  <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

2. Legacy Code Refactoring

When working with legacy code, this analyzer helps you identify where to create seams so you can test a class. By detecting the 18 anti-patterns that block testability, you can systematically refactor code to introduce dependency injection points, extract interfaces, and break hard dependencies—making previously untestable code testable.

What are Seams?

A seam is a place where you can alter behavior in your program without editing in that place. Seams are essential for testing because they allow you to substitute dependencies, mock behaviors, and isolate code under test.

This analyzer identifies 18 anti-patterns (SEAM001-SEAM018) across 5 categories that hinder testability by blocking seams.

Installation

dotnet add package Seams.Analyzers

Or via Package Manager:

Install-Package Seams.Analyzers

Diagnostic Rules

Default Severity Levels

Not all rules are enabled by default. This follows the Principle of Least Astonishment (POLA) - rules that may produce many warnings in typical codebases are disabled by default to avoid overwhelming users on first installation.

Severity Meaning Rules
Warning Enabled, likely issues SEAM003, SEAM012, SEAM013, SEAM014, SEAM016
Info Enabled, suggestions SEAM001, SEAM005, SEAM006, SEAM007, SEAM015, SEAM017, SEAM018
Disabled Opt-in only SEAM002, SEAM004, SEAM008, SEAM009, SEAM010, SEAM011

To enable disabled rules, add to your .editorconfig:

# Enable specific disabled rules
dotnet_diagnostic.SEAM002.severity = suggestion
dotnet_diagnostic.SEAM004.severity = suggestion
dotnet_diagnostic.SEAM008.severity = suggestion
dotnet_diagnostic.SEAM009.severity = suggestion
dotnet_diagnostic.SEAM010.severity = suggestion
dotnet_diagnostic.SEAM011.severity = suggestion

Direct Dependencies

Rule Description Default
SEAM001 Direct instantiation of concrete types Info
SEAM002 Concrete type in constructor parameter Disabled
SEAM003 Service locator pattern usage Warning

Static Dependencies

Rule Description Default
SEAM004 Static method calls (File, Console, etc.) Disabled
SEAM005 DateTime.Now/UtcNow usage Info
SEAM006 Guid.NewGuid() usage Info
SEAM007 Environment.GetEnvironmentVariable Info
SEAM008 Static property access (ConfigurationManager) Disabled

Inheritance Blockers

Rule Description Default
SEAM009 Sealed class prevents inheritance Disabled
SEAM010 Non-virtual method prevents override Disabled
SEAM011 Complex private method (50+ lines) Disabled

Global State

Rule Description Default
SEAM012 Singleton pattern implementation Warning
SEAM013 Static mutable field Warning
SEAM014 Ambient context (HttpContext.Current, etc.) Warning

Infrastructure Dependencies

Rule Description Default
SEAM015 Direct file system access Info
SEAM016 Direct HttpClient creation Warning
SEAM017 Direct database connection creation Info
SEAM018 Direct Process.Start usage Info

Configuration

Suppress diagnostics or exclude specific types via .editorconfig:

# Disable a rule entirely
dotnet_diagnostic.SEAM001.severity = none

# Exclude specific types from SEAM001
dotnet_code_quality.SEAM001.excluded_types = T:MyNamespace.AllowedFactory

# Exclude specific methods from SEAM004
dotnet_code_quality.SEAM004.excluded_methods = M:System.Console.WriteLine

# Exclude namespaces from SEAM009
dotnet_code_quality.SEAM009.excluded_namespaces = MyNamespace.Internal

# Set complexity threshold for SEAM011 (default: 50)
dotnet_code_quality.SEAM011.complexity_threshold = 100

Example

public class OrderService
{
    // SEAM001: Direct instantiation creates hard dependency
    private readonly EmailService _emailService = new EmailService();

    // Better: Inject the dependency
    private readonly IEmailService _emailService;

    public OrderService(IEmailService emailService)
    {
        _emailService = emailService;
    }
}

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

Acknowledgments

This project is inspired by Michael Feathers' book Working Effectively with Legacy Code. The concept of "seams" as places where you can alter program behavior without editing in that place comes directly from his work. Thank you to Michael Feathers for providing such valuable insights into making legacy code testable.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Analyze .NET projects for seams to make a class unit testable

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages