CodePaste Logo
New Snippet New Recent Snippets Recent Snippets My Snippets My Snippets My Favorites Favorites Web Code Search Snippets Search
Sign inor Register
Language: csharp

Prism EventAggregator extension methods

2734 Views   
/* See the updated version at: http://codepaste.net/woqq1d which supports mocking. some simple helpers to clean up the syntax / remove the need for the Get<Event> pattern. Works with pure poco event classes. Inspired by this post from Ward Bell: http://neverindoubtnet.blogspot.com/2009/07/simplify-prism-event-aggregator.html example usage given an OrderCreated event //Given this event: public class OrderCreated{ public Order Order {get;set;} } //to publish use this _ea.Publish(new OrderCreated(new Order())); //to subscribe use this where CreateOrder is an Action<OrderCreated> _ea.Subscribe<OrderCreated>(CreateOrder); */ public static class EventAggregatorExtensions { public static void Publish<TEvent>(this IEventAggregator eventAggregator, TEvent publishEvent) { var prismEvent = eventAggregator.GetEvent<CompositePresentationEvent<TEvent>>(); prismEvent.Publish(publishEvent); } public static SubscriptionToken Subscribe<TEvent>(this IEventAggregator eventAggregator, Action<TEvent> subscription) { var prismEvent = eventAggregator.GetEvent<CompositePresentationEvent<TEvent>>(); return prismEvent.Subscribe(subscription); } public static SubscriptionToken Subscribe<TEvent>(this IEventAggregator eventAggregator, Action<TEvent> subscription, bool keepSubscriberReferenceAlive) { var prismEvent = eventAggregator.GetEvent<CompositePresentationEvent<TEvent>>(); return prismEvent.Subscribe(subscription, keepSubscriberReferenceAlive); } public static SubscriptionToken Subscribe<TEvent>(this IEventAggregator eventAggregator, Action<TEvent> subscription, ThreadOption threadOption, bool keepSubscriberReferenceAlive = false, Predicate<TEvent> filter = null) { var prismEvent = eventAggregator.GetEvent<CompositePresentationEvent<TEvent>>(); return prismEvent.Subscribe(subscription, threadOption, keepSubscriberReferenceAlive, filter); } public static void Unsubscribe<TEvent>(this IEventAggregator eventAggregator, SubscriptionToken token) { var prismEvent = eventAggregator.GetEvent<CompositePresentationEvent<TEvent>>(); prismEvent.Unsubscribe(token); } public static void Unsubscribe<TEvent>(this IEventAggregator eventAggregator, Action<TEvent> subscription) { var prismEvent = eventAggregator.GetEvent<CompositePresentationEvent<TEvent>>(); prismEvent.Unsubscribe(subscription); } }
by Glenn Block
  October 10, 2010 @ 4:00pm
Tags:
Add a comment


Report Abuse
brought to you by:
West Wind Techologies