Skip to content

Commit

Permalink
Add QueryMediatorExtensions, CommandMediatorExtensions, and `Even…
Browse files Browse the repository at this point in the history
…tMediatorExtensions` for backward compatibility
  • Loading branch information
litenova committed Jan 11, 2024
1 parent 8a66d02 commit c1c20d4
Show file tree
Hide file tree
Showing 58 changed files with 302 additions and 105 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v.0.23.1
- Add `QueryMediatorExtensions` for backward compatibility.
- Add `CommandMediatorExtensions` for backward compatibility.
- Add `EventMediatorExtensions` for backward compatibility.

## v.0.23.0
- Fix the missing `Exception` parameter in `IAsyncMessageErrorHandler[TMessage, TMessageResult]` and `IAsyncMessageErrorHandler[TMessage]` interfaces.

Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>latest</LangVersion>
<Authors>A. Shafie</Authors>
<PackageTags>mediator;cqrs</PackageTags>
<VersionPrefix>0.23.0</VersionPrefix>
<VersionPrefix>0.23.1</VersionPrefix>
<PackageIcon>icon.png</PackageIcon>
<Description>LiteBus is an easy-to-use and ambitious in-process mediator providing the foundation to implement Command Query Separation (CQS). It is implemented with minimal reflection and instead utilizes covariance and contravariance to provide its core functionality.</Description>
<PackageProjectUrl>https://github.com/litenova/LiteBus</PackageProjectUrl>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System.Threading;
using System.Threading.Tasks;

namespace LiteBus.Commands.Abstractions;

