Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ jobs:
if: github.event_name != 'pull_request'
run: dotnet test --no-build -c Release --logger trx --collect:"XPlat Code Coverage"

# Microsoft.Extensions.DependencyInjection 8.0.0 resolves the Abstractions 8.0.1 floor that src/RequestFlow declares.
# The separate artifacts path keeps this build away from the outputs Pack reads.
- name: Test at the dependency floor
run: >
dotnet test tests/RequestFlow.Tests.Unit -c Release -f net8.0
-p:DependencyInjectionVersion=8.0.0
--artifacts-path artifacts/dependency-floor

- name: Pack
run: dotnet pack --no-build -c Release -o artifacts/packages

Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). Version

The `release` workflow uses the section matching the pushed tag as the GitHub Release body. A missing section fails the build before publishing. Before tagging, rename `[Unreleased]` to the release version and add its date.

## [Unreleased]

### Fixed

- `AddRequestFlow` no longer throws `InvalidOperationException` ("This service descriptor is keyed") when a keyed service is registered first and the application resolves `Microsoft.Extensions.DependencyInjection.Abstractions` 8.0.0 or 8.0.1, on .NET Framework as well as .NET.
- An interface declared as an event publish strategy for more than one target reports `RF0013` once. The repeat no longer adds `RF0014`.
- `ICommandDispatcher` and `IQueryDispatcher` resolve to separate classes, so a query dispatcher can no longer be cast to a command dispatcher. The ValueTask pair is split the same way.
- The detached strategy sample in [events.md](docs/events.md#detach-entry-work-from-the-publishing-token) takes `IHostApplicationLifetime`, which a hosted application registers. The earlier constructor took a `CancellationToken`, which the container cannot resolve.

### Performance

- Publication allocates less for a `ParallelPublishStrategy` event with no applicable handler. Cancellation and completion behavior are unchanged.

## [1.0.0] - 2026-10-17

First stable release.
Expand Down Expand Up @@ -176,6 +189,7 @@ First public preview.
- `RequestFlow.Cqrs.Abstractions` and `RequestFlow.Cqrs`: command and query contracts with typed dispatchers, registered through `AddCqrs`, for codebases that want the split enforced by the compiler.
- `provider.ValidateRequestFlow()` to force validation at startup instead of at the first dispatch.

[Unreleased]: https://github.com/illia1f/RequestFlow/compare/v1.0.0...HEAD
[1.0.0]: https://github.com/illia1f/RequestFlow/compare/v1.0.0-preview.9...v1.0.0
[1.0.0-preview.9]: https://github.com/illia1f/RequestFlow/compare/v1.0.0-preview.8...v1.0.0-preview.9
[1.0.0-preview.8]: https://github.com/illia1f/RequestFlow/compare/v1.0.0-preview.7...v1.0.0-preview.8
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Supports Task and ValueTask requests, stages, streams, and events. Additionally,
> dotnet add package RequestFlow
> ```

See the [1.0.0 release notes](https://github.com/illia1f/RequestFlow/blob/main/CHANGELOG.md#100---2026-10-14) for changes since preview.9.
See the [1.0.0 release notes](https://github.com/illia1f/RequestFlow/blob/main/CHANGELOG.md#100---2026-10-17) for changes since preview.9.

**[Getting started: define a request, register its handler, and send it](https://github.com/illia1f/RequestFlow/blob/main/docs/getting-started.md).**

Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Anything past v1 is direction, not commitment.

Release: [1.0.0](CHANGELOG.md#100---2026-10-14).
Release: [1.0.0](CHANGELOG.md#100---2026-10-17).

## v1.0

Expand Down
6 changes: 4 additions & 2 deletions docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public async Task PublishAsync(
`CancellationToken.None` means "use the publishing token" for `StartAsync` and `RunAsync`. Pass a real strategy-owned or application-lifetime token to detach work:

```csharp
public sealed class DetachedStrategy(CancellationToken lifetimeToken)
public sealed class DetachedStrategy(IHostApplicationLifetime lifetime)
: IEventPublishStrategy
{
public async Task PublishAsync(
Expand All @@ -211,7 +211,7 @@ public sealed class DetachedStrategy(CancellationToken lifetimeToken)
throw delivery.Canceled(null, delivery.Count, cancellationToken);

for (int i = 0; i < delivery.Count; i++)
_ = ObserveAsync(delivery, i, lifetimeToken);
_ = ObserveAsync(delivery, i, lifetime.ApplicationStopping);

await Task.CompletedTask;
}
Expand All @@ -229,6 +229,8 @@ public sealed class DetachedStrategy(CancellationToken lifetimeToken)
}
```

`IHostApplicationLifetime` is in `Microsoft.Extensions.Hosting`, and only a host registers it: the Generic Host or ASP.NET Core. Without a host, inject a service of your own that exposes the token.

Detached work may outlive the publishing scope. Do not use this pattern when handlers depend on scoped services that can be disposed after `PublishAsync` returns.

## Which handlers receive an event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace RequestFlow.Cqrs;

/// <summary>
/// Default <see cref="ICommandDispatcher"/> and <see cref="IQueryDispatcher"/> forwarding to <see cref="IRequestDispatcher"/>.
/// </summary>
internal sealed class CqrsDispatcher(IRequestDispatcher dispatcher) : ICommandDispatcher, IQueryDispatcher
internal sealed class CqrsCommandDispatcher(IRequestDispatcher dispatcher) : ICommandDispatcher
{
private readonly IRequestDispatcher _dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));

Expand All @@ -26,12 +23,4 @@ public Task SendAsync(ICommand command, CancellationToken cancellationToken = de

return _dispatcher.SendAsync(command, cancellationToken);
}

/// <inheritdoc />
public Task<TResponse> SendAsync<TResponse>(IQuery<TResponse> query, CancellationToken cancellationToken = default)
{
ThrowHelper.ThrowIfNull(query);

return _dispatcher.SendAsync(query, cancellationToken);
}
}
18 changes: 18 additions & 0 deletions src/RequestFlow.Cqrs/CqrsQueryDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace RequestFlow.Cqrs;

internal sealed class CqrsQueryDispatcher(IRequestDispatcher dispatcher) : IQueryDispatcher
{
private readonly IRequestDispatcher _dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));

/// <inheritdoc />
public Task<TResponse> SendAsync<TResponse>(IQuery<TResponse> query, CancellationToken cancellationToken = default)
{
ThrowHelper.ThrowIfNull(query);

return _dispatcher.SendAsync(query, cancellationToken);
}
}
8 changes: 4 additions & 4 deletions src/RequestFlow.Cqrs/CqrsRequestFlowBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public static RequestFlowBuilder AddCqrs(this RequestFlowBuilder builder)
if (builder is null)
throw new ArgumentNullException(nameof(builder));

builder.Services.TryAddTransient<ICommandDispatcher, CqrsDispatcher>();
builder.Services.TryAddTransient<IQueryDispatcher, CqrsDispatcher>();
builder.Services.TryAddTransient<IValueCommandDispatcher, CqrsValueDispatcher>();
builder.Services.TryAddTransient<IValueQueryDispatcher, CqrsValueDispatcher>();
builder.Services.TryAddTransient<ICommandDispatcher, CqrsCommandDispatcher>();
builder.Services.TryAddTransient<IQueryDispatcher, CqrsQueryDispatcher>();
builder.Services.TryAddTransient<IValueCommandDispatcher, CqrsValueCommandDispatcher>();
builder.Services.TryAddTransient<IValueQueryDispatcher, CqrsValueQueryDispatcher>();
builder.Services.TryAddTransient<IStreamQueryDispatcher, CqrsStreamDispatcher>();

builder.Services.TryAddEnumerable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace RequestFlow.Cqrs;

internal sealed class CqrsValueDispatcher(IValueRequestDispatcher dispatcher)
: IValueCommandDispatcher, IValueQueryDispatcher
internal sealed class CqrsValueCommandDispatcher(IValueRequestDispatcher dispatcher)
: IValueCommandDispatcher
{
private readonly IValueRequestDispatcher _dispatcher =
dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
Expand All @@ -29,14 +29,4 @@ public ValueTask SendAsync(

return _dispatcher.SendAsync(command, cancellationToken);
}

/// <inheritdoc />
public ValueTask<TResponse> SendAsync<TResponse>(
IValueQuery<TResponse> query,
CancellationToken cancellationToken = default)
{
ThrowHelper.ThrowIfNull(query);

return _dispatcher.SendAsync(query, cancellationToken);
}
}
22 changes: 22 additions & 0 deletions src/RequestFlow.Cqrs/CqrsValueQueryDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace RequestFlow.Cqrs;

internal sealed class CqrsValueQueryDispatcher(IValueRequestDispatcher dispatcher)
: IValueQueryDispatcher
{
private readonly IValueRequestDispatcher _dispatcher =
dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));

/// <inheritdoc />
public ValueTask<TResponse> SendAsync<TResponse>(
IValueQuery<TResponse> query,
CancellationToken cancellationToken = default)
{
ThrowHelper.ThrowIfNull(query);

return _dispatcher.SendAsync(query, cancellationToken);
}
}
2 changes: 1 addition & 1 deletion src/RequestFlow/Events/EventPlanFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static EventPlan CreatePlan(
if (strategyType == typeof(SequentialPublishStrategy))
return new SequentialEventPlan(@event, entries);
if (strategyType == typeof(ParallelPublishStrategy))
return entries.Length == 1
return entries.Length <= 1
? new SequentialEventPlan(@event, entries)
: new ParallelEventPlan(@event, entries);
if (strategyType == typeof(FailFastPublishStrategy))
Expand Down
6 changes: 5 additions & 1 deletion src/RequestFlow/Registration/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,12 @@ private static RequestFlowRegistry GetOrAddRegistry(IServiceCollection services)
{
foreach (var descriptor in services)
{
if (descriptor.ImplementationInstance is RequestFlowRegistry existing)
// ImplementationInstance throws on a keyed descriptor in Microsoft.Extensions.DependencyInjection.Abstractions 8.0.0 and 8.0.1.
if (descriptor.ServiceType == typeof(RequestFlowRegistry)
&& descriptor.ImplementationInstance is RequestFlowRegistry existing)
{
return existing;
}
}

var registry = new RequestFlowRegistry();
Expand Down
5 changes: 4 additions & 1 deletion src/RequestFlow/Validation/Rules/EventStrategyRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ private static IEnumerable<RequestFlowValidationProblem> FindShapeProblems(
foreach (EventStrategyModel strategy in strategies)
{
Type strategyType = strategy.StrategyType;
if (strategyType.IsInterface && reportedInterfaces.Add(strategyType))
if (strategyType.IsInterface)
{
if (!reportedInterfaces.Add(strategyType))
continue;

yield return new RequestFlowValidationProblem(
ProblemCodes.EventStrategyIsInterface,
$"Event publish strategy '{strategyType.FullName}' is an interface, so the " +
Expand Down
3 changes: 2 additions & 1 deletion tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<!-- ValidationFixtures lives here too, but it is a scanned library rather than a test host, so it must not pull in the runner. -->
<IsUnitTestProject Condition="'$(IsUnitTestProject)' == '' And $(MSBuildProjectName.EndsWith('.Tests.Unit'))">true</IsUnitTestProject>
<DependencyInjectionVersion Condition="'$(DependencyInjectionVersion)' == ''">10.0.9</DependencyInjectionVersion>
</PropertyGroup>

<ItemGroup Condition="'$(IsUnitTestProject)' == 'true'">
<PackageReference Include="coverlet.collector" Version="10.0.1" PrivateAssets="all" />
<PackageReference Include="coverlet.msbuild" Version="10.0.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.9" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(DependencyInjectionVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.7.0" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="Shouldly" Version="4.3.0" />
Expand Down
18 changes: 16 additions & 2 deletions tests/RequestFlow.Cqrs.Tests.Unit/AddCqrsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ public async Task Given_Registered_Cqrs_When_Sending_Value_Query_Then_Returns_Va
result.ShouldBe("found 42");
}

[Fact]
public void Given_Registered_Cqrs_When_Resolving_Task_Dispatchers_Then_Neither_Implements_The_Other_Side()
{
_queries.ShouldNotBeAssignableTo<ICommandDispatcher>();
_commands.ShouldNotBeAssignableTo<IQueryDispatcher>();
}

[Fact]
public void Given_Registered_Cqrs_When_Resolving_Value_Dispatchers_Then_Neither_Implements_The_Other_Side()
{
_valueQueries.ShouldNotBeAssignableTo<IValueCommandDispatcher>();
_valueCommands.ShouldNotBeAssignableTo<IValueQueryDispatcher>();
}

[Fact]
public async Task Given_Registered_Cqrs_When_Streaming_Query_Then_Yields_Query_Handler_Items()
{
Expand Down Expand Up @@ -232,11 +246,11 @@ public void Given_Registered_Request_Flow_When_Registering_Cqrs_Then_Dispatchers
d.ServiceType == typeof(IStreamQueryDispatcher) && d.Lifetime == ServiceLifetime.Transient);
services.ShouldContain(d =>
d.ServiceType == typeof(IValueCommandDispatcher)
&& d.ImplementationType == typeof(CqrsValueDispatcher)
&& d.ImplementationType == typeof(CqrsValueCommandDispatcher)
&& d.Lifetime == ServiceLifetime.Transient);
services.ShouldContain(d =>
d.ServiceType == typeof(IValueQueryDispatcher)
&& d.ImplementationType == typeof(CqrsValueDispatcher)
&& d.ImplementationType == typeof(CqrsValueQueryDispatcher)
&& d.Lifetime == ServiceLifetime.Transient);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace RequestFlow.Cqrs.Tests.Unit;

public sealed class CqrsDispatcherTests
public sealed class CqrsCommandDispatcherTests
{
[Fact]
public async Task Given_Typed_Command_When_Sending_Command_Then_Forwards_Command_And_Token_And_Returns_The_Same_Task()
Expand Down Expand Up @@ -36,21 +36,6 @@ public async Task Given_Void_Command_When_Sending_Command_Then_Forwards_To_Void_
await _requests.Received(1).SendAsync((IRequest)command, cts.Token);
}

[Fact]
public async Task Given_Query_When_Sending_Query_Then_Forwards_Query_And_Token_And_Returns_The_Same_Task()
{
var query = new FindName("42");
using var cts = new CancellationTokenSource();
Task<string> expected = Task.FromResult("bob");
_requests.SendAsync(query, cts.Token).Returns(expected);

Task<string> result = _sut.SendAsync(query, cts.Token);

result.ShouldBeSameAs(expected);
(await result).ShouldBe("bob");
await _requests.Received(1).SendAsync(query, cts.Token);
}

[Fact]
public void Given_Null_Typed_Command_When_Sending_Command_Then_Throws_Argument_Null_Exception()
{
Expand All @@ -69,30 +54,21 @@ public void Given_Null_Void_Command_When_Sending_Command_Then_Throws_Argument_Nu
exception.ParamName.ShouldBe("command");
}

[Fact]
public void Given_Null_Query_When_Sending_Query_Then_Throws_Argument_Null_Exception()
{
var exception = Should.Throw<ArgumentNullException>(
() => { _sut.SendAsync((IQuery<string>)null!); });

exception.ParamName.ShouldBe("query");
}

[Fact]
public void Given_Null_Request_Dispatcher_When_Creating_Dispatcher_Then_Throws_Argument_Null_Exception()
{
Should.Throw<ArgumentNullException>(() => new CqrsDispatcher(null!));
Should.Throw<ArgumentNullException>(() => new CqrsCommandDispatcher(null!));
}

#region Initialization

private readonly IRequestDispatcher _requests;
private readonly CqrsDispatcher _sut;
private readonly CqrsCommandDispatcher _sut;

public CqrsDispatcherTests()
public CqrsCommandDispatcherTests()
{
_requests = Substitute.For<IRequestDispatcher>();
_sut = new CqrsDispatcher(_requests);
_sut = new CqrsCommandDispatcher(_requests);
}

#endregion
Expand All @@ -104,8 +80,6 @@ public sealed record Rename(string Name) : ICommand<string>;

public sealed record Purge : ICommand;

public sealed record FindName(string Id) : IQuery<string>;

// The assembly scan demands one handler per request type, even for requests only mocked here.
public sealed class RenameHandler : ICommandHandler<Rename, string>
{
Expand All @@ -119,11 +93,5 @@ public Task HandleAsync(Purge request, CancellationToken cancellationToken)
=> Task.CompletedTask;
}

public sealed class FindNameHandler : IQueryHandler<FindName, string>
{
public Task<string> HandleAsync(FindName request, CancellationToken cancellationToken)
=> Task.FromResult(request.Id);
}

#endregion
}
Loading