Back to Dotnet Starter Kit

TemplateEngine Refactoring

src/Tools/CLI/Scaffolding/REFACTORING.md

1.1.04.0 KB
Original Source

TemplateEngine Refactoring

This document outlines the refactoring of the TemplateEngine.cs god class into focused, single-responsibility components.

Overview

The original TemplateEngine.cs was a 1645-line god class that handled everything from template loading to rendering and validation. This refactoring splits it into focused services while maintaining backward compatibility.

New Architecture

1. ITemplateLoader / TemplateLoader

Responsibility: Load templates from embedded resources and static sources

  • GetFrameworkVersion() - Gets the current framework version
  • GetStaticTemplate(name) - Gets static template content by name
  • TemplateExists(name) - Checks if a template exists

2. ITemplateParser / TemplateParser

Responsibility: Parse template syntax and normalize project names

  • ExtractVariables(template) - Extracts interpolation variables
  • IsValidTemplate(template) - Validates template syntax
  • NormalizeProjectName(name, context) - Normalizes names for different contexts (Docker, database, etc.)

3. ITemplateRenderer / TemplateRenderer

Responsibility: Render templates with variable substitution

  • All the Render*() methods that generate specific templates
  • Handles the actual template interpolation logic
  • Uses other services for loading, parsing, and validation

4. ITemplateValidator / TemplateValidator

Responsibility: Validate template structure and project configuration

  • ValidateProjectOptions() - Validates input parameters
  • ValidateTemplateAvailability() - Ensures required templates exist
  • ValidateGeneratedContent() - Validates output quality
  • ValidateProjectStructure() - Checks for logical issues

5. ITemplateCache / TemplateCache

Responsibility: Cache frequently used templates

  • In-memory caching with statistics
  • Thread-safe operations
  • Cache hit/miss metrics

6. TemplateServices

Responsibility: Dependency injection container

  • Factory pattern for creating services
  • Manages service lifetimes
  • Provides easy access to all services

Backward Compatibility

The refactored TemplateEngine class maintains the exact same public API as before. All existing code calling TemplateEngine.GenerateXxx() methods will continue to work without changes.

Benefits

  1. Single Responsibility Principle - Each class has one clear purpose
  2. Testability - Smaller, focused classes are easier to unit test
  3. Maintainability - Changes are isolated to specific areas
  4. Extensibility - New template types or sources are easier to add
  5. Performance - Template caching reduces repeated work
  6. Validation - Built-in validation catches issues early

Files Changed

  • TemplateEngine.cs - Refactored to delegate to services (maintains API)
  • ITemplateLoader.cs + TemplateLoader.cs - Template loading
  • ITemplateParser.cs + TemplateParser.cs - Template parsing
  • ITemplateRenderer.cs + TemplateRenderer.cs - Template rendering
  • ITemplateValidator.cs + TemplateValidator.cs - Validation logic
  • ITemplateCache.cs + TemplateCache.cs - Caching layer
  • TemplateServices.cs - DI container
  • TemplateEngineTests.cs - Basic validation tests
  • FSH.CLI.csproj - Added Microsoft.Extensions.DependencyInjection

Testing

Run TemplateEngineTests.RunValidationTests() to verify the refactoring works correctly.

Migration Notes

  • No breaking changes - existing code continues to work
  • The original TemplateEngine.cs is backed up as TemplateEngine.cs.backup
  • All template generation logic has been moved but preserved
  • Validation now provides better error messages and warnings

Future Enhancements

  1. Template Hot Reloading - Watch template files and reload automatically
  2. Custom Template Sources - Load from databases, HTTP, etc.
  3. Template Composition - Combine multiple templates
  4. Async Operations - For I/O heavy template operations
  5. Template Versioning - Support multiple template versions
  6. Plugin Architecture - Allow external template providers