This document provides comprehensive guidance for developers working on the Linq2GraphQL.Client project, particularly when modifying T4 templates and the code generation system.
- Prerequisites
- Project Structure
- T4 Template Development
- Code Generation Workflow
- Troubleshooting
- Best Practices
- Visual Studio 2022 (recommended) or Visual Studio 2019/2022
- .NET 8.0 SDK or later
- T4 Template Support - Ensure the "Text Template Transformation" workload is installed in Visual Studio
src/
├── Linq2GraphQL.Generator/ # Main code generation project
│ ├── Templates/ # T4 template files
│ │ ├── Client/ # Client generation templates
│ │ ├── Class/ # Class generation templates
│ │ ├── Interface/ # Interface generation templates
│ │ ├── Methods/ # Method generation templates
│ │ └── Enum/ # Enum generation templates
│ ├── GraphQLSchema/ # Schema parsing and processing
│ └── ClientGenerator.cs # Main generation orchestration
└── Linq2GraphQL.Client/ # Core client library
This project uses T4 (Text Template Transformation Toolkit) for code generation. T4 templates are .tt files that generate C# source code based on GraphQL schema information.
.tt- Source T4 template files (human-editable).tt.cs- Partial class definitions for template variables and helper methods.cs- Preprocessed T4 templates (auto-generated, contains the actualTransformText()method)
When modifying .tt files:
- Edit the
.ttfile with your changes - Manually regenerate the
.csfile using Visual Studio's custom tool - Build the project to ensure compilation
- Test the generation by running the client generator
.tt file, you MUST manually regenerate the corresponding .cs file.
In Visual Studio 2022:
- Right-click on the
.ttfile in Solution Explorer - Select "Run Custom Tool"
- This will regenerate the
.csfile with your changes - Verify the
.csfile contains your updated template logic
Alternative method:
- Right-click on the
.ttfile - Select "Properties"
- Set "Custom Tool" to
TextTemplatingFilePreprocessor - Set "Custom Tool Namespace" to your desired namespace
- Save the file to trigger regeneration
Each T4 template requires:
.ttfile - Contains the template logic and output format.tt.csfile - Provides the partial class with constructor parameters and helper methods.csfile - Auto-generated preprocessed template (regenerated from.tt)
<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#= variableName #> <!-- Output variable value -->
<# if (condition) { #> <!-- Conditional blocks -->
// C# code here
<# } #>
<# foreach (var item in collection) { #> <!-- Loops -->
// Process each item
<# } #>
Define helper methods in the .tt.cs file:
public partial class TemplateName
{
private readonly string variableName;
public TemplateName(string variableName)
{
this.variableName = variableName;
}
private string HelperMethod()
{
return "Helper logic here";
}
}Edit .tt file → Run Custom Tool → Build Project → Test Generation → Repeat
After modifying templates:
- Build the project to ensure no compilation errors
- Run the client generator to test template output
- Verify generated code matches your expectations
- Test the generated client in a sample application
# Build the generator project
dotnet build src/Linq2GraphQL.Generator
# Generate a client
dotnet run --project src/Linq2GraphQL.Generator -- <endpoint> [options]Problem: Changes to .tt files not reflected in generated output.
Solution:
- Ensure you've run the "Run Custom Tool" on the
.ttfile - Check that the
.csfile was updated with your changes - Clean and rebuild the project
- Verify the T4 preprocessor is working in Visual Studio
Problem: Compilation error "does not contain a definition for 'TransformText'".
Solution:
- The
.csfile is missing or outdated - Run "Run Custom Tool" on the corresponding
.ttfile - Ensure the
.tt.csfile exists and has the correct partial class definition
Problem: Template variables like namespaceName or name are undefined.
Solution:
- Check the
.tt.csfile has the correct constructor parameters - Verify the partial class has
readonlyfields for all template variables - Ensure the
ClientGenerator.cspasses the correct parameters when instantiating templates
- Check the
.csfile content - It should contain your template logic in theTransformText()method - Verify template compilation - Build errors often indicate template syntax issues
- Use Visual Studio's T4 debugging - Set breakpoints in the generated
.csfiles - Check build output - Look for T4-related error messages
- Keep templates focused - Each template should handle one specific aspect of code generation
- Use helper methods - Move complex logic to the
.tt.csfile - Maintain readability - Use clear variable names and consistent formatting
- Handle edge cases - Always check for null values and empty collections
- Separate concerns - Keep template logic separate from business logic
- Use partial classes - Leverage C# partial classes for template organization
- Consistent naming - Follow the project's naming conventions
- Documentation - Include XML comments in generated code
- Test with various schemas - Ensure templates work with different GraphQL schemas
- Validate generated code - Check that generated code compiles and works correctly
- Regression testing - Ensure changes don't break existing functionality
- Integration testing - Test the complete generation pipeline
- Commit
.ttand.tt.csfiles - These are source files - Ignore generated
.csfiles - Add**/*.csto.gitignorefor auto-generated files - Document template changes - Include clear commit messages for template modifications
- Review generated output - Verify that template changes produce the expected results
- Check existing templates - Review similar templates for examples
- T4 documentation - Microsoft's T4 documentation provides comprehensive guidance
- Project issues - Search existing GitHub issues for similar problems
- Community support - Reach out to the project maintainers or community
Note: T4 template development requires careful attention to the regeneration workflow. Always remember to run the custom tool after modifying .tt files to ensure your changes are applied to the generated code.