Open Closed

Cannot update or restore default and dynamic templates in overridden TemplateContentAppService #10027


User avatar
0
ohaddur created

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)
  • User Avatar
    1
    AI-Bot created
    Support Team ABP Agent

    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 TemplateDefinitionProvider and 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

    1. 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.
    2. 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.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    What is your actual use case? Have you encountered any problems?

    Thanks.

  • User Avatar
    0
    ohaddur created

    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.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Great 👍

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.1.0-preview. Updated on October 27, 2025, 08:34