Overview
Template dependencies define relationships between templates, ensuring they execute in the correct order and enabling templates to reference each other. The Intent Architect Software Factory uses these dependencies to build an execution graph and resolve references during code generation.Why Template Dependencies?
Template dependencies solve several key problems:- Execution Order - Ensure templates execute in the correct sequence
- Cross-Template References - Enable templates to reference types/code from other templates
- Type Resolution - Automatically resolve type names from dependent templates
- Validation - Detect missing templates or circular dependencies early
ITemplateDependency Interface
The identifier of the template or role being depended upon. Can be
null for type-based dependencies.Optional. If specified, the dependency must be accessible to this output target.
Creating Template Dependencies
TheTemplateDependency class provides static factory methods for creating dependencies:
OnTemplate (by Template ID)
The unique template identifier or role name.
The output target that must be able to access the template.
OnTemplate (by Template Instance)
The template instance to depend on.
OnModel (by Template ID and Model)
The template identifier.
The model instance the template must be bound to.
OnModel (with Predicate)
The template identifier.
Predicate to match template models.
OfType (Type-Based Dependency)
The template type to depend on.
Adding Dependencies
In Template Constructor
Add dependencies in the template constructor to ensure they’re registered early:Using AddTemplateDependency
Implicit Dependencies via Type Sources
Adding type sources automatically creates dependencies:GetTypeName() resolves a type from this source, the dependency is automatically tracked.
Retrieving Dependencies
GetTemplateDependencies
Dependency Resolution and Execution Order
The Software Factory builds a dependency graph and executes templates in topological order:- Templates with no dependencies execute first
- Templates with dependencies execute after their dependencies
- Circular dependencies are detected and cause errors
Example Execution Order
Cross-Output-Target Dependencies
Templates can depend on templates in different output targets (e.g., different projects):Fast Lookup Dependencies
The framework optimizes dependency resolution usingIFastLookupTemplateDependency. Dependencies created via TemplateDependency factory methods automatically use this optimization.
Benefits
- Caching - Template lookups are cached
- Performance - Reduces search time for large template sets
- Automatic - No additional code required
Common Patterns
Entity and Repository Pattern
Service and DTO Pattern
Aggregate Root Dependencies
Troubleshooting
Circular Dependency Error
Problem: Two templates depend on each other, creating a cycle. Solution: Refactor to remove the circular dependency, often by:- Using interfaces
- Creating a third template both can depend on
- Using late binding (string references instead of type resolution)
Template Not Found
Problem: Dependency references a template that doesn’t exist. Solution:- Verify the template ID is correct
- Ensure the module containing the template is installed
- Check if the template should be in a different output target
Wrong Execution Order
Problem: Template executes before its dependencies are ready. Solution:- Add explicit dependencies using
AddTemplateDependency() - Ensure type sources are configured before resolving types
- Check that dependencies are added in the constructor, not later lifecycle hooks
Best Practices
Add Dependencies Early
Add dependencies in the template constructor to ensure they’re registered before execution planning.
Use Type Sources
Use
AddTypeSource() for type resolution - it automatically manages dependencies.Specify Accessibility
Use the
accessibleTo parameter for cross-output-target dependencies.Avoid Circular Dependencies
Design template relationships to avoid circular dependencies.
Use Model-Based Dependencies
Prefer
OnModel() over OnTemplate() when templates are model-bound.Related Topics
IntentTemplateBase
Base class providing dependency management methods
Type Resolution
Using type sources and GetTypeName with dependencies
Output Target
Understanding output target accessibility
Template Registration
How templates are instantiated and registered