12 votes

UmbracoAOP Event Subscriber

UmbracoAOP is a project looking into how Aspect Oriented Programming can be applied to general Umbraco tasks.  Such as subscribing to Umbraco events!


What is Event Subscriber?


Event Subscriber allows developers to easily subscribe to Umbraco events, without worrying about all the plumbing.  This tool will allow you to subscribe to all ContentService and MediaService events.  Event Subscriber also allows you to filter by content type alias or media type alias when your function should be called.

Here's how you would subscribe to the Content Service publish event with Event Subscriber:

public class BlogPost
    //run on all Published events
    public static void WhenPublished(IPublishingStrategy sender, 
          Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
        //do something

    //run only when a BlogPost is published
    [UmbracoEvent( "BlogPost" )]
    public static void BlogPost_OnPublished(IPublishingStrategy sender, 
          Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
        //do something


Here's how you could subscribe to the publish event currently:

public class Startup : ApplicationEventHandler
    public Startup()
        ContentService.Published += Published;
        ContentService.Published += FilteredPublished;

    void Published(IPublishingStrategy sender, 
         Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
        //do something

    void FilteredPublished(IPublishingStrategy sender, 
         Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
        if (e.PublishedEntities.Any(c => c.ContentType.Alias == "BlogPost" || 
                    c.ContentType.Alias == "BlogFolder"))
            //do something



How does it work?


Event Subscriber detects the method signature and a method suffix.  The suffix is the name of the event - for example Saved, Saving, Published, Publishing, Copied, Moved, etc.

For example

 public static void WhenSaved(IContentService sender, 
          Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IContent> e)
     //do something

The function name is WhenSaved.  The beginning part of the method name can be named however you like.  It can even be 'IHopeThisIsSaved'.

The parameters must match the parameters required by the Umbraco Service events.

You can also specify when your event method should be called based on the Target Type Alias (Content, Media etc). 

Here is an example of a method that should only be called when a BlogPost is unpublished:

 [UmbracoEvent( "BlogPost" )]
 public static void BlogPost_OnPublished(IContentService sender, 
          Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IContent> e)
     //do something

You can also specify multiple types:

[UmbracoEvent( "BlogPost", "BlogFolder" ) )]
 public static void BlogPost_OnPublished(IContentService sender, 
          Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IContent> e)
     //do something



Why use Event Subscriber?


  • Easy to learn
  • No need to worry about which application startup to use
  • Abstracts over the Umbraco API, making it easy to make updates in the future.
  • Removes the need for various repeated if-statements to check for the correct type


You can install via NuGet:

PM> Install-Package UmbracoAOP.EventSubscriber

The source code is under MIT license.

You can contribute or download the source here:  https://github.com/base33/UmbracoAOP

The branch for this code is in "UmbracoE".



Reporting Issues:


Please log issues here: https://github.com/base33/UmbracoAOP/issues

You can also tweet me if you have any questions: @CraigNoble1989



Debugging Issues:


If you experience any issues, please refer to the correct Event signature (event and sender):

Content Service: http://our.umbraco.org/documentation/Reference/Events-v6/ContentService-Events

Media Service: http://our.umbraco.org/documentation/reference/events-v6/MediaService-Events


Upgrading from 1.0 to >=1.1:

Once you have installed the latest verion, either via Nuget or the package installer, here are some tips on updating your code:

- Change [ContentEvent.EventName] to just [UmbracoEvent]

- Change [ContentEvent.EventName( "BlogPost" )] to [UmbracoEvent("BlogPost")]

- Append the name of the event to the end of your method name.  The name of the event can be found here: http://our.umbraco.org/documentation/Reference/Events-v6/ContentService-Events


Package owner

Craig Noble

Craig Noble

Craig has 584 karma points

Package Compatibility

This package is compatible with the following versions as reported by community members who have downloaded this package:
Untested or doesn't work on Umbraco Cloud
Version 11.0.x (untested)
Version 10.1.x (untested)
Version 10.0.x (untested)
Version 9.5.x (untested)
Version 9.4.x (untested)
Version 9.3.x (untested)
Version 9.2.x (untested)
Version 9.1.x (untested)
Version 9.0.x (untested)
Version 8.18.x (untested)
Version 8.17.x (untested)
Version 8.16.x (untested)
Version 8.15.x (untested)
Version 8.14.x (untested)
Version 8.13.x (untested)
Version 8.12.x (untested)
Version 8.11.x (untested)
Version 8.10.x (untested)
Version 8.9.x (untested)
Version 8.8.x (untested)
Version 8.7.x (untested)
Version 8.6.x (untested)
Version 8.5.x (untested)
Version 8.4.x (untested)
Version 8.3.x (untested)
Version 8.2.x (untested)
Version 8.1.x (untested)
Version 8.0.x (untested)
Version 7.15.x (untested)
Version 7.14.x (untested)
Version 7.13.x (untested)
Version 7.12.x (untested)
Version 7.11.x (untested)
Version 7.10.x (untested)
Version 7.9.x (untested)
Version 7.8.x (untested)
Version 7.7.x (untested)
Version 7.6.x (untested)
Version 7.5.x (untested)
Version 7.4.x (untested)
Version 7.3.x (untested)
Version 7.2.x (untested)
Version 7.1.x (untested)
Version 7.0.x (untested)

You must login before you can report on package compatibility.

Package Information

  • Package owner: Craig Noble
  • Created: 06/10/2014
  • Current version 1.1
  • License MIT
  • Downloads on Our: 1.2K