enba
Jan 18, 2010
  9382
(1 votes)

.NET Reactive Extensions on DataFactory Events

.Net Framework 4 is soon to be released and it is bringing a lot of really, really nice features. Some of those are already available as an add-on to the current version(3.5 sp1) or as a beta of .NET Framework 4.

The one that particularly tickled my curiosity are the Reactive Extensions for .NET(RX). There are a lots of videos on Channel 9 where you can see how it works. Especially interesting are the new IObservable<T> and IObserver<T> interfaces. Of course Observer design pattern is nothing new and it is already largely applied in .NET trough the events model, but what is interesting with this new approach is the possibility to apply LINQ to the events.

Let me show you with the example. Let’s say we want to subscribe to the PublishingPage event that is fired by  EPiServer.DataFactory before page is published but only for the pages that are published on some custom PageProvider. We could write a LINQ query like this

 var remotePagePublished = from evnt in DataFactory.Instance.GetPublishingPageEvent() 
                           where evnt.EventArgs.PageLink.IsRemote()
                           select evnt.EventArgs.Page;

Now compare it with the classic event model

...

DataFactory.Instance.PublishingPage +=

new PageEventHandler(Instance_PublishingPage);

...
void Instance_PublishingPage(object sender, PageEventArgs e)
{
      if (e.PageLink.IsRemote())
     { 
          // do something
     }
}
To me LINQ seems much easier to understand

Now let’s see how can we enable DataFactory events to be “queryable”.

First of all we need to download and install RX. After that we need to reference 2 new libraries

dlls

You might noticed in the LINQ query that I was invoking GetPublishingPageEvent() which actually doesn't exist on DataFactory. It is an extension method that I wrote and it returns an IObservable instance of the PublishingPage event.

public static class DataFactoryEventExtensions
{
   public static IObservable<IEvent<PageEventArgs>> GetPublishingPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), 
                                h => dataFactory.PublishingPage += h, 

h => dataFactory.PublishingPage -= h);

/*
// if you prefer you can use this more readable overload 
// return Observable.FromEvent<PageEventArgs>(dataFactory, "PublishingPage");
*/
   }
}

As you can se there already is a Observable type that exposes many useful static methods which we can use to retrieve an observable instance. Now if you compile this code and add it to your solution you are able to simply subscribe to the event and do specific filtering on it by using LINQ as shown before. 

var remotePagePublishing = from evnt in DataFactory.Instance.GetPublishingPageEvent()                          
			  where evnt.EventArgs.PageLink.IsRemote()                          
			  select evnt.EventArgs.Page; 
remotePagePublishing.Subscribe(                
                (page) => {
                    // do something with published page
                });

The nice thing is that IObservable<T> defines only one method “Subscribe” which return an IDisposable object.

If you’re interested in playing with DataFactory events and LINQ I’ve written an extension class for all data factory events so that you can have kick start. There are also two helper methods that allow you getting an event by its name: GetPageEventByName, GetChildrenEventByName. Here is the code.

 
public static class DataFactoryEventExtensions
{
   public static IObservable<IEvent<PageEventArgs>> GetPageEventByName(this DataFactory dataFactory, string eventName)
   {
       return Observable.FromEvent <PageEventArgs>(dataFactory, eventName);
   }
 