/// <summary>
/// Provides extension methods for <see cref="ICommandMediator"/> to simplify command sending operations.
/// </summary>
public static class CommandMediatorExtensions
{
/// <summary>
/// Sends a command asynchronously using the specified command mediator.
/// </summary>
/// <param name="commandMediator">The command mediator to use for sending the command.</param>
/// <param name="command">The command to send.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the send operation.</param>
/// <returns>A task representing the asynchronous send operation.</returns>
/// <example>
/// Usage example:
/// <code>
/// await commandMediator.SendAsync(myCommand, cancellationToken);
/// </code>
/// </example>
public static Task SendAsync(this ICommandMediator commandMediator,
ICommand command,
CancellationToken cancellationToken = default)
{
return commandMediator.SendAsync(command, null, cancellationToken);
}

/// <summary>
/// Sends a command asynchronously using the specified command mediator and returns a result.
/// </summary>
/// <typeparam name="TCommandResult">The type of the result returned by the command.</typeparam>
/// <param name="commandMediator">The command mediator to use for sending the command.</param>
/// <param name="command">The command to send.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the send operation.</param>
/// <returns>A task representing the asynchronous operation, containing the result of the command.</returns>
/// <example>
/// Usage example:
/// <code>
/// var result = await commandMediator.SendAsync(myCommand, cancellationToken);
/// </code>
/// </example>
public static Task<TCommandResult> SendAsync<TCommandResult>(this ICommandMediator commandMediator,
ICommand<TCommandResult> command,
CancellationToken cancellationToken = default)
{
return commandMediator.SendAsync(command, null, cancellationToken);
}

/// <summary>
/// Sends a tagged command asynchronously using the specified command mediator.
/// </summary>
/// <param name="commandMediator">The command mediator to use for sending the command.</param>
/// <param name="command">The command to send.</param>
/// <param name="tag">A tag that specifies the context or category of the command.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the send operation.</param>
/// <returns>A task representing the asynchronous send operation.</returns>
/// <example>
/// Usage example:
/// <code>
/// await commandMediator.SendAsync(myCommand, "UserAction", cancellationToken);
/// </code>
/// </example>
public static Task SendAsync(this ICommandMediator commandMediator,
ICommand command,
string tag,
CancellationToken cancellationToken = default)
{
return commandMediator.SendAsync(command,
new CommandMediationSettings
{
Filters =
{
Tags = new[] { tag }
}
},
cancellationToken);
}

/// <summary>
/// Sends a tagged command asynchronously using the specified command mediator and returns a result.
/// </summary>
/// <typeparam name="TCommandResult">The type of the result returned by the command.</typeparam>
/// <param name="commandMediator">The command mediator to use for sending the command.</param>
/// <param name="command">The command to send.</param>
/// <param name="tag">A tag that specifies the context or category of the command.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the send operation.</param>
/// <returns>A task representing the asynchronous operation, containing the result of the command.</returns>
/// <example>
/// Usage example:
/// <code>
/// var result = await commandMediator.SendAsync(myCommand, "UserAction", cancellationToken);
/// </code>
/// </example>
public static Task<TCommandResult> SendAsync<TCommandResult>(this ICommandMediator commandMediator,
ICommand<TCommandResult> command,
string tag,
CancellationToken cancellationToken = default)
{
return commandMediator.SendAsync(command,
new CommandMediationSettings
{
Filters =
{
Tags = new[] { tag }
}
},
cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http:https://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http:https://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=extensions/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Threading;
using System.Threading.Tasks;

namespace LiteBus.Events.Abstractions;

/// <summary>
/// Provides extension methods for <see cref="IEventMediator"/> for simplified event publishing.
/// </summary>
public static class EventMediatorExtensions
{
/// <summary>
/// Publishes an event asynchronously using the specified event mediator.
/// </summary>
/// <param name="eventMediator">The event mediator to use for publishing the event.</param>
/// <param name="event">The event to publish.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the publish operation.</param>
/// <returns>A task that represents the asynchronous publish operation.</returns>
/// <example>
/// Usage example:
/// <code>
/// await eventMediator.PublishAsync(myEvent, cancellationToken);
/// </code>
/// </example>
public static Task PublishAsync(this IEventMediator eventMediator, IEvent @event, CancellationToken cancellationToken = default)
{
return eventMediator.PublishAsync(@event, null, cancellationToken);
}

/// <summary>
/// Publishes an event asynchronously using the specified event mediator with a specific tag.
/// </summary>
/// <param name="eventMediator">The event mediator to use for publishing the event.</param>
/// <param name="event">The event to publish.</param>
/// <param name="tag">A tag that specifies the context or category of the event.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the publish operation.</param>
/// <returns>A task that represents the asynchronous publish operation.</returns>
/// <example>
/// Usage example:
/// <code>
/// await eventMediator.PublishAsync(myEvent, "UserAction", cancellationToken);
/// </code>
/// </example>
public static Task PublishAsync(this IEventMediator eventMediator, IEvent @event, string tag, CancellationToken cancellationToken = default)
{
return eventMediator.PublishAsync(@event,
new EventMediationSettings
{
Filters =
{
Tags = new[] { tag }
}
},
cancellationToken);
}
}
2 changes: 1 addition & 1 deletion src/LiteBus.Events.Abstractions/IEventMediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ public interface IEventMediator
/// <param name="cancellationToken">Cancellation token for the operation.</param>
/// <returns>A task representing the asynchronous event publication operation.</returns>
Task PublishAsync<TEvent>(TEvent @event, EventMediationSettings? eventMediationSettings = null, CancellationToken cancellationToken = default) where TEvent : notnull;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http:https://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http:https://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=extensions/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Security.Principal;
using System.Threading.Tasks;
using LiteBus.Events.Abstractions;
using LiteBus.Messaging.Abstractions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;

Expand Down
121 changes: 121 additions & 0 deletions src/LiteBus.Queries.Abstractions/Extensions/QueryMediatorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace LiteBus.Queries.Abstractions;

/// <summary>
/// Provides extension methods for <see cref="IQueryMediator"/> to simplify querying and streaming operations.
/// </summary>
public static class QueryMediatorExtensions
{
/// <summary>
/// Executes a query asynchronously using the specified query mediator.
/// </summary>
/// <typeparam name="TQueryResult">The type of the result returned by the query.</typeparam>
/// <param name="queryMediator">The query mediator to use for executing the query.</param>
/// <param name="query">The query to execute.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the query operation.</param>
/// <returns>A task representing the asynchronous operation, containing the result of the query.</returns>
/// <example>
/// Usage example:
/// <code>
/// var result = await queryMediator.QueryAsync(myQuery, cancellationToken);
/// </code>
/// </example>
public static Task<TQueryResult> QueryAsync<TQueryResult>(this IQueryMediator queryMediator,
IQuery<TQueryResult> query,
CancellationToken cancellationToken = default)
{
return queryMediator.QueryAsync(@query, null, cancellationToken);
}

/// <summary>
/// Streams query results asynchronously using the specified query mediator.
/// </summary>
/// <typeparam name="TQueryResult">The type of the results returned by the stream query.</typeparam>
/// <param name="queryMediator">The query mediator to use for executing the stream query.</param>
/// <param name="query">The stream query to execute.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the stream operation.</param>
/// <returns>An async enumerable representing the stream of results.</returns>
/// <example>
/// Usage example:
/// <code>
/// await foreach (var item in queryMediator.StreamAsync(myStreamQuery, cancellationToken))
/// {
/// // Process each item
/// }
/// </code>
/// </example>
public static IAsyncEnumerable<TQueryResult> StreamAsync<TQueryResult>(this IQueryMediator queryMediator,
IStreamQuery<TQueryResult> query,
CancellationToken cancellationToken = default)
{
return queryMediator.StreamAsync(query, null, cancellationToken);
}

/// <summary>
/// Executes a tagged query asynchronously using the specified query mediator.
/// </summary>
/// <typeparam name="TQueryResult">The type of the result returned by the query.</typeparam>
/// <param name="queryMediator">The query mediator to use for executing the query.</param>
/// <param name="query">The query to execute.</param>
/// <param name="tag">A tag that specifies the context or category of the query.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the query operation.</param>
/// <returns>A task representing the asynchronous operation, containing the result of the query.</returns>
/// <example>
/// Usage example:
/// <code>
/// var result = await queryMediator.QueryAsync(myQuery, "UserQuery", cancellationToken);
/// </code>
/// </example>
public static Task<TQueryResult> QueryAsync<TQueryResult>(this IQueryMediator queryMediator,
IQuery<TQueryResult> query,
string tag,
CancellationToken cancellationToken = default)
{
return queryMediator.QueryAsync(@query,
new QueryMediationSettings
{
Filters =
{
Tags = new[] { tag }
}
},
cancellationToken);
}

/// <summary>
/// Streams tagged query results asynchronously using the specified query mediator.
/// </summary>
/// <typeparam name="TQueryResult">The type of the results returned by the stream query.</typeparam>
/// <param name="queryMediator">The query mediator to use for executing the stream query.</param>
/// <param name="query">The stream query to execute.</param>
/// <param name="tag">A tag that specifies the context or category of the query.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the stream operation.</param>
/// <returns>An async enumerable representing the stream of results.</returns>
/// <example>
/// Usage example:
/// <code>
/// await foreach (var item in queryMediator.StreamAsync(myStreamQuery, "UserQueryStream", cancellationToken))
/// {
/// // Process each item
/// }
/// </code>
/// </example>
public static IAsyncEnumerable<TQueryResult> StreamAsync<TQueryResult>(this IQueryMediator queryMediator,
IStreamQuery<TQueryResult> query,
string tag,
CancellationToken cancellationToken = default)
{
return queryMediator.StreamAsync(query,
new QueryMediationSettings
{
Filters =
{
Tags = new[] { tag }
}
},
cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http:https://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http:https://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=extensions/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using LiteBus.Commands.Abstractions;
using LiteBus.Messaging.Abstractions;

namespace LiteBus.Commands.UnitTests.UseCases.CommandWithTag;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Threading;
using System.Threading.Tasks;
using LiteBus.Commands.Abstractions;
using LiteBus.UnitTests.Data.FakeCommand.Messages;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace LiteBus.UnitTests.Data.FakeCommand.Messages;

public sealed class FakeCommandResult
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Threading;
using System.Threading.Tasks;
using LiteBus.Commands.Abstractions;
using LiteBus.UnitTests.Data.FakeCommand.Messages;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Threading;
using System.Threading.Tasks;
using LiteBus.Commands.Abstractions;

namespace LiteBus.UnitTests.Data.FakeCommand.PreHandlers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Threading;
using System.Threading.Tasks;
using LiteBus.Events.Abstractions;

namespace LiteBus.UnitTests.Data.FakeEvent.Handlers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Threading;
using System.Threading.Tasks;
using LiteBus.Events.Abstractions;

namespace LiteBus.UnitTests.Data.FakeEvent.Handlers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Threading;
using System.Threading.Tasks;
using LiteBus.Events.Abstractions;

namespace LiteBus.UnitTests.Data.FakeEvent.Handlers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Threading;
using System.Threading.Tasks;
using LiteBus.Events.Abstractions;

namespace LiteBus.UnitTests.Data.FakeEvent.PostHandlers;
Expand Down
Loading

0 comments on commit c1c20d4

Please sign in to comment.