Skip to content

Commit

Permalink
Improve performance of reporting actor by removing type check on inte…
Browse files Browse the repository at this point in the history
…rface
  • Loading branch information
Galad committed Aug 8, 2020
1 parent 1cfd17e commit a7c5201
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
14 changes: 0 additions & 14 deletions src/Tranquire/IThenAction.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Tranquire/Reporting/INotificationContentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public IActionNotificationContent CreateAfter(TimeSpan time)

public IActionNotificationContent CreateBefore<T>(INamed action, DateTimeOffset date, CommandType commandType)
{
return new BeforeThenNotificationContent(date, (action as IThenAction<T>).Question);
return new BeforeThenNotificationContent(date, (action as ThenAction<T>).Question);
}

public IActionNotificationContent CreateError(Exception error, TimeSpan time)
Expand Down
6 changes: 3 additions & 3 deletions src/Tranquire/Reporting/ReportingActor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Tranquire.Reporting
{
Expand Down Expand Up @@ -144,11 +145,10 @@ private TResult ExecuteNotifyingAction<TResult>(
}

}
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member

#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
private static INotificationContentFactory GetNotificationContentFactories<T>(INamed action)
{
if (action is IThenAction<T>)
if (action is ThenAction<T>)
{
return _thenNotificationContentFactory;
}
Expand Down
23 changes: 16 additions & 7 deletions src/Tranquire/ThenAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@

namespace Tranquire
{

/// <summary>
/// Represent an action that verifies the answer of a question
/// Represent the base class for an action that verifies the answer of a question
/// </summary>
/// <typeparam name="T">The question answer type</typeparam>
/// <typeparam name="TResult">The result type</typeparam>
public sealed class ThenAction<T, TResult> : ActionBase<TResult>, IThenAction<TResult>
public abstract class ThenAction<TResult> : ActionBase<TResult>
{
/// <summary>
/// Gets the question
/// </summary>
INamed IThenAction<TResult>.Question => Question;
public abstract INamed Question { get; }
}

/// <summary>
/// Represent an action that verifies the answer of a question
/// </summary>
/// <typeparam name="T">The question answer type</typeparam>
/// <typeparam name="TResult">The result type</typeparam>
public sealed class ThenAction<T, TResult> : ThenAction<TResult>
{
/// <summary>
/// Gets the question
/// </summary>
public IQuestion<T> Question { get; }
public override INamed Question { get; }

/// <summary>
/// Gets the action that verifies the outcome
/// </summary>
public Func<T, TResult> VerifyAction { get; }

private readonly IQuestion<T> _question;

/// <summary>
/// Creates a new instance of <see cref="ThenAction{T, TResult}"/>
/// </summary>
Expand All @@ -34,6 +42,7 @@ public ThenAction(IQuestion<T> question, Func<T, TResult> verifyAction)
{
Question = question ?? throw new ArgumentNullException(nameof(question));
VerifyAction = verifyAction ?? throw new ArgumentNullException(nameof(verifyAction));
_question = question;
}

/// <inheritdoc />
Expand All @@ -42,7 +51,7 @@ public ThenAction(IQuestion<T> question, Func<T, TResult> verifyAction)
/// <inheritdoc />
protected override TResult ExecuteWhen(IActor actor)
{
var answer = actor.AsksFor(Question);
var answer = actor.AsksFor(_question);
return VerifyAction(answer);
}
}
Expand Down

0 comments on commit a7c5201

Please sign in to comment.