   public static IObservable<IEvent<ChildrenEventArgs>> GetChildrenEventByName(this DataFactory dataFactory, string eventName)
   {
       return Observable.FromEvent<ChildrenEventArgs>(dataFactory, eventName);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetPublishingPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.PublishingPage += h, h => dataFactory.PublishingPage -= h);
   }
   
   public static IObservable<IEvent<PageEventArgs>> GetPublishedPageEvent(this DataFactory dataFactory)
   {
        return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.PublishedPage += h, h => dataFactory.PublishedPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetCreatingPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.CreatingPage += h, h => dataFactory.CreatingPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetCreatedPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.CreatedPage += h, h => dataFactory.CreatedPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetCheckingInPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.CheckingInPage += h, h => dataFactory.CheckingInPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetCheckedInPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.CheckedInPage += h, h => dataFactory.CheckedInPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetDeletingPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.DeletingPage += h, h => dataFactory.DeletingPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetDeletedPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.DeletedPage += h, h => dataFactory.DeletedPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetDeletingPageLanguageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.DeletingPageLanguage += h, h => dataFactory.DeletingPageLanguage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetLoadingPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.LoadingPage += h, h => dataFactory.LoadingPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetLoadedPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.LoadedPage += h, h => dataFactory.LoadedPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetFailedLoadingPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.FailedLoadingPage += h, h => dataFactory.FailedLoadingPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetFinishedLoadingPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.FinishedLoadingPage += h, h => dataFactory.FinishedLoadingPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetLoadingDefaultPageDataEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.LoadingDefaultPageData += h, h => dataFactory.LoadingDefaultPageData -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetLoadedDefaultPageDataEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.LoadedDefaultPageData += h, h => dataFactory.LoadedDefaultPageData -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetMovingPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.MovingPage += h, h => dataFactory.MovingPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetMovedPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.MovedPage += h, h => dataFactory.MovedPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetSavingPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.SavingPage += h, h => dataFactory.SavingPage -= h);
   }
 
   public static IObservable<IEvent<PageEventArgs>> GetSavedPageEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<PageEventArgs> h) => new PageEventHandler(h), h => dataFactory.SavedPage += h, h => dataFactory.SavedPage -= h);
   }
       
   public static IObservable<IEvent<ChildrenEventArgs>> GetLoadingChildrenEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<ChildrenEventArgs> h) => new ChildrenEventHandler(h), h => dataFactory.LoadingChildren += h, h => dataFactory.LoadingChildren -= h);
   }
 
   public static IObservable<IEvent<ChildrenEventArgs>> GetLoadedChildrenEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<ChildrenEventArgs> h) => new ChildrenEventHandler(h), h => dataFactory.LoadedChildren += h, h => dataFactory.LoadedChildren -= h);
   }
 
   public static IObservable<IEvent<ChildrenEventArgs>> GetFailedLoadingChildrenEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<ChildrenEventArgs> h) => new ChildrenEventHandler(h), h => dataFactory.FailedLoadingChildren += h, h => dataFactory.FailedLoadingChildren -= h);
   }
 
   public static IObservable<IEvent<ChildrenEventArgs>> GetFinishedLoadingChildrenEvent(this DataFactory dataFactory)
   {
       return Observable.FromEvent((EventHandler<ChildrenEventArgs> h) => new ChildrenEventHandler(h), h => dataFactory.FinishedLoadingChildren += h, h => dataFactory.FinishedLoadingChildren -= h);
   }
}

Let me know what you think about this and if you have more cool ideas on how this approach could be used. If you want to learn more on RX you can visit RX home page where you’ll find a lot of useful links on learning resources.

Happy programming.

Jan 18, 2010

Comments

Sep 21, 2010 10:33 AM

Cool Enes, nice post!
/ Paul Smith

Sep 21, 2010 10:33 AM

Great summary, Enes. Thanks for sharing this!
/ John W

Please login to comment.
Latest blogs
Optimizely CMS 13: What Actually Changed and Why It Matters

I had the privilege of attending a deep-dive session on CMS 13 this week, and after seeing the full roadmap laid out across these slides, I wanted ...

Aniket | May 12, 2026

Introducing the Optimizely MCP Server: AI That Speaks Commerce

MCP AI Commerce B2B Claude ChatGPT OpenAI Optimizely Insite Commerce Introducing the Optimizely MCP Server : AI That Speaks Commerce We've connecte...

Vaibhav | May 12, 2026

AEO, GEO and SEO with Epicweb AI Assistant in Optimizely CMS

Traditional SEO remains important, but content must now also be optimized for answer engines and generative AI. This article explains how the Epicw...

Luc Gosso (MVP) | May 11, 2026 |

Accelerating Optimizely CMS and Commerce upgrades with agentic AI (Part 1 of 2)

How Niteco's Upgrade Machine   uses orchestrated AI coding agents to deliver a buildable baseline and a running CMS, then hands over for...

Hung Le Hoang | May 11, 2026

Commerce 15 and CMS 13: Optimizely’s Next Step Toward AI-Powered, Graph-First Commerce

Optimizely is preparing to release Commerce 15 in mid-May 2026 , positioning this as a foundational shift—not just an upgrade. The direction is...

Augusto Davalos | May 7, 2026

The future of Content: Introducing Optimizely CMS 13

Optimizely In the rapidly evolving landscape of digital experience, the "monolithic vs. headless" debate is being replaced by a more sophisticated...

Aniket | May 6, 2026