Skip to content

Refactor CompareCommand dependency injection and investigate namespace mapping configuration #28

@jbrinkman

Description

@jbrinkman

Summary

This issue tracks the refactoring of the CompareCommand class to use proper dependency injection patterns and investigation of namespace mapping configuration issues.

Background

The original issue was that namespace mappings defined in JSON configuration files were not being applied during API comparison. Investigation revealed that the root cause was improper dependency injection patterns in the CompareCommand class.

Work Completed

✅ Dependency Injection Refactoring

  1. Refactored CompareCommand Constructor

    • Changed from 2 parameters to 4 parameters
    • Added IExitCodeManager exitCodeManager parameter
    • Added IGlobalExceptionHandler exceptionHandler parameter
    • Eliminated service locator anti-pattern by removing repeated GetRequiredService calls
  2. Updated TypeRegistrar

    • Modified TypeRegistrar.cs to provide the new constructor parameters
    • Ensured proper dependency resolution through the DI container
  3. Created Comprehensive DI Validation Tests

    • Added DependencyInjectionTests.cs with 13 test methods
    • Validates all service registrations can be resolved correctly
    • Ensures scoped services work properly
    • Tests individual service resolution for all dependencies
  4. Fixed All Unit Tests

    • Updated CompareCommandTests.cs to use proper constructor parameters
    • Fixed CompareCommandErrorTests.cs with existing mock objects
    • Updated CompareCommandFilteringTests.cs with helper method for dependency resolution
    • All 363 tests now pass successfully

✅ Application Architecture Improvements

  • Better Performance: Constructor injection is more efficient than repeated service resolution
  • Improved Testability: Dependencies are now explicit and easier to mock
  • Enhanced Maintainability: Clear dependency declarations make the code more maintainable
  • Proper DI Patterns: Eliminated service locator anti-pattern

Outstanding Work

🔍 Namespace Mapping Investigation

While the DI architecture is now properly implemented, the original namespace mapping configuration issue still needs investigation:

  • JSON configuration files with namespace mappings (e.g., "StackExchange.Redis" → "Valkey.Glide") are not being applied
  • Configuration loading works correctly, but the mappings may not be passed through to the comparison logic
  • Need to trace the configuration flow from JSON loading through to the API comparison process

Technical Details

Constructor Changes

// Before (2 parameters)
public CompareCommand(IServiceProvider serviceProvider, ILogger<CompareCommand> logger)

// After (4 parameters) 
public CompareCommand(
    IServiceProvider serviceProvider,
    ILogger<CompareCommand> logger,
    IExitCodeManager exitCodeManager,
    IGlobalExceptionHandler exceptionHandler)

Test Results

  • Total tests: 363
  • Passed: 363
  • Failed: 0
  • Skipped: 0

Files Modified

  • src/DotNetApiDiff/Commands/CompareCommand.cs
  • src/DotNetApiDiff/Commands/TypeRegistrar.cs
  • src/DotNetApiDiff/Program.cs (made ConfigureServices public for testing)
  • tests/DotNetApiDiff.Tests/DependencyInjectionTests.cs (new file)
  • tests/DotNetApiDiff.Tests/Commands/CompareCommandTests.cs
  • tests/DotNetApiDiff.Tests/Commands/CompareCommandErrorTests.cs
  • tests/DotNetApiDiff.Tests/Commands/CompareCommandFilteringTests.cs

Next Steps

  1. Investigate namespace mapping flow - Trace how namespace mappings from JSON config are passed to the API comparison logic
  2. Verify configuration application - Ensure loaded configuration is properly applied to services that need it
  3. Test with real assemblies - Validate namespace mapping works end-to-end with actual assemblies

Build Status

✅ All builds passing
✅ All tests passing
✅ Application runs successfully

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions