When overriding TemplateContentAppService to support both default (definition-based) and dynamic templates, neither the update (PUT /api/text-template-management/template-contents) nor the restore-to-default (POST /restore-to-default) endpoints persist changes to the database.
How should both template types (default + dynamic) be updated correctly?
Are default templates intended to persist in the DB, or always in-memory via TemplateDefinitionProvider?
What is the recommended approach for unified update/restore behavior in TextTemplateManagement?
4 Answer(s)
-
1
To support both default (definition-based) and dynamic templates in ABP's TextTemplateManagement, you need to understand how each template type is managed and persisted:
- Default templates are defined via
TemplateDefinitionProviderand are not persisted in the database by default. They exist in memory and are typically loaded from embedded resources or the virtual file system. - Dynamic templates (user-created or modified at runtime) are stored in the database and managed via the TextTemplateManagement module.
Update and Restore Behavior
- Updating a default template: When you update a default template via the API, the change is stored in the database as an override. The system will use the database value instead of the in-memory definition. If you restore to default, the database override is deleted, and the system falls back to the original definition.
- Updating a dynamic template: The update is persisted directly in the database.
Recommended Approach for Unified Update/Restore
- Override
TemplateContentAppService: In your custom service, when handling updates or restores, check if the template exists in the database. If not, create a new record to override the default. For dynamic templates, update the existing record. - Restore to Default: For both types, removing the database record will restore the template to its default (definition-based) state if it exists, or remove it entirely if it's dynamic.
Example Logic
public override async Task UpdateAsync(UpdateTemplateContentDto input) { var template = await _templateContentRepository.FindAsync(input.Name, input.CultureName); if (template == null) { // Create override for default template template = new TextTemplateContent( GuidGenerator.Create(), input.Name, input.CultureName, input.Content ); await _templateContentRepository.InsertAsync(template); } else { // Update dynamic or overridden template template.SetContent(input.Content); await _templateContentRepository.UpdateAsync(template); } } public override async Task RestoreToDefaultAsync(RestoreTemplateContentDto input) { var template = await _templateContentRepository.FindAsync(input.Name, input.CultureName); if (template != null) { await _templateContentRepository.DeleteAsync(template); } // Now, the system will use the default definition if it exists }Key Points
- Default templates: Not persisted unless overridden; updates create a DB override.
- Dynamic templates: Always persisted in the DB.
- Unified behavior: Always update or delete the DB record; fallback to definition if no DB record exists.
Sources
This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.
- Default templates are defined via
-
0
What is your actual use case? Have you encountered any problems?
Thanks.
-
0
I was trying to extend ABP’s Text Template Management to support both default and dynamic templates. The issue I faced was that updating or restoring template content didn’t work properly for either type. After applying the AI Bot’s suggested approach, the problem was resolved and both default and dynamic templates can now be updated correctly.
-
0
Great 👍