Menu

chain_of_command_pattern

Katherine E. Lightsey


Chain of command pattern - The chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain.


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], @next [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;

        execute [logging].[set_entry] @typed_entry=@output;

        set @next = @output.query(N'(/command/next)[1]');
        if (@next is not null)
                execute procedure [workflow].[get] @workflow=@next;
    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
Wiki: workflow