enba
Jan 18, 2010
  8405
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024