Created
September 17, 2015 16:22
-
-
Save dadhi/87f375df2e90de40c202 to your computer and use it in GitHub Desktop.
DryIoc_Issue164_EventAggregator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using NUnit.Framework; | |
namespace DryIoc.IssuesTests | |
{ | |
[TestFixture] | |
public class Issue164_EventAggregatorImpl | |
{ | |
[Test] | |
public void Able_to_handle_multiple_events_being_singleton() | |
{ | |
var container = new Container(); | |
container.Register<IEventDispatcher, DryIocEventDispatcher>(); | |
container.RegisterMany<ScoringService>(Reuse.Singleton); | |
container.RegisterMany<RenderingService>(); | |
var eventDispatcher = container.Resolve<IEventDispatcher>(); | |
eventDispatcher.Dispatch(new ZoneDestroyedEventArgs()); | |
eventDispatcher.Dispatch(new ZoneCreatedEventArgs()); | |
} | |
public interface IEventDispatcher | |
{ | |
void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : EventArgs; | |
} | |
public class DryIocEventDispatcher : IEventDispatcher | |
{ | |
private readonly IResolver _resolver; | |
public DryIocEventDispatcher(IResolver resolver) | |
{ | |
_resolver = resolver; | |
} | |
public void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : EventArgs | |
{ | |
foreach (var handler in _resolver.ResolveMany<IHandles<TEvent>>()) | |
{ | |
handler.Handle(eventToDispatch); | |
} | |
} | |
} | |
public interface IHandles<T> where T : EventArgs | |
{ | |
void Handle(T args); | |
} | |
public class ZoneDestroyedEventArgs : EventArgs { } | |
public class ZoneCreatedEventArgs : EventArgs { } | |
public class ScoringService : IHandles<ZoneDestroyedEventArgs>, IHandles<ZoneCreatedEventArgs> | |
{ | |
public void Handle(ZoneDestroyedEventArgs args) | |
{ | |
Console.WriteLine("Scoring Service Handled ZoneDestroyed Event"); | |
} | |
public void Handle(ZoneCreatedEventArgs args) | |
{ | |
Console.WriteLine("Scoring Service Handled ZoneCreated Event"); | |
} | |
} | |
public class RenderingService : IHandles<ZoneDestroyedEventArgs>, IHandles<ZoneCreatedEventArgs> | |
{ | |
public void Handle(ZoneDestroyedEventArgs args) | |
{ | |
Console.WriteLine("Rendering Service Handled ZoneDestroyed Event"); | |
} | |
public void Handle(ZoneCreatedEventArgs args) | |
{ | |
Console.WriteLine("Rendering Service Handled ZoneCreated Event"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment