Templates can override lifecycle methods to participate in the Software Factory execution:
public class MyTemplate : IntentTemplateBase<ClassModel>{ // Called after template creation public override void OnCreated() { base.OnCreated(); // Initialize template state } // Called after all templates are configured public override void OnConfigured() { base.OnConfigured(); // Discover and track dependencies } // Called after all templates are registered public override void AfterTemplateRegistration() { base.AfterTemplateRegistration(); // Final setup before execution } // Called before template execution public override void BeforeTemplateExecution() { base.BeforeTemplateExecution(); // Prepare for code generation }}
1
OnCreated
Template instance is created and constructor has executed
2
OnConfigured
All templates have been created and can now be discovered
3
AfterTemplateRegistration
All templates are registered and dependency graph is complete
Templates can declare dependencies on other templates:
public class ServiceTemplate : IntentTemplateBase<ServiceModel>{ public override void OnCreated() { base.OnCreated(); // Dependency on a specific template instance AddTemplateDependency("Intent.Application.Dtos.DtoTemplate", Model.ReturnType); // Dependency on a template by ID only AddTemplateDependency("Intent.Infrastructure.DependencyInjection"); }}
Template dependencies must be added during OnCreated() or OnConfigured() to ensure proper execution order.
Templates use the type resolution system to generate type names:
public class ServiceTemplate : IntentTemplateBase<ServiceModel>{ public ServiceTemplate(string templateId, IOutputTarget outputTarget, ServiceModel model) : base(templateId, outputTarget, model) { // Register type sources AddTypeSource("Intent.Application.Dtos.DtoTemplate"); AddTypeSource("Intent.Domain.Entities.EntityTemplate"); } public string GetReturnType() { // Resolves the type name and tracks dependency return GetTypeName(Model.ReturnType); }}
How Type Resolution Works
Template calls GetTypeName(typeReference)
System searches registered type sources for a template that outputs this type
If found, the template is added as a dependency
The fully qualified type name is returned
Collection formatters and nullable formatters are applied
public override bool CanRunTemplate(){ // Don't generate for abstract classes if (Model.IsAbstract) { return false; } return base.CanRunTemplate();}public override bool CanDiscoverTemplate(){ // Other templates can still find and reference this template return true;}
CanRun
Controls whether the template generates output
IsDiscoverable
Controls whether other templates can find this template