C # -Pipeline Style Event Model

In ASP.NET web applications, events are fired in the following order:

for simplicity Loading => validation => postback => rendering

Suppose I want to design such an event associated with a pipeline

Example:

Event 1 [โ€œAudience collectsโ€, โ€œGuysโ€ {Event 2 and Event 3 Please wait until I get a signal}}

after the completion of the 1st event

Event 2 [{Event 2, Event 3 "Audience collected! My task completed}]

Event 2 takes control to complete its task.

Event 2 ["Audio is recorded" Event 3, please wait until I hear a signal

after event 2 completed the task

.....

Event 3 ["Presentation by John skeet is Over :)"]

With a very simple example, can someone explain how I can create this?

+3
source share
3 answers

A very simple solution that is also close to what is actually happening in ASP.NET:

class EventChain { public event EventHandler Phase1Completed; public event EventHandler Phase2Completed; public event EventHandler Phase3Completed; protected void OnPhase1Complete() { if (Phase1Completed != null) { Phase1Completed(this, EventArgs.Empty); } } public void Process() { // Do Phase 1 ... OnPhase1Complete(); // Do Phase 2 ... OnPhase2Complete(); } } 
0
source

The Windows Workflow Foundation is designed for this. There are 2 screencasts you can see how to implement something like this.

http://blog.wekeroad.com/mvc-storefront/mvcstore-part-19a/

http://blog.wekeroad.com/mvc-storefront/mvcstore-part-21/

+1
source

This example shows how you can take a simple abstract class and a simple extension method in the list of handlers and implement the pipeline model you are talking about. This, of course, is a trivialized example, because I just pass it a string as event data. but you can obviously tweak it to suit your situation.

The RaiseEvent extension RaiseEvent lists the handlers in the list and calls the Handle method on the handler to notify the event.

 public abstract class Handler { public abstract void Handle(string event); } public static class HandlerExtensions { public static void RaiseEvent(this IEnumerable<Handler> handlers, string event) { foreach(var handler in handlers) { handler.Handle(event); } } } ... List<Handler> handlers = new List<Handler>(); handlers.Add(new Handler1()); handlers.Add(new Handler2()); handlers.RaiseEvent("event 1"); handlers.RaiseEvent("event 2"); handlers.RaiseEvent("event 3"); 
0
source

Source: https://habr.com/ru/post/1383418/


All Articles