Archive for August, 2009

Simple Pipeline Event model with C#

After declaring my love for extension methods in the last post, it only seemed appropriate that it would come up again in an answer I gave to a stackoverflow question.  The question stated:

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 ?

My answer again leveraged an extension method to simplify the notification of events to each individual handler:

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");

Comments

IServiceProvider Extension Method

I love extension methods, ’nuff said:

public static class ServiceProviderExtension
{
    public static T GetService<T>(this IServiceProvider provider) where T: class
    {
        return provider.GetService(typeof(T)) as T;
    }

    public static K GetService<T, K>(this IServiceProvider provider)
        where T : class, K
        where K : class
    {
        return provider.GetService() as K;
    }

    public static K GetService<K>(this IServiceProvider provider, Type type)
        where K : class
    {
        return provider.GetService(type) as K;
    }

    public static K GetService<K>(this IServiceProvider provider, string type)
        where K : class
    {
        return provider.GetService(Type.GetType(type)) as K;
    }
}

Comments (1)

Blogging from the iPhone

I suppose I should have expected there to be “an app for that”. But I’m pleasantly surprised that there is a free wordpress app. Sweet.

Thaat being said there seems to be a bug with posting pictures taken from the app. That’s ok though, still pretty cool

Comments