Table of Contents

Class MethodUpdate

Namespace
Dynamicweb.Updates
Assembly
Dynamicweb.Core.dll

Represents an update that executes a specified method (action) when applied.

using System.Reflection;

namespace Dynamicweb.Updates.Examples
{
    /// <summary>
    /// Example UpdateProvider demonstrating usage of SqlUpdate, SettingUpdate, MethodUpdate, and FileUpdate.
    /// </summary>
    public class SqlUpdateSampleProvider : UpdateProvider
    {
        public override IEnumerable<Update> GetUpdates()
        {
            // SQL Update: Add a column to a table if it does not exist
            yield return SqlUpdate.AddColumn(
                id: "d50467a2-0e9b-4c70-a490-7ab4af88851b",
                provider: this,
                tableName: "MyCustomTable",
                columnName: "MyCustomColumn",
                columnDefinition: "NVARCHAR(50) NULL"
            );

            // SettingUpdate: Set a configuration value, only if it does not already exist
            yield return new SettingUpdate(
                id: "99fd48e3-f364-4108-bb19-6355e8b31896",
                provider: this,
                name: "/Globalsettings/System/Url/HighVolumeProductUrls/Enable",
                value: "True"
            );

            // SettingUpdate: Overwrite a configuration value even if it exists
            yield return new SettingUpdate(
                id: "89c68710-c2c1-4e02-8ea5-f712adeea24f",
                provider: this,
                name: "/Globalsettings/System/Url/HighVolumeProductUrls/Enable",
                value: "True",
                overwrite: true
            );

            // MethodUpdate: Execute custom logic during update
            yield return new MethodUpdate(
                id: "30c561dd-5509-4636-b81a-146be8a51e40",
                provider: this,
                method: context =>
                {
                    // Custom logic here
                    Console.WriteLine("Custom method executed as part of update.");
                }
            );

            // FileUpdate: Write a file to disk, only if it does not already exist
            yield return new FileUpdate(
                id: "efe5952c-bf3c-400d-a654-6fe2eb79ca44",
                provider: this,
                filePath: "/Files/Templates/Feeds/CustomFeedTemplate.cshtml",
                fileStreamProvider: () => GetFileStreamFromEmbeddedResource("CustomFeedTemplate.cshtml"),
                overwrite: false
            );

            // FileUpdate: Overwrite an existing file
            yield return new FileUpdate(
                id: "27c24328-285b-4cae-b62b-23130b6fcde5",
                provider: this,
                filePath: "/Files/Templates/Feeds/CustomFeedTemplate.cshtml",
                fileStreamProvider: () => GetFileStreamFromEmbeddedResource("CustomFeedTemplate.cshtml"),
                overwrite: true
            );
        }

        private Stream GetFileStreamFromEmbeddedResource(string resourceName)
        {
            string resourceFullName = $"CustomAssembly.{resourceName}";
            var assembly = Assembly.GetAssembly(this.GetType());
            var stream = assembly?.GetManifestResourceStream(resourceName);

            if (stream is null)
                throw new FileNotFoundException($"Embedded resource '{resourceFullName}' not found.");

            return stream;
        }
    }
}
public class MethodUpdate : Update
Inheritance
MethodUpdate
Inherited Members

Constructors

MethodUpdate(string, UpdateProvider, Action<UpdateContext>)

Initializes a new instance of the MethodUpdate class.

public MethodUpdate(string id, UpdateProvider provider, Action<UpdateContext> method)

Parameters

id string

The unique identifier for the update.

provider UpdateProvider

The update provider that owns this update.

method Action<UpdateContext>

The action to execute when the update is applied. The action receives an UpdateContext as its parameter.

Methods

Execute(UpdateContext)

Executes the update by invoking the specified method with the provided context.

public override void Execute(UpdateContext context)

Parameters

context UpdateContext

The context for the update execution.

To top