Template Types
Intent Architect supports several template registration patterns:FilePerModel
Generates one file per metadata model instance (e.g., one class per entity)
SingleFileListModel
Generates one file for all model instances (e.g., a registration file)
FilePerModelNoMetadata
Generates files without specific metadata models
SingleFileNoModel
Generates a single file without metadata dependencies
Creating a Template
Step 1: Define the Template Class
Create a template that inherits fromCSharpTemplateBase:
CommandHandlerTemplatePartial.cs
Step 2: Configure the Template Output
Use theCSharpFile builder to construct the output:
Configure Method
Step 3: Create the Template Registration
CommandHandlerTemplateRegistration.cs
Using the CSharpFile Builder
TheCSharpFile builder provides a fluent API for constructing C# files:
Adding Usings
Creating Classes
Adding Properties
Adding Methods
Adding Constructors
Creating Interfaces
Template Decorators
Templates can be extended by decorators. Define a base decorator class:CommandHandlerDecorator.cs
Managing Dependencies
Adding NuGet Packages
Adding Template Dependencies
Type Resolution
Template Metadata
Add metadata to track elements for other templates and decorators:Advanced Features
Conditional Output
Multiple Files from One Template
ImplementITemplateBeforeExecutionHook:
File Merging with RoslynWeaver
Configure merge behavior:Helper Methods and Extensions
Create extension methods for common operations:TemplateExtensions.cs
Testing Templates
Create unit tests for template output:CommandHandlerTemplateTests.cs
Best Practices
Use the CSharpFile builder
Use the CSharpFile builder
Always use
CSharpFile builder instead of string concatenation. It provides:- Automatic using statement management
- Proper indentation
- Type resolution
- Metadata tracking
Keep templates focused
Keep templates focused
Each template should generate one type of file. Use multiple templates for different file types.
Leverage metadata
Leverage metadata
Use metadata to communicate between templates and decorators:
Handle edge cases
Handle edge cases
Check for null models, empty collections, and optional features:
Make templates configurable
Make templates configurable
Use template settings and module settings to allow customization:
Common Patterns
Repository Pattern Template
Service Implementation Template
Next Steps
Decorators
Learn how to extend templates with decorators
Factory Extensions
Hook into the Software Factory lifecycle
Testing
Write comprehensive tests for templates
Creating Modules
Package templates into distributable modules
Real-World Examples
Explore these templates in the source code:- CommandHandlerTemplate -
Modules/Intent.Modules.Application.MediatR/Templates/CommandHandler/ - DtoModelTemplate -
Modules/Intent.Modules.Application.Dtos/Templates/DtoModel/ - RepositoryTemplate -
Modules/Intent.Modules.EntityFrameworkCore/Templates/Repository/
