Skip to content

Commit

Permalink
Standardized error messages between convention and registration-base …
Browse files Browse the repository at this point in the history
…event routers.
  • Loading branch information
Oliver, Jonathan committed Jul 28, 2011
1 parent 271c862 commit cd09fe6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/proj/CommonDomain.Core/CommonDomain.Core.csproj
Expand Up @@ -45,6 +45,7 @@
<Compile Include="AggregateBase.cs" />
<Compile Include="ConflictDetector.cs" />
<Compile Include="ConventionEventRouter.cs" />
<Compile Include="ExtensionMethods.cs" />
<Compile Include="HandlerForDomainEventNotFoundException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RegistrationEventRouter.cs" />
Expand Down
11 changes: 4 additions & 7 deletions src/proj/CommonDomain.Core/ConventionEventRouter.cs
Expand Up @@ -8,6 +8,7 @@ namespace CommonDomain.Core
public class ConventionEventRouter<TEvent> : IRouteEvents<TEvent>
{
private readonly IDictionary<Type, Action<TEvent>> handlers = new Dictionary<Type, Action<TEvent>>();
private IAggregate registered;

public virtual void Register<TEventMessage>(Action<TEventMessage> handler) where TEventMessage : TEvent
{
Expand All @@ -22,6 +23,8 @@ public virtual void Register(IAggregate aggregate)
if (aggregate == null)
throw new ArgumentNullException("aggregate");

this.registered = aggregate;

// Get instance methods named Apply with one parameter returning void
var applyMethods = aggregate.GetType()
.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
Expand Down Expand Up @@ -52,13 +55,7 @@ public virtual void Dispatch(object eventMessage)
if (this.handlers.TryGetValue(eventMessage.GetType(), out handler))
handler((TEvent)eventMessage);
else
{
var message = string.Format(
"Cannot apply message to aggregate instance. The aggregate must define a method called void Apply({0} @event)",
eventMessage.GetType().Name);

throw new HandlerForDomainEventNotFoundException(message);
}
this.registered.ThrowHandlerNotFound(eventMessage);
}

private void Register(Type messageType, Action<TEvent> handler)
Expand Down
20 changes: 20 additions & 0 deletions src/proj/CommonDomain.Core/ExtensionMethods.cs
@@ -0,0 +1,20 @@
namespace CommonDomain.Core
{
using System.Globalization;

internal static class ExtensionMethods
{
public static string FormatWith(this string format, params object[] args)
{
return string.Format(CultureInfo.InvariantCulture, format ?? string.Empty, args);
}

public static void ThrowHandlerNotFound(this IAggregate aggregate, object eventMessage)
{
var exceptionMessage = "Aggregate of type '{0}' raised an event of type '{1}' but not handler could be found to handle the message."
.FormatWith(aggregate.GetType().Name, eventMessage.GetType().Name);

throw new HandlerForDomainEventNotFoundException(exceptionMessage);
}
}
}
21 changes: 9 additions & 12 deletions src/proj/CommonDomain.Core/RegistrationEventRouter.cs
Expand Up @@ -6,31 +6,28 @@ namespace CommonDomain.Core
public class RegistrationEventRouter<TEvent> : IRouteEvents<TEvent>
{
private readonly IDictionary<Type, Action<TEvent>> handlers = new Dictionary<Type, Action<TEvent>>();
private string aggregateTypeName;
private IAggregate regsitered;

public virtual void Register<TEventMessage>(Action<TEventMessage> handler) where TEventMessage : TEvent
{
this.handlers[typeof(TEventMessage)] = @event => handler((TEventMessage)@event);
}
public virtual void Register(IAggregate aggregate)
{
this.aggregateTypeName = aggregate.GetType().Name;
if (aggregate == null)
throw new ArgumentNullException("aggregate");

this.regsitered = aggregate;
}

public virtual void Dispatch(object eventMessage)
{
Action<TEvent> action;
Action<TEvent> handler;

if (!this.handlers.TryGetValue(eventMessage.GetType(), out action))
{
var message = string.Format(
"Aggregate of type {0} raised and event of type {1}, but no handler was registered.",
this.aggregateTypeName,
eventMessage.GetType().Name);
throw new HandlerForDomainEventNotFoundException(message);
}
if (!this.handlers.TryGetValue(eventMessage.GetType(), out handler))
this.regsitered.ThrowHandlerNotFound(eventMessage);

action((TEvent)eventMessage);
handler((TEvent)eventMessage);
}
}
}

0 comments on commit cd09fe6

Please sign in to comment.