Skip to content

Instantly share code, notes, and snippets.

@beufordy3k
Last active August 29, 2015 14:07
Show Gist options
  • Save beufordy3k/96a2c33a3f4a533e55fc to your computer and use it in GitHub Desktop.
Save beufordy3k/96a2c33a3f4a533e55fc to your computer and use it in GitHub Desktop.
Prism IEventAggregator Generic subscription service
using System;
using System.Collections.Generic;
using TestApp.Interfaces;
using Microsoft.Practices.Prism.PubSubEvents;
namespace TestApp.Services
{
public class EventSubscriberService : IEventSubscriberService, IDisposable
{
private readonly IEventAggregator m_eventAggregator;
private Dictionary<SubscriptionToken, Type> m_subscriptions;
public EventSubscriberService(IEventAggregator eventAggregator)
{
m_eventAggregator = eventAggregator;
m_subscriptions = new Dictionary<SubscriptionToken, Type>();
}
public void Subscribe<T, U>(string moduleName, Action<U> eventAction, ThreadOption threadOption) where T : PubSubEvent<U>, new()
{
var subscriptionToken = m_eventAggregator.GetEvent<T>().Subscribe(eventAction, threadOption, true);
m_subscriptions.Add(subscriptionToken, typeof(T));
}
public void Dispose()
{
foreach (var item in m_subscriptions)
{
//Do Something
}
}
}
}
using System;
using Microsoft.Practices.Prism.PubSubEvents;
namespace TestApp.Interfaces
{
public interface IEventSubscriberService
{
void Subscribe<T, U>(string moduleName, Action<U> eventAction, ThreadOption threadOption) where T : PubSubEvent<U>, new();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment