November Happy Hour will be moved to Thursday December 5th.

enba
Jan 18, 2010
  8466
(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 SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog