Radarr/NzbDrone.Common/Eventing/EventAggregator.cs

31 lines
917 B
C#
Raw Normal View History

2013-02-23 20:09:44 +00:00
using System.Collections.Generic;
using System.Linq;
2013-02-23 20:09:44 +00:00
using NLog;
2013-02-23 20:34:51 +00:00
using NzbDrone.Common.EnsureThat;
namespace NzbDrone.Common.Eventing
{
public class EventAggregator : IEventAggregator
{
2013-02-23 20:09:44 +00:00
private readonly Logger _logger;
private readonly IEnumerable<IHandle> _handlers;
2013-02-23 20:09:44 +00:00
public EventAggregator(Logger logger, IEnumerable<IHandle> handlers)
{
2013-02-23 20:34:51 +00:00
Ensure.That(() => handlers).HasItems();
2013-02-23 20:09:44 +00:00
_logger = logger;
_handlers = handlers;
}
public void Publish<TEvent>(TEvent message) where TEvent : IEvent
{
2013-02-23 20:09:44 +00:00
_logger.Trace("Publishing {0}", message.GetType().Name);
2013-02-23 20:09:44 +00:00
foreach (var handler in _handlers.OfType<IHandle<TEvent>>())
{
2013-02-23 20:09:44 +00:00
_logger.Trace("{0} => {1}", message.GetType().Name, handler.GetType().Name);
handler.Handle(message);
}
}
}
}