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 with mocking support

2741 Views   
/* 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); This version now supports mockability for unit-testing. To mock, create a mock of IEventAggregatorExtensionProvider within your unit test. Then call EventAggregatorExtensions.SetProvider passing in your mock. Now each call to the extension methods will delegate to your mock, thus you can verify that your mock was invoked. */ public static class EventAggregatorExtensions { private static IEventAggregatorExtensionsProvider _provider = new EventAggregatorExtensionsProvider(); public static void SetProvider(IEventAggregatorExtensionsProvider provider) { _provider = provider; } public static void Publish<TEvent>(this IEventAggregator eventAggregator, TEvent publishEvent) { _provider.Publish<TEvent>(eventAggregator, publishEvent); } public static SubscriptionToken Subscribe<TEvent>(this IEventAggregator eventAggregator, Action<TEvent> subscription) { return _provider.Subscribe(eventAggregator, subscription); } public static SubscriptionToken Subscribe<TEvent>(this IEventAggregator eventAggregator, Action<TEvent> subscription, bool keepSubscriberReferenceAlive) { return _provider.Subscribe<TEvent>(eventAggregator, subscription, keepSubscriberReferenceAlive); } public static SubscriptionToken Subscribe<TEvent>(this IEventAggregator eventAggregator, Action<TEvent> subscription, ThreadOption threadOption, bool keepSubscriberReferenceAlive = false, Predicate<TEvent> filter = null) { return _provider.Subscribe(eventAggregator, subscription, threadOption, keepSubscriberReferenceAlive, filter); } public static void Unsubscribe<TEvent>(this IEventAggregator eventAggregator, SubscriptionToken token) { _provider.Unsubscribe<TEvent>(eventAggregator, token); } public static void Unsubscribe<TEvent>(this IEventAggregator eventAggregator, Action<TEvent> subscription) { _provider.Unsubscribe(eventAggregator, subscription); } } public class EventAggregatorExtensionsProvider : IEventAggregatorExtensionsProvider { public void Publish<TEvent>(IEventAggregator eventAggregator, TEvent publishEvent) { var prismEvent = eventAggregator.GetEvent<CompositePresentationEvent<TEvent>>(); prismEvent.Publish(publishEvent); } public SubscriptionToken Subscribe<TEvent>(IEventAggregator eventAggregator, Action<TEvent> subscription) { var prismEvent = eventAggregator.GetEvent<CompositePresentationEvent<TEvent>>(); return prismEvent.Subscribe(subscription); } public SubscriptionToken Subscribe<TEvent>(IEventAggregator eventAggregator, Action<TEvent> subscription, bool keepSubscriberReferenceAlive) { var prismEvent = eventAggregator.GetEvent<CompositePresentationEvent<TEvent>>(); return prismEvent.Subscribe(subscription, keepSubscriberReferenceAlive); } public SubscriptionToken Subscribe<TEvent>(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 void Unsubscribe<TEvent>(IEventAggregator eventAggregator, SubscriptionToken token) { var prismEvent = eventAggregator.GetEvent<CompositePresentationEvent<TEvent>>(); prismEvent.Unsubscribe(token); } public void Unsubscribe<TEvent>(IEventAggregator eventAggregator, Action<TEvent> subscription) { var prismEvent = eventAggregator.GetEvent<CompositePresentationEvent<TEvent>>(); prismEvent.Unsubscribe(subscription); } } public interface IEventAggregatorExtensionsProvider { void Publish<TEvent>(IEventAggregator eventAggregator, TEvent publishEvent); SubscriptionToken Subscribe<TEvent>(IEventAggregator eventAggregator, Action<TEvent> subscription); SubscriptionToken Subscribe<TEvent>(IEventAggregator eventAggregator, Action<TEvent> subscription, bool keepSubscriberReferenceAlive); SubscriptionToken Subscribe<TEvent>(IEventAggregator eventAggregator, Action<TEvent> subscription, ThreadOption threadOption, bool keepSubscriberReferenceAlive = false, Predicate<TEvent> filter = null); void Unsubscribe<TEvent>(IEventAggregator eventAggregator, SubscriptionToken token); void Unsubscribe<TEvent>(IEventAggregator eventAggregator, Action<TEvent> subscription); }
by Glenn Block
  October 10, 2010 @ 9:20pm
Tags:
Add a comment


Report Abuse
brought to you by:
West Wind Techologies