Menu

command_pattern

Katherine E. Lightsey


Command pattern - The command pattern is a behavioral design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.


Command pattern using [xml]
- command object
- receiver object

    declare @command [xml] = 
    N'<command>
        <receiver>
            <parameters>@output [xml] output</parameters>
            <sql>select @output = (<select statement> 
                for xml path(output'), root(N'output_tree'));</sql>
        </receiver>
    </command>';

Setter / Run
- invoker object

    declare procedure [workflow].[run] @workflow [xml]
    as
        declare @output [xml];

        execute sp_execute
        @sql=@workflow.value(N'(/command/receiver/sql/text())[1]', N'[nvarchar](max)'),
        @parameters=@workflow.value(
            N'(/command/receiver/parameters/text())[1]', N'[nvarchar](max)'),
        @output=@output output;
    go

Getter
-client object

    declare procedure [workflow].[get] @workflow [xml]
    as 
        set @workflow = (select [workflow] from [<table>] 
                            where [workflow].<id> = @workflow.<id>);
        if (@workflow is not null)
            [workflow].[run] @workflow=@workflow;
    go


Related

Wiki: design_pattern

MongoDB Logo MongoDB
Gen AI apps are built with MongoDB Atlas
Atlas offers built-in vector search and global availability across 125+ regions. Start building AI apps faster, all in one place.
Try Free →