I have a class which processes messages:
public abstract class ProcessingBase {
public bool IsBusy { get; set; }
public Queue<CustomMessage> PendingMessages { get; private set; }
public abstract MessageProcessingResult Process();
...
In the processing modules I've created so far, one uses a queue to process messages, sending them over a socket and the other sends emails. This works fine for individual processing modules, but suppose I wanted to chain these two?
I was thinking I could do something like:
public class ChainProcessor : ProcessingBase {
public List<ProcessingBase> Processors { get; set; }
public override MessageProcessingResult Process() {
if (IsBusy)
return null;
IsBusy = true;
CustomMessage msg = null;
this.ProcessedMessages = new List<CustomMessage>();
// create clone of queue
var messagesToSend = new Queue<CustomMessage>(this.PendingMessages);
this.PendingMessages.Clear();
while (messagesToSend.Count > 0 && (msg = messagesToSend.Dequeue()) != null) {
foreach (var processor in this.Processors) {
// something with yield return?
}
}
It's the actual chaining which I'm shaky on. Ideally I'd want to process them in a kind of 'wave'. Eg:
Module 1 - Processed message A
Module 2 - Processed message A
Module 1 - Processed message B
Module 2 - Processed message B
Module 3 - Processed message A
Module 1 - Processed message C
Where each message is passed through the chain, with messages constantly entering and leaving as they move through. What would be the best way of doing this? Or am I limited to sequentially passing each message through the entire chain?
Ie (What I don't want): Module 1 - Processed message A Module 2 - Processed message A Module 3 - Processed message A Module 1 - Processed message B Module 2 - Processed message B Module 3 - Processed message B Module 1 - Processed message C
EDIT: I was hoping I could do something where the first processor would yield return back to the 'chain processor', which would pass that message onto module 2, then perhaps the chain could start the next message on processor1