How to filter Template Instances
When creating Templates that are configured to create a File per Model, you may need to filter which Models create Template Instances. An easy place to do this is in the Template Registrations.
For this How-to Guide, we will imagine a use case where we want to only create Repositories for Entities that have been flagged with a Stereotype. Let's assume that we have an Entity Settings
Stereotype with a check-box (boolean) property Has Repository
. It is configured to automatically apply to Domain
entities.
Finally, we will assume that a C# Template called RepositoryTemplate
has been created with the type File Per Model
and Designer and Model Type set to Domain
and Class
retrospectively:
Properties of the Repository Template, specified in the Module Builder.
To apply filtering to our Template, in the Visual Studio solution:
Navigate to the
RepositoryTemplateRegistration.cs
file.Alter the
GetModels
method to filter based on the Stereotype, as follows:public override IEnumerable<ClassModel> GetModels(IApplication application) { return _metadataManager.Domain(application).GetClassModels() .Where(x => x.GetEntitySettings().HasRepository()) .ToList(); }
Recompile the Project (
.csproj
).Reinstall the Module and rerun the Software Factory Execution.
Tip
Any metadata can be used to filter the creation of Template Instances. You may decide to use Stereotypes to be explicit or base the filtering on a convention like the name of the entity.