Skip to content

Instantly share code, notes, and snippets.

@joshrobb
Forked from randomcodenz/DomainEvents.cs
Created August 25, 2010 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshrobb/549090 to your computer and use it in GitHub Desktop.
Save joshrobb/549090 to your computer and use it in GitHub Desktop.
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." );
}
}
}
}
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