Created
August 25, 2010 08:05
-
-
Save randomcodenz/549082 to your computer and use it in GitHub Desktop.
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 ExtensionMethods; | |
namespace Events | |
{ | |
public static class DomainEvents | |
{ | |
private static IEventHandlerResolver _handlerResolver; | |
public static bool IsInitialised() | |
{ | |
return _handlerResolver != null; | |
} | |
public static void Initialise( IEventHandlerResolver handlerResolver ) | |
{ | |
_handlerResolver = handlerResolver; | |
} | |
public static void Raise<T>( T args ) where T : IDomainEvent | |
{ | |
EnsureInitialised(); | |
var eventHandlers = _handlerResolver.ResolveAll<T>(); | |
eventHandlers.Do( handler => handler.Handle( args ) ); | |
} | |
public static void ResetHandlerResolver() | |
{ | |
_handlerResolver = null; | |
} | |
private static void EnsureInitialised() | |
{ | |
if( _handlerResolver == null ) | |
{ | |
throw new InvalidOperationException( "DomainEvents has not been initialised. Initialise( IEventHandlerResolver ) must be called prior to registering callbacks or raising events." ); | |
} | |
} | |
} | |
} |
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.Collections.Generic; | |
namespace Events | |
{ | |
public interface IEventHandlerResolver | |
{ | |
IEnumerable<IHandles<T>> ResolveAll<T>() where T : IDomainEvent; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment