From f40daf4b1faeae3d1b4c597e5331974c40da0a1d Mon Sep 17 00:00:00 2001 From: Patrick Schiefer Date: Sat, 5 Mar 2022 19:43:29 +0100 Subject: [PATCH 1/4] added command queue --- content/docs/patterns/command-queue/index.md | 216 ++++++++++++++++++ content/docs/patterns/command-queue/queue.png | Bin 0 -> 7334 bytes 2 files changed, 216 insertions(+) create mode 100644 content/docs/patterns/command-queue/index.md create mode 100644 content/docs/patterns/command-queue/queue.png diff --git a/content/docs/patterns/command-queue/index.md b/content/docs/patterns/command-queue/index.md new file mode 100644 index 00000000..aec2e223 --- /dev/null +++ b/content/docs/patterns/command-queue/index.md @@ -0,0 +1,216 @@ +--- +title: "Command Queue" +tags: ["AL"] +categories: ["Pattern"] +--- + +_Created by Patrick Schiefer, Described by Patrick Schiefer_ + +## Abstract +The goal of this pattern is to control the flow of multiple actions + + +## Problem +Sometimes its neccassary to perform multiple actions in Business Central, for example you want to post more than one order or before you post an Order you also have to post a purchase order this often leads to spaghetti code with big if else structures, which is not easy to read + + +## Description +To structure this problem we can use the "Command Queue" pattern. The pattern consist of two main parts the queue and the command interface + +![image](queue.png) + +First the command interface, it only has one procedure to execute the command +```al +interface ICommand +{ + procedure Execute(); +} +``` + + +And then the Queue which consist of two codeunits, the Queue itself and a Queue Entry + +```al +codeunit 50100 "Queue" +{ + procedure Push(var value: Interface ICommand) + var + Entry: Codeunit QueueEntry; + begin + Entry.SetValue(value); + if count = 0 then begin + first := Entry; + last := Entry; + end + else begin + last.SetNextEntry(Entry); + last := Entry; + end; + count += 1; + end; + + procedure Pop() value: Interface ICommand; + begin + if count > 0 then begin + value := first.GetValue(); + first := first.GetNextEntry(); + count -= 1; + end + else + Error('The Queue is empty!'); + end; + + procedure GetSize(): Integer + begin + exit(count); + end; + + var + first: Codeunit QueueEntry; + last: Codeunit QueueEntry; + count: Integer; +} + + + +codeunit 50102 "QueueEntry" +{ + procedure SetValue(var v: Interface ICommand); + begin + value := v; + end; + + procedure GetValue(): Interface ICommand; + begin + exit(value); + end; + + procedure GetNextEntry(): Codeunit QueueEntry; + begin + exit(NextEntry); + end; + + procedure SetNextEntry(var Entry: Codeunit QueueEntry); + begin + NextEntry := Entry; + end; + + var + value: Interface ICommand; + NextEntry: Codeunit QueueEntry; +} +``` + +As we see the queue entry stores a command, since the command is an interface we can hide each business logic behind + +## Benefits +The logical flow is very easy to adopt, it is even possible to add entries to the queue while it is processed. + +## Example + +In this short example I show you how to post multiple sales orders and display message after finishing the last post. + + +We have two commands in this example, the "SalesOrderPostCommander" is used to post a sales order and the "MessageCommander" displays a message. +```al +codeunit 50104 "SalesOrderPostCommander" implements ICommand +{ + procedure SetSalesOrderNumber(value : Code[20]) + begin + No := value; + end; + + procedure Execute() + begin + // TODO Post Sales Header + end; + + + var + No : Code[20]; +} + + +codeunit 50103 "MessageCommander" implements ICommand +{ + procedure SetText(value: Text); + begin + t := value; + end; + + procedure Execute() + begin + Message(t); + end; + + var + t: Text; +} +``` + +Using this two codeunits we can now implement a patch posting +```al + +codeunit 50105 PatchPostQueue +{ + procedure PatchPost() + begin + FilterSalesOrdersToPost(); + if not SalesOrders.Findset(false) then + exit(); // Nothing to post + + repeat + AddSalesOrderToQueue(SalesOrder."No."); + until SalesOrders.Next() = 0; + + AddMessageToQueue('Posting Complete'); + ExecuteQueue(); + end; + + local procedure ExecuteQueue() + var + object : interface "ICommand"; + begin + repeat + object := queue.Pop(); + object.Execute(); + until queue.GetSize() = 0; + end; + + local procedure FilterSalesOrdersToPost() + begin + // Filter Sales Orders here + end; + + local procedure AddMessageToQueue(message : Text); + var + t: Codeunit MessageCommander; + object: Interface ICommand; + begin + t.SetText(message); + object := t; + queue.Push(object); + end; + + local procedure AddSalesOrderToQueue(No : Text); + var + SaleOrderCommander: Codeunit SalesOrderPostCommander; + object: Interface ICommand; + begin + SaleOrderCommander.SetSalesOrderNumber(No); + object := SaleOrderCommander; + queue.Push(object); + end; + + + var + SalesOrders : Record "Sales Header"; + queue: Codeunit Queue; +} + +``` + + +## References +[Detailed Explanation of the pattern](https://patrickschiefer.wordpress.com/2022/02/24/part-2-how-to-implement-a-command-queue-in-pure-al/) + diff --git a/content/docs/patterns/command-queue/queue.png b/content/docs/patterns/command-queue/queue.png new file mode 100644 index 0000000000000000000000000000000000000000..a7411ca6d45dc8099c0523bc4289933e36c953c1 GIT binary patch literal 7334 zcmbVxcT`hd^X~CQc`b-^uz+xgAiaZ>VBr8kic}Q^6@sA&zJh>s3reUV5d@T?RFNh` zf`EVuNR%c`z#tZybZG%;x%=Svt@r-!T6e9xE`PAI_nGs|p4rdLo=hUl%?x+%+_w{g zpxs6|#1ewo$Plz;a621ViSc*$grL7N%}uQJSuEB(bJaVc^CROIcyvCj>B-CQl#k{t z7PQ<{NFzXqSAF4|;RnwLMyHnghZbIb_!*h|**T`o#Tp7t>#6UYEU|>NAc(<$(18Gj zP!R^0SZhKAT6b|gWElj>%f&HRAs@}fd=M6k!C*lQ7AsB*(u#vvj5tWnp}VP&06|B* z;=z&?)ttqE;usL+BNAO)s1?VcQ011J;+KmZSS%Xlqb7j{wh1)xE;j{1kfeDa9bf=3 z0*$2|Z%)vH7;#!5KD=TOL}Nf!@(`X}*zF~G1WZ}1XgSE`qdAQQi9x(2O@-PJ6i0xL zK$3XyfyQv5BEWzWux9{<+9PTek5J-8<4}$I(d2kYUX(?HmdW~%oIU~4q7k%MGy)LS zmIu06aRe=Gh!-yh;UP&DgBB7m>OwVV&*904Z=x-M+s(Uyy9jYKU_Mo|yEsV9S}r)Yy0-Ym5laUORdX3# z(FB$qu?!-BKP@c=*s)d|jRsyJh{?-o0jFBaxx~x4_=p~nbD#mMEgee84lX{t+SZ~a zUit(ARN^Ixx73e|qY>g5u%_d7PNA)TbJ&v}`%|wgn1w^~ybu8#h883zsVxs&AvroF zr0tZ?kHLoch=$*m51#+8+o2V#_` zH`K~rWOa@qaJy6FmGjStQ)eri!*M-JcIPWO$jDur&d1LpWyp{Ac$^6xZRq(P{-EcR zXTZ<}rC7D-`5yN#j95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V?d00001 From 19ca096afe64e685a835404af9ba0a50f0c1b665 Mon Sep 17 00:00:00 2001 From: Patrick Schiefer Date: Wed, 9 Mar 2022 16:16:29 +0100 Subject: [PATCH 2/4] Update Description --- content/docs/patterns/command-queue/index.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/content/docs/patterns/command-queue/index.md b/content/docs/patterns/command-queue/index.md index aec2e223..cdd41e57 100644 --- a/content/docs/patterns/command-queue/index.md +++ b/content/docs/patterns/command-queue/index.md @@ -7,14 +7,17 @@ categories: ["Pattern"] _Created by Patrick Schiefer, Described by Patrick Schiefer_ ## Abstract -The goal of this pattern is to control the flow of multiple actions +The goal of this pattern is to control the flow of multiple processes ## Problem -Sometimes its neccassary to perform multiple actions in Business Central, for example you want to post more than one order or before you post an Order you also have to post a purchase order this often leads to spaghetti code with big if else structures, which is not easy to read - +Sometimes its neccassary to perform multiple processes in Business Central, for example you want to post more than one order or before you post an Order you also have to post a purchase order this often leads to spaghetti code with big if else structures, which is not easy to read ## Description +The pattern is ideal for executing several independent processes in succession. Since the processes are independent, each process must take care of error handling itself. +The pattern should not be used to control a single process. + +## The Pattern To structure this problem we can use the "Command Queue" pattern. The pattern consist of two main parts the queue and the command interface ![image](queue.png) @@ -211,6 +214,7 @@ codeunit 50105 PatchPostQueue ``` + ## References [Detailed Explanation of the pattern](https://patrickschiefer.wordpress.com/2022/02/24/part-2-how-to-implement-a-command-queue-in-pure-al/) From f89246575909e614b96dfca7f114584fbc91891f Mon Sep 17 00:00:00 2001 From: Patrick Schiefer Date: Wed, 9 Mar 2022 16:18:49 +0100 Subject: [PATCH 3/4] Descpription --- content/docs/patterns/command-queue/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/patterns/command-queue/index.md b/content/docs/patterns/command-queue/index.md index cdd41e57..373a2727 100644 --- a/content/docs/patterns/command-queue/index.md +++ b/content/docs/patterns/command-queue/index.md @@ -15,7 +15,7 @@ Sometimes its neccassary to perform multiple processes in Business Central, for ## Description The pattern is ideal for executing several independent processes in succession. Since the processes are independent, each process must take care of error handling itself. -The pattern should not be used to control a single process. +The command queue should not be used to control a single process. Also it is important to know that the queue is just in the memory so if the service gets restartet the queue is gone and has to be rebuild ## The Pattern To structure this problem we can use the "Command Queue" pattern. The pattern consist of two main parts the queue and the command interface From 9a6f6e5b8b3090a27abaaa12c195d8493b376f6b Mon Sep 17 00:00:00 2001 From: Jeremy Vyska <35526546+JeremyVyska@users.noreply.github.com> Date: Tue, 5 Apr 2022 14:31:07 +0200 Subject: [PATCH 4/4] Very minor language tweaks Otherwise, solid :) --- content/docs/patterns/command-queue/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/docs/patterns/command-queue/index.md b/content/docs/patterns/command-queue/index.md index 373a2727..036d080f 100644 --- a/content/docs/patterns/command-queue/index.md +++ b/content/docs/patterns/command-queue/index.md @@ -15,7 +15,7 @@ Sometimes its neccassary to perform multiple processes in Business Central, for ## Description The pattern is ideal for executing several independent processes in succession. Since the processes are independent, each process must take care of error handling itself. -The command queue should not be used to control a single process. Also it is important to know that the queue is just in the memory so if the service gets restartet the queue is gone and has to be rebuild +The command queue should not be used to control a single process. Also it is important to know that the queue is just in the memory so if the service gets restarted the queue is gone and has to be rebuilt. ## The Pattern To structure this problem we can use the "Command Queue" pattern. The pattern consist of two main parts the queue and the command interface @@ -104,7 +104,7 @@ codeunit 50102 "QueueEntry" } ``` -As we see the queue entry stores a command, since the command is an interface we can hide each business logic behind +As we see the queue entry stores a command, since the command is an interface we can hide each business logic behind. ## Benefits The logical flow is very easy to adopt, it is even possible to add entries to the queue while it is processed.