# Goal: Extract interface from concrete class
# Step 1: Analyze class
serena_get_symbols_overview: "src/Services/OrderService.cs"
# Step 2: Create interface
Write: src/Interfaces/IOrderService.cs
Content: Interface with public methods
# Step 3: Update class to implement interface
serena_replace_content: "OrderService"
Pattern: "class OrderService"
Replacement: "class OrderService : IOrderService"
# Step 4: Find all usages
serena_find_referencing_symbols: "OrderService"
# Step 5: Update to use interface where appropriate
# Manual review: which references should use interface?
Scenario 2: Base Class Creation
# Goal: Extract common code into base class
# Step 1: Find similar classes
serena_find_symbol: "UserService"
serena_find_symbol: "OrderService"
# Step 2: Compare structures
serena_get_symbols_overview: "UserService"
serena_get_symbols_overview: "OrderService"
# Step 3: Create base class
Write: src/Services/BaseService.cs
# Step 4: Update derived classes
serena_replace_content: "UserService"
Pattern: "class UserService"
Replacement: "class UserService : BaseService"
Scenario 3: Namespace Reorganization
# Goal: Move types to new namespace
# Step 1: Find all types in namespace
serena_find_symbol: "MyApp.Services.*"
# Step 2: Update namespace declarations
serena_replace_content: "OrderService"
Pattern: "namespace MyApp.Services"
Replacement: "namespace MyApp.Application.Services"
# Step 3: Update using statements
# Find all files referencing old namespace
serena_find_referencing_symbols: "MyApp.Services"
Safety Guidelines
Always Do These Steps
Find references first
serena_find_referencing_symbols: "TargetSymbol"
# Review before modifying
Make backup or use git
Bash: git stash
# Or work on branch
Verify compilable after changes
Bash: dotnet build
Run tests
Bash: dotnet test
Avoid These Mistakes
Don't rename without checking references
May break external consumers
Could miss string-based references
Don't change public API without versioning consideration
Breaking changes require major version bump
Consider backward compatibility
Don't ignore compile errors
Serena helps but doesn't guarantee correctness
Always verify with compiler
Performance Considerations
Large Codebases
# For very large refactors:
1. **Batch operations**
- Group related changes
- Apply in single commit
2. **Incremental approach**
- Refactor one subsystem at a time
- Verify between steps
3. **Use branches**
- Feature branches for major refactors
- Easy rollback if issues
Complex Dependencies
# When dependencies are complex:
1. **Map dependency graph first**
serena_find_referencing_symbols: "TargetType"
# Multiple levels deep
2. **Identify breaking changes**
- Public API surface
- Cross-project dependencies
- External consumers
3. **Plan migration strategy**
- Deprecation periods
- Adapter patterns
- Feature flags
Integration with Other Skills
[skill:dotnet-serena-code-navigation] - Navigate before refactoring
[skill:dotnet-solid-principles] - Apply during refactoring