views:

488

answers:

3

In ASP.NET Web Apps , events are fired in particluar order :

for simplicity Load => validation =>postback =>rendering

Suppose I want to develop such pipeline -styled event

Example :

Event 1 [ "Audiance are gathering" ,Guys{ Event 2 and Event 3 Please wait until i signal }]

after Event 1 finished it task

Event 2 [ { Event 2, Event 3 "Audiance gathered! My task is over } ]

Event 2 is taking over the control to perform its task

Event 2 [ " Audiance are Logging in " Event 3 please wait until i signal ]

after Event 2 finished it task

.....

Event 3 [ "Presentation By Jon skeet is Over :) "]

With very basic example can anybody explain ,how can i design this ?

A: 

This example shows how you can take a simple abstract class, and a simple extension method on a list of handlers, and implement the pipeline model you're talking about. This is of course a trivialized example as I'm simply passing it a string as the event data. but you can obviously customize to suit your situation.

the extension method RaiseEvent enumerates over the list of handlers and calls the Handle method on the handler to notify it of 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");
Joel Martinez
You could do this with 1 event - it looks like you are duplicating the Invocation list.
Henk Holterman
+1  A: 

Windows Workflow Foundation is designed to do just this. There's 2 screencasts here you can take a look at on how to implement something like this.

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

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

Brian Surowiec
A: 

A very simple solution, that is also close to what actually happens 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();    
    }
}
Henk Holterman