Skip to main content

Overview

IntentTemplateBase is the foundational base class for all templates in Intent Architect modules. It provides essential functionality for template lifecycle management, dependency tracking, type resolution, and file output configuration.

Class Hierarchy

T4TemplateBase
└── IntentTemplateBase
    └── IntentTemplateBase<TModel>
        └── IntentTemplateBase<TModel, TDecorator>

IntentTemplateBase

The base class for all templates without a specific model binding.

Constructor

templateId
string
required
Unique identifier for this template. Must be unique across the application.
outputTarget
IOutputTarget
required
The output target defining where this template’s output will be written.

Properties

Id
string
Unique identifier for this template instance.
CanRun
bool
Indicates whether the template should generate output. Default is true.
IsDiscoverable
bool
Indicates whether the template can be discovered by other templates. Default is true.
ExecutionContext
ISoftwareFactoryExecutionContext
Software Factory execution context providing access to application-wide services.
OutputTarget
IOutputTarget
The output target determining where this template’s output will be written.
FileMetadata
IFileMetadata
Metadata of the template, including file path and custom metadata.

Core Methods

RunTemplate

public virtual string RunTemplate()
Executes the template and returns the generated output as a string.
Returns
string
The generated template output with normalized line endings.

GetTemplateFileConfig

public abstract ITemplateFileConfig GetTemplateFileConfig()
Defines the file configuration including file name, location, and overwrite behavior.
Returns
ITemplateFileConfig
Configuration for the template’s output file.

Lifecycle Hooks

Templates support several lifecycle hooks for customization:

OnCreated

public virtual void OnCreated()
Called after the template has been created and registered.

OnConfigured

public virtual void OnConfigured()
Called after all templates have been configured.

AfterTemplateRegistration

public virtual void AfterTemplateRegistration()
Executed after all templates have been registered.

BeforeTemplateExecution

public virtual void BeforeTemplateExecution()
Executed before the template’s RunTemplate method runs.

Template Discovery

GetTemplate

public TTemplate? GetTemplate<TTemplate>(string templateId, TemplateDiscoveryOptions? options = null)
    where TTemplate : class
Retrieves an instance of another template by its ID.
templateId
string
required
The unique template identifier or role name.
options
TemplateDiscoveryOptions
Options controlling template discovery behavior (e.g., whether to throw if not found).
Returns
TTemplate?
The template instance if found, otherwise null.
Overloads:
// Get template bound to a specific model
GetTemplate<TTemplate>(string templateId, IMetadataModel model, TemplateDiscoveryOptions? options = null)

// Get template bound to a model by ID
GetTemplate<TTemplate>(string templateId, string modelId, TemplateDiscoveryOptions? options = null)

// Get template by dependency
GetTemplate<TTemplate>(ITemplateDependency dependency, TemplateDiscoveryOptions? options = null)

TryGetTemplate

public bool TryGetTemplate<TTemplate>(string templateId, out TTemplate? template)
    where TTemplate : class
Attempts to retrieve a template instance without throwing exceptions.
Returns
bool
true if the template was found, otherwise false.

Type Resolution

See the Type Resolution documentation for detailed information.

GetTypeName

public virtual string GetTypeName(ITypeReference typeReference, string? collectionFormat = null)
Resolves the type name for a given type reference.
typeReference
ITypeReference
required
The type reference to resolve.
collectionFormat
string
Collection format to apply if the type is a collection (e.g., “List<T>”, “T[]”).
Returns
string
The resolved type name.
Overloads:
// Resolve from element
GetTypeName(IElement element)

// Resolve from template
GetTypeName(ITemplate template, TemplateDiscoveryOptions? options = null)

// Resolve from template ID
GetTypeName(string templateId, TemplateDiscoveryOptions? options = null)

// Resolve from template bound to model
GetTypeName(string templateId, IMetadataModel model, TemplateDiscoveryOptions? options = null)

AddTypeSource

public ClassTypeSource AddTypeSource(string templateId, string? collectionFormat = null)
Adds a template as a source for type resolution. When resolving types, the software factory will search templates registered as type sources.
templateId
string
required
The template ID to use as a type source.
collectionFormat
string
Default collection format for types resolved from this source.
Returns
ClassTypeSource
The type source for fluent API configuration.

IntentTemplateBase<TModel>

Base class for templates bound to a specific model.

Constructor

templateId
string
required
Unique identifier for this template.
outputTarget
IOutputTarget
required
The output target for this template.
model
TModel
required
The model instance this template is bound to.

Properties

Model
TModel
The model instance this template is bound to.

Methods

GetCorrelationId

public override string? GetCorrelationId()
Returns a correlation ID used to track template outputs between Software Factory executions. For model-based templates, this combines the template ID with the model ID.
Returns
string?
The correlation ID in format {TemplateId}#{ModelId}, or null if the model doesn’t support correlation.

Example

public class MyEntityTemplate : IntentTemplateBase<ClassModel>
{
    public const string TemplateId = "MyModule.EntityTemplate";

    public MyEntityTemplate(IOutputTarget outputTarget, ClassModel model) 
        : base(TemplateId, outputTarget, model)
    {
    }

    public override ITemplateFileConfig GetTemplateFileConfig()
    {
        return new TemplateFileConfig(
            fileName: $"{Model.Name}",
            fileExtension: "cs"
        );
    }

    public override string TransformText()
    {
        return $@"
public class {Model.Name}
{{
{string.Join(Environment.NewLine, Model.Properties.Select(p => $"    public {GetTypeName(p)} {p.Name} {{ get; set; }}"))}
}}";
    }
}

IntentTemplateBase<TModel, TDecorator>

Base class for templates that support both models and decorators.

Constructor

templateId
string
required
Unique identifier for this template.
outputTarget
IOutputTarget
required
The output target for this template.
model
TModel
required
The model instance this template is bound to.

Generic Type Parameters

TDecorator
ITemplateDecorator
required
The decorator contract interface. Must implement ITemplateDecorator.

Methods

GetDecorators

public IEnumerable<TDecorator> GetDecorators()
Returns all decorators added to this template, ordered by priority.
Returns
IEnumerable<TDecorator>
Decorators ordered by their Priority property.

AddDecorator

public void AddDecorator(TDecorator decorator)
Adds a decorator to this template. Called automatically by the Software Factory when a decorator is resolved.
decorator
TDecorator
required
The decorator instance to add.

GetDecoratorsOutput

protected string GetDecoratorsOutput(Func<TDecorator, string> propertyFunc)
Aggregates output from all decorators using the specified property function.
propertyFunc
Func<TDecorator, string>
required
Function to extract string output from each decorator.
Returns
string
Aggregated output from all decorators.

Example

public interface IClassDecorator : ITemplateDecorator
{
    string Fields();
    string Methods();
}

public class EntityTemplate : IntentTemplateBase<ClassModel, IClassDecorator>
{
    public const string TemplateId = "MyModule.EntityTemplate";

    public EntityTemplate(IOutputTarget outputTarget, ClassModel model)
        : base(TemplateId, outputTarget, model)
    {
    }

    public override ITemplateFileConfig GetTemplateFileConfig()
    {
        return new TemplateFileConfig(
            fileName: $"{Model.Name}",
            fileExtension: "cs"
        );
    }

    public override string TransformText()
    {
        return $@"
public class {Model.Name}
{{
{GetDecoratorsOutput(x => x.Fields())}

{GetDecoratorsOutput(x => x.Methods())}
}}";
    }
}

Template Registration

Learn about registering templates with the Software Factory

Output Target

Configure where template outputs are written

Template Dependencies

Manage dependencies between templates

Build docs developers (and LLMs) love