enba
Jan 18, 2010
visibility 9597
star star star star star
(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

error Please login to comment.
Latest blogs
Log your Optimizely Graph queries

Log Graph calls from .NET in in a way where it's easy to copy the query and parameters to GraphiQL.

Johan Kronberg | Nov 6, 2026 |

Introducing Optimizely Feature Flagging: Flag Your Content Model

Feature flag Optimizely CMS content properties without code changes: visibility, validation, editors and whole content types, with any flag provider.

Andy Blyth | Nov 3, 2026 |

Introducing Optimizely Content Seeder: Seed Your CMS from Code

Seed Optimizely CMS 12 and 13 with pages, blocks, media and Visual Builder sections from a fluent C# definition, with optional AI-written content.

Andy Blyth | Nov 3, 2026 |

Optimizely DXP Alert: Are You Affected by the October 2026 Node.js Security Vulnerabilities?

On October 27, 2026, Optimizely published a security advisory regarding recently disclosed Node.js vulnerabilities affecting Node.js 22.x, 24.x, an...

Madhu | Nov 2, 2026 |

Build Your First Opal Tool in C#

Optimizely ships a C# SDK for building custom

Adnan Zameer | Nov 2, 2026 |

Introducing Publishing Timeline for Optimizely CMS 13

When working with scheduled content in Optimizely CMS, having a clear overview of what's going live and when can be really helpful, especially when...

Mardy | Nov 2, 2026