What are Factory Extensions?
Factory extensions are classes that:- Execute at specific phases of Software Factory execution
- Can modify application configuration
- Access all templates and metadata
- Perform validation and verification
- Generate additional outputs or perform cleanup
- Integrate with external systems
Software Factory Lifecycle
The Software Factory executes in these phases:Creating a Factory Extension
using Intent.Engine;
using Intent.Modules.Common;
using Intent.Modules.Common.Plugins;
using Intent.Plugins.FactoryExtensions;
namespace MyModule.FactoryExtensions
{
public class ValidationFactoryExtension : FactoryExtensionBase
{
public override string Id => "MyModule.ValidationFactoryExtension";
public override int Order => 0;
protected override void OnAfterTemplateRegistrations(
IApplication application)
{
// Validate that required templates are present
var requiredTemplates = new[]
{
"Intent.Common.CSharp.Templates.Startup",
"Intent.AspNetCore.Controllers.Controller"
};
foreach (var templateId in requiredTemplates)
{
var templates = application.FindTemplateInstances(templateId);
if (!templates.Any())
{
throw new Exception(
$"Required template '{templateId}' is not registered.");
}
}
}
protected override void OnBeforeTemplateExecution(
IApplication application)
{
// Perform additional setup before generation
ConfigureOutputPaths(application);
ValidateMetadata(application);
}
protected override void OnAfterTemplateExecution(
IApplication application)
{
// Post-generation tasks
GenerateAdditionalFiles(application);
LogSummary(application);
}
private void ConfigureOutputPaths(IApplication application)
{
// Custom path configuration
}
private void ValidateMetadata(IApplication application)
{
// Metadata validation logic
}
private void GenerateAdditionalFiles(IApplication application)
{
// Generate supplementary files
}
private void LogSummary(IApplication application)
{
// Log generation summary
}
}
}
Lifecycle Hook Methods
OnStart
Executes at the very beginning:OnAfterMetadataLoad
After metadata is loaded, before template registration:OnAfterTemplateRegistrations
After all templates are registered:OnBeforeTemplateExecution
Just before templates execute:OnAfterTemplateExecution
After all templates have executed:OnEnd
Final cleanup:Common Factory Extension Patterns
Template Validation
Ensure required templates are present:Metadata Transformation
Modify metadata before generation:Dynamic Template Registration
Register templates based on conditions:Cross-Cutting Concerns
Add logging, caching, or validation:Code Generation Reports
Generate summary reports:External System Integration
Integrate with external tools or APIs:Execution Order
Control execution order with theOrder property:
Accessing Application Context
Application Settings
Metadata Manager
Output Targets
Finding Templates
Event Dispatcher
Error Handling
Throwing Exceptions
Stop execution with clear error messages:Logging Warnings
Warn without stopping execution:Best Practices
Keep Extensions Focused
Each extension should have a single responsibility:Use Appropriate Lifecycle Hooks
Execute logic at the right time:Handle Errors Gracefully
Log Important Actions
Testing Factory Extensions
Unit Testing
Integration Testing
Test with actual application:Troubleshooting
Extension not executing
Extension not executing
- Verify extension is registered in
.imodspec - Check module is installed in application
- Ensure no exceptions in extension constructor
- Review Software Factory logs for errors
Wrong execution order
Wrong execution order
- Check
Orderproperty values - Lower numbers execute first
- Use negative numbers for early execution
- Review other extensions’ order values
Cannot access templates
Cannot access templates
- Ensure accessing in correct lifecycle phase
- Templates available after
OnAfterTemplateRegistrations - Use correct template ID or interface
- Check template is actually registered
Next Steps
Creating Templates
Learn to create code generation templates
Creating Decorators
Modify template outputs with decorators
Testing Modules
Test your factory extensions
Designer Configuration
Configure visual designers