Skip to content

Commit 0170e3d

Browse files
committed
Revert NotifyAsync
1 parent f3176c6 commit 0170e3d

6 files changed

Lines changed: 41 additions & 75 deletions

File tree

src/Merq.Abstractions/IMessageBus.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public interface IMessageBus
103103
/// <param name="callerName">Optional calling member name, provided by default by the compiler.</param>
104104
/// <param name="callerFile">Optional calling file name, provided by default by the compiler.</param>
105105
/// <param name="callerLine">Optional calling line number, provided by default by the compiler.</param>
106-
Task NotifyAsync<TEvent>(TEvent e, [CallerMemberName] string? callerName = default, [CallerFilePath] string? callerFile = default, [CallerLineNumber] int? callerLine = default);
106+
ValueTask NotifyAsync<TEvent>(TEvent e, [CallerMemberName] string? callerName = default, [CallerFilePath] string? callerFile = default, [CallerLineNumber] int? callerLine = default);
107107

108108
/// <summary>
109109
/// Observes the events of a given type <typeparamref name="TEvent"/>.

src/Merq.Abstractions/IMessageBusExtensions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.ComponentModel;
33
using System.Runtime.CompilerServices;
4+
using System.Threading;
45
using System.Threading.Tasks;
56

67
namespace Merq;
@@ -17,7 +18,11 @@ public static class IMessageBusExtensions
1718
[EditorBrowsable(EditorBrowsableState.Never)]
1819
[Obsolete("Use await NotifyAsync instead.")]
1920
public static void Notify<TEvent>(this IMessageBus bus, TEvent e, [CallerMemberName] string? callerName = default, [CallerFilePath] string? callerFile = default, [CallerLineNumber] int? callerLine = default)
20-
=> bus.NotifyAsync(e, callerName, callerFile, callerLine).Forget();
21+
{
22+
var result = bus.NotifyAsync(e, callerName, callerFile, callerLine);
23+
while (!result.IsCompleted)
24+
Thread.SpinWait(1);
25+
}
2126

2227
/// <summary>
2328
/// Notifies the bus of a new event instance of <typeparamref name="TEvent"/>.
@@ -30,7 +35,7 @@ public static void Notify<TEvent>(this IMessageBus bus, TEvent e, [CallerMemberN
3035
/// <summary>
3136
/// Notifies the bus of a new event instance of <typeparamref name="TEvent"/>.
3237
/// </summary>
33-
public static Task NotifyAsync<TEvent>(this IMessageBus bus, [CallerMemberName] string? callerName = default, [CallerFilePath] string? callerFile = default, [CallerLineNumber] int? callerLine = default) where TEvent : new()
38+
public static ValueTask NotifyAsync<TEvent>(this IMessageBus bus, [CallerMemberName] string? callerName = default, [CallerFilePath] string? callerFile = default, [CallerLineNumber] int? callerLine = default) where TEvent : new()
3439
=> bus.NotifyAsync(new TEvent(), callerName, callerFile, callerLine);
3540

3641
/// <summary>

src/Merq.Abstractions/TaskExtensions.cs

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/Merq.Tests/DuckTyping.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,14 @@ public partial record OtherMessageEvent(string Message)
2121

2222
public class DynamicDuckTyping : DuckTyping
2323
{
24-
protected override IMessageBus CreateMessageBus(IServiceCollection? services = null)
25-
{
26-
services ??= new ServiceCollection();
27-
services.AddSingleton<IMessageBus>(sp => new DynamicallyMessageBus(sp));
28-
return services.BuildServiceProvider().GetRequiredService<IMessageBus>();
29-
}
24+
protected override IMessageBus CreateMessageBus(IServiceProvider? services = null) =>
25+
new DynamicallyMessageBus(services ?? new MockServiceProvider());
3026
}
3127

3228
public class AutoMapperDuckTyping : DuckTyping
3329
{
34-
protected override IMessageBus CreateMessageBus(IServiceCollection? services = null)
35-
{
36-
services ??= new ServiceCollection();
37-
services.AddSingleton<IMessageBus>(sp => new AutoMapperMessageBus(sp));
38-
return services.BuildServiceProvider().GetRequiredService<IMessageBus>();
39-
}
30+
protected override IMessageBus CreateMessageBus(IServiceProvider? services = null) =>
31+
new AutoMapperMessageBus(services ?? new MockServiceProvider());
4032

4133
[Fact]
4234
public void ExecuteWithExtraCtorArg()
@@ -45,7 +37,7 @@ public void ExecuteWithExtraCtorArg()
4537
var services = new ServiceCollection();
4638
services.AddSingleton(handler.Object);
4739
services.AddSingleton(typeof(IServiceCollection), _ => services);
48-
var bus = CreateMessageBus(services);
40+
var bus = CreateMessageBus(services.BuildServiceProvider());
4941

5042
var cmd = new Library2::Library.Echo2("Foo");
5143

@@ -57,7 +49,7 @@ public void ExecuteWithExtraCtorArg()
5749

5850
public abstract class DuckTyping
5951
{
60-
protected abstract IMessageBus CreateMessageBus(IServiceCollection? services = null);
52+
protected abstract IMessageBus CreateMessageBus(IServiceProvider? services = null);
6153

6254
#if NET6_0_OR_GREATER
6355
[Fact]
@@ -118,7 +110,7 @@ public void CanHandleDuck()
118110
var services = new ServiceCollection();
119111
services.AddSingleton<ICommandHandler<Library1::Library.Echo, string>, Library1::Library.EchoHandler>();
120112
services.AddSingleton(typeof(IServiceCollection), _ => services);
121-
var bus = CreateMessageBus(services);
113+
var bus = CreateMessageBus(services.BuildServiceProvider());
122114

123115
Assert.True(bus.CanHandle<Library2::Library.Echo>());
124116
Assert.True(bus.CanHandle(new Library2::Library.Echo("Foo")));
@@ -130,7 +122,7 @@ public void CanExecuteDuck()
130122
var services = new ServiceCollection();
131123
services.AddSingleton<ICommandHandler<Library1::Library.Echo, string>, Library1::Library.EchoHandler>();
132124
services.AddSingleton(typeof(IServiceCollection), _ => services);
133-
var bus = CreateMessageBus(services);
125+
var bus = CreateMessageBus(services.BuildServiceProvider());
134126

135127
var cmd = new Library2::Library.Echo("Foo");
136128

@@ -143,7 +135,7 @@ public void ExecuteCommand()
143135
var services = new ServiceCollection();
144136
services.AddSingleton<ICommandHandler<Library1::Library.Echo, string>, Library1::Library.EchoHandler>();
145137
services.AddSingleton(typeof(IServiceCollection), _ => services);
146-
var bus = CreateMessageBus(services);
138+
var bus = CreateMessageBus(services.BuildServiceProvider());
147139

148140
var cmd = new Library2::Library.Echo("Foo");
149141

@@ -158,7 +150,7 @@ public void ExecuteNoOpCommand()
158150
var services = new ServiceCollection();
159151
services.AddSingleton<ICommandHandler<Library1::Library.NoOp>, Library1::Library.NoOpHandler>();
160152
services.AddSingleton(typeof(IServiceCollection), _ => services);
161-
var bus = CreateMessageBus(services);
153+
var bus = CreateMessageBus(services.BuildServiceProvider());
162154

163155
var cmd = new Library2::Library.NoOp();
164156

@@ -171,7 +163,7 @@ public async Task ExecuteAsyncCommandAsync()
171163
var services = new ServiceCollection();
172164
services.AddSingleton<IAsyncCommandHandler<Library1::Library.EchoAsync, string>, Library1::Library.EchoAsyncHandler>();
173165
services.AddSingleton(typeof(IServiceCollection), _ => services);
174-
var bus = CreateMessageBus(services);
166+
var bus = CreateMessageBus(services.BuildServiceProvider());
175167

176168
var cmd = new Library2::Library.EchoAsync("Foo");
177169

@@ -186,7 +178,7 @@ public async Task ExecuteNoOpAsyncCommandAsync()
186178
var services = new ServiceCollection();
187179
services.AddSingleton<IAsyncCommandHandler<Library1::Library.NoOpAsync>, Library1::Library.NoOpAsyncHandler>();
188180
services.AddSingleton(typeof(IServiceCollection), _ => services);
189-
var bus = CreateMessageBus(services);
181+
var bus = CreateMessageBus(services.BuildServiceProvider());
190182

191183
var cmd = new Library2::Library.NoOpAsync();
192184

src/Merq/MessageBus.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public System.Collections.Generic.IAsyncEnumerable<TResult> ExecuteStream<TResul
324324
#endif
325325

326326
/// <inheritdoc/>
327-
public Task NotifyAsync<TEvent>(TEvent e, [CallerMemberName] string? callerName = default, [CallerFilePath] string? callerFile = default, [CallerLineNumber] int? callerLine = default)
327+
public ValueTask NotifyAsync<TEvent>(TEvent e, [CallerMemberName] string? callerName = default, [CallerFilePath] string? callerFile = default, [CallerLineNumber] int? callerLine = default)
328328
{
329329
var type = (e ?? throw new ArgumentNullException(nameof(e))).GetType();
330330
using var activity = StartEventActivity(type, e, callerName, callerFile, callerLine);
@@ -372,7 +372,7 @@ public Task NotifyAsync<TEvent>(TEvent e, [CallerMemberName] string? callerName
372372
Publishing.Record(watch.ElapsedMilliseconds, new Tag("Event", type.FullName));
373373
}
374374

375-
return Task.CompletedTask;
375+
return new ValueTask();
376376
}
377377

378378
/// <summary>

src/Samples/Library1/Commands.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,42 @@ public void Execute(NoOp command) { }
2424
}
2525

2626
[Service]
27-
public class EchoHandler(IMessageBus bus) : ICommandHandler<Echo, string>
27+
public class EchoHandler : ICommandHandler<Echo, string>
2828
{
29+
readonly IMessageBus? bus;
30+
31+
public EchoHandler() { }
32+
33+
public EchoHandler(IMessageBus bus) => this.bus = bus;
34+
2935
public bool CanExecute(Echo command) => !string.IsNullOrEmpty(command.Message);
3036

3137
public string Execute(Echo command)
3238
{
3339
if (string.IsNullOrEmpty(command.Message))
3440
throw new NotSupportedException("Cannot echo an empty or null message");
3541

36-
bus.NotifyAsync(new OnDidSay(command.Message)).Forget();
42+
bus?.Notify(new OnDidSay(command.Message));
3743
return command.Message;
3844
}
3945
}
4046

4147
[Service]
42-
public class EchoAsyncHandler(IMessageBus bus) : IAsyncCommandHandler<EchoAsync, string>
48+
public class EchoAsyncHandler : IAsyncCommandHandler<EchoAsync, string>
4349
{
50+
readonly IMessageBus? bus;
51+
52+
public EchoAsyncHandler() { }
53+
54+
public EchoAsyncHandler(IMessageBus bus) => this.bus = bus;
55+
56+
4457
public bool CanExecute(EchoAsync command) => true;
4558

46-
public async Task<string> ExecuteAsync(EchoAsync command, CancellationToken cancellation = default)
59+
public Task<string> ExecuteAsync(EchoAsync command, CancellationToken cancellation = default)
4760
{
48-
await bus.NotifyAsync(new OnDidSay(command.Message));
49-
return command.Message;
61+
bus?.Notify(new OnDidSay(command.Message));
62+
return Task.FromResult(command.Message);
5063
}
5164
}
5265

0 commit comments

Comments
 (0)