Skip to content

Commit

Permalink
Updated IAmACommandHandler Interface (#2313)
Browse files Browse the repository at this point in the history
  • Loading branch information
preardon authored Oct 11, 2022
1 parent 7522e45 commit 9af1304
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Paramore.Brighter/CommandProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,12 @@ public Guid DepositPost<T>(T request) where T : class, IRequest
/// database, that you want to signal via the request to downstream consumers
/// Pass deposited Guid to <see cref="ClearOutbox"/>
/// </summary>
/// <param name="request">The requests to save to the outbox</param>
/// <param name="requests">The requests to save to the outbox</param>
/// <typeparam name="T">The type of the request</typeparam>
/// <returns>The Id of the Message that has been deposited.</returns>
public Guid[] DepositPost<T>(IEnumerable<T> request) where T : class, IRequest
public Guid[] DepositPost<T>(IEnumerable<T> requests) where T : class, IRequest
{
return DepositPost(request, _boxTransactionConnectionProvider);
return DepositPost(requests, _boxTransactionConnectionProvider);
}

private Guid DepositPost<T>(T request, IAmABoxTransactionConnectionProvider connectionProvider) where T : class, IRequest
Expand Down
27 changes: 27 additions & 0 deletions src/Paramore.Brighter/IAmACommandProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ public interface IAmACommandProcessor
/// <returns></returns>
Guid DepositPost<T>(T request) where T : class, IRequest;

/// <summary>
/// Adds a messages into the outbox, and returns the id of the saved message.
/// Intended for use with the Outbox pattern: http://gistlabs.com/2014/05/the-outbox/ normally you include the
/// call to DepositPostBox within the scope of the transaction to write corresponding entity state to your
/// database, that you want to signal via the request to downstream consumers
/// Pass deposited Guid to <see cref="ClearOutbox"/>
/// </summary>
/// <param name="requests">The requests to save to the outbox</param>
/// <typeparam name="T">The type of the request</typeparam>
/// <returns>The Id of the Message that has been deposited.</returns>
public Guid[] DepositPost<T>(IEnumerable<T> requests) where T : class, IRequest;

/// <summary>
/// Adds a message into the outbox, and returns the id of the saved message.
/// Intended for use with the Outbox pattern: http://gistlabs.com/2014/05/the-outbox/ normally you include the
Expand All @@ -114,6 +126,21 @@ public interface IAmACommandProcessor
/// <returns></returns>
Task<Guid> DepositPostAsync<T>(T request, bool continueOnCapturedContext = false, CancellationToken cancellationToken = default(CancellationToken)) where T : class, IRequest;

/// <summary>
/// Adds a message into the outbox, and returns the id of the saved message.
/// Intended for use with the Outbox pattern: http://gistlabs.com/2014/05/the-outbox/ normally you include the
/// call to DepositPostBox within the scope of the transaction to write corresponding entity state to your
/// database, that you want to signal via the request to downstream consumers
/// Pass deposited Guid to <see cref="ClearOutboxAsync"/>
/// </summary>
/// <param name="requests">The requests to save to the outbox</param>
/// <param name="continueOnCapturedContext">Should we use the calling thread's synchronization context when continuing or a default thread synchronization context. Defaults to false</param>
/// <param name="cancellationToken">The Cancellation Token.</param>
/// <typeparam name="T">The type of the request</typeparam>
/// <returns></returns>
Task<Guid[]> DepositPostAsync<T>(IEnumerable<T> requests, bool continueOnCapturedContext = false,
CancellationToken cancellationToken = default(CancellationToken)) where T : class, IRequest;

/// <summary>
/// Flushes the message box message given by <param name="posts"> to the broker.
/// Intended for use with the Outbox pattern: http://gistlabs.com/2014/05/the-outbox/ <see cref="DepositPostBox"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ enum CommandType
Call
}

internal class SpyCommandProcessor : IAmACommandProcessor
internal class SpyCommandProcessor : Paramore.Brighter.IAmACommandProcessor
{
private readonly Queue<IRequest> _requests = new Queue<IRequest>();
private readonly Dictionary<Guid, IRequest> _postBox = new Dictionary<Guid, IRequest>();
Expand Down Expand Up @@ -113,6 +113,17 @@ public Guid DepositPost<T>(T request) where T : class, IRequest
return request.Id;
}

public Guid[] DepositPost<T>(IEnumerable<T> request) where T : class, IRequest
{
var ids = new List<Guid>();
foreach (T r in request)
{
ids.Add(DepositPost(r));
}

return ids.ToArray();
}

public async Task<Guid> DepositPostAsync<T>(T request, bool continueOnCapturedContext = false,
CancellationToken cancellationToken = default(CancellationToken)) where T : class, IRequest
{
Expand All @@ -123,6 +134,18 @@ public async Task<Guid> DepositPostAsync<T>(T request, bool continueOnCapturedCo
return await tcs.Task;
}

public async Task<Guid[]> DepositPostAsync<T>(IEnumerable<T> requests, bool continueOnCapturedContext = false,
CancellationToken cancellationToken = default(CancellationToken)) where T : class, IRequest
{
var ids = new List<Guid>();
foreach (T r in requests)
{
ids.Add(await DepositPostAsync(r, cancellationToken: cancellationToken));
}

return ids.ToArray();
}

public void ClearOutbox(params Guid[] posts)
{
foreach (var messageId in posts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ public Guid DepositPost<T>(T request) where T : class, IRequest
return request.Id;
}

public Guid[] DepositPost<T>(IEnumerable<T> request) where T : class, IRequest
{
var ids = new List<Guid>();
foreach (T r in request)
{
ids.Add(DepositPost(r));
}

return ids.ToArray();
}

public Task<Guid> DepositPostAsync<T>(T request, bool continueOnCapturedContext = false, CancellationToken cancellationToken = default(CancellationToken)) where T : class, IRequest
{
var tcs = new TaskCompletionSource<Guid>(TaskCreationOptions.RunContinuationsAsynchronously);
Expand All @@ -93,6 +104,18 @@ public Guid DepositPost<T>(T request) where T : class, IRequest

}

public async Task<Guid[]> DepositPostAsync<T>(IEnumerable<T> requests, bool continueOnCapturedContext = false,
CancellationToken cancellationToken = default(CancellationToken)) where T : class, IRequest
{
var ids = new List<Guid>();
foreach (T r in requests)
{
ids.Add(await DepositPostAsync(r, cancellationToken: cancellationToken));
}

return ids.ToArray();
}

public void ClearOutbox(params Guid[] posts)
{
foreach (var post in posts)
Expand Down

0 comments on commit 9af1304

Please sign in to comment.