Extensibility and Events

Extensions Series

FunnelWeb's extensibility system makes it possible to write interesting extensions to FunnelWeb using a MEF powered plug-in model.

When the application starts up, it uses MEF to scan a bin\Extensions folder for classes implementing the IFunnelWebExtension interface:

public interface IFunnelWebExtension 
{ 
    void Initialize(ContainerBuilder builder); 
}

Each extension gets a chance to contribute to the IOC container. Eventually we'll be able to use this to abstract things like data storage or spam filtering. For example, by placing "FunnelWeb.Raven.dll" into the bin\Extensions folder, you'll be able to use the RavenDB data provider.

The second thing that goes hand-in-hand with extensions is events.

The idea is that the application acts like a little service bus - a request comes in, ASP.NET MVC processes it, controllers do the bulk of the work, and finally the NHibernate transaction is committed.

During that processing chain, different parts of the app can raise events. They do this by taking a dependency on IEventPublisher. For example, inside the WikiController, when a comment is posted, it raises:

EventPublisher.Publish(new CommentPostedEvent(entry, comment));

An extension can then register with the IOC container that it has a new IEventListener:

[FunnelWebExtension(FullName = "Beep when comments are posted", Publisher = "FunnelWeb", SupportLink = "http://code.google.com/p/funnelweb")] 
public class MyExtension : IFunnelWebExtension 
{ 
    public void Initialize(ContainerBuilder builder) 
    {
        builder.RegisterType<BeepWhenCommentsPosted>().As<IEventListener>().InstancePerLifetimeScope(); 
    } 
}

The event listener would look like this:

public class BeepWhenCommentsPosted: IEventListener 
{ 
    public void Handle(Event payload) 
    { 
        var commentDetails = payload as CommentPostedEvent; 
        if (commentDetails == null) 
            return; 
        Console.Beep(); 
    }

Currently there's only one event - CommentPostedEvent - but in future we'll add a few more:

  • Pingback posted
  • Page not found
  • Authentication failed
  • Page edited
  • Page viewed

It would then be easy to implement features like:

  • Build a "most popular pages" list by counting the PageViewed events
  • Build a pingback system that notifies other people's blogs if you revise a page to include links to their blog
  • Build something to replace the "search" page by redirecting NotFound events to Google.com

If you're planning to implement any new features for FunnelWeb, it would be good to consider whether they would be better as extensions than editing the code directly.

mef extensions plugins events
Posted by: The FunnelWeb Team
Last revised: 02 Feb, 2011 11:36 PM History

Comments

No comments yet. Be the first!

No new comments are allowed on this post.