2013-02-23 20:09:44 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-02-19 02:37:16 +00:00
|
|
|
|
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;
|
2013-02-19 02:37:16 +00:00
|
|
|
|
|
|
|
|
|
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-19 02:37:16 +00:00
|
|
|
|
|
2013-02-23 20:09:44 +00:00
|
|
|
|
public EventAggregator(Logger logger, IEnumerable<IHandle> handlers)
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-02-23 20:34:51 +00:00
|
|
|
|
Ensure.That(() => handlers).HasItems();
|
2013-02-23 20:09:44 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_handlers = handlers;
|
2013-02-19 02:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 19:18:48 +00:00
|
|
|
|
public void Publish<TEvent>(TEvent message) where TEvent : IEvent
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-02-23 20:09:44 +00:00
|
|
|
|
_logger.Trace("Publishing {0}", message.GetType().Name);
|
2013-02-19 02:37:16 +00:00
|
|
|
|
|
2013-02-23 20:09:44 +00:00
|
|
|
|
foreach (var handler in _handlers.OfType<IHandle<TEvent>>())
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-02-23 20:09:44 +00:00
|
|
|
|
_logger.Trace("{0} => {1}", message.GetType().Name, handler.GetType().Name);
|
|
|
|
|
handler.Handle(message);
|
2013-02-19 02:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|