-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More perf work, more tests, docs and sample
- Loading branch information
1 parent
5e9081b
commit 1a0be21
Showing
15 changed files
with
630 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project> | ||
<Import Project="..\Directory.Build.props" /> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CSharpier.MsBuild" Version="0.27.3"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
samples/basic/NotificationPublisher/NotificationPublisher.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>disable</ImplicitUsings> | ||
<!-- Tells the compiler to emit the code generated by Mediator.SourceGenerator as files in the project --> | ||
<!-- This is useful for debugging purposes --> | ||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> | ||
<!-- The path where the generated files will be placed --> | ||
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<!-- Tells the compiler to ignore the generated files when compiling the project (it will still be part of the compilation) --> | ||
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" /> | ||
<None Include="$(CompilerGeneratedFilesOutputPath)/**/*.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(DotNetVersion)" /> | ||
</ItemGroup> | ||
|
||
<!-- Use Mediator from local git repo --> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Mediator.SourceGenerator\Mediator.SourceGenerator.csproj" OutputItemType="Analyzer" /> | ||
<ProjectReference Include="..\..\..\src\Mediator\Mediator.csproj" /> | ||
</ItemGroup> | ||
<!-- Uncomment below to use Mediator from NuGet --> | ||
<!-- <ItemGroup> | ||
<PackageReference Include="Mediator.SourceGenerator" Version="3.0.0-*"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Mediator.Abstractions" Version="3.0.0-*" /> | ||
</ItemGroup> --> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Mediator; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
var services = new ServiceCollection(); | ||
|
||
services.AddMediator(options => | ||
{ | ||
options.NotificationPublisherType = typeof(MyNotificationPublisher); | ||
}); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var mediator = serviceProvider.GetRequiredService<IMediator>(); | ||
|
||
var id = Guid.NewGuid(); | ||
var notification = new Notification(id); | ||
|
||
Console.WriteLine("Publishing!"); | ||
Console.WriteLine("-----------------------------------"); | ||
|
||
await mediator.Publish(notification); | ||
|
||
Console.WriteLine("-----------------------------------"); | ||
Console.WriteLine("Finished publishing!"); | ||
|
||
return 0; | ||
|
||
// | ||
// Here are the types used | ||
// | ||
|
||
public sealed class MyNotificationPublisher : INotificationPublisher | ||
{ | ||
public async ValueTask Publish<TNotification>( | ||
NotificationHandlers<TNotification> handlers, | ||
TNotification notification, | ||
CancellationToken cancellationToken | ||
) | ||
where TNotification : INotification | ||
{ | ||
try | ||
{ | ||
// IsSingleHandler is a convenience method to check if there is only one handler | ||
// so that we can early exist. Used for optimization purposes by the built in implementations. | ||
if (handlers.IsSingleHandler(out var singleHandler)) | ||
{ | ||
await singleHandler.Handle(notification, cancellationToken); | ||
return; | ||
} | ||
// IsArray is a convenience method to check if the handlers are an array (for the built-in DI container in BCL, this is the case) | ||
// so that we can iterate and/or index directly. Used for optimization purposes by the built in implementations. | ||
else if (handlers.IsArray(out var array)) | ||
{ | ||
foreach (var handler in array) | ||
{ | ||
await handler.Handle(notification, cancellationToken); | ||
} | ||
} | ||
else | ||
{ | ||
// Or we can just box the tasks and await them all | ||
await Task.WhenAll( | ||
handlers.Select(handler => handler.Handle(notification, cancellationToken).AsTask()) | ||
); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Notifications should be fire-and-forget, we just need to log it! | ||
Console.Error.WriteLine(ex); | ||
} | ||
} | ||
} | ||
|
||
public sealed record Notification(Guid Id) : INotification; | ||
|
||
public sealed class MyNotificationHandler : INotificationHandler<Notification> | ||
{ | ||
public ValueTask Handle(Notification notification, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine($"{GetType().Name} - {notification.Id}"); | ||
throw new Exception("Something went wrong!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
## NotificationPublisher | ||
|
||
Simple showcase of using a custom notification publisher, by implementing `INotificationPublisher`. | ||
The custom publisher catches all exceptions and logs them, a so called fire-and-forget implementation. | ||
|
||
### Build and run | ||
|
||
```console | ||
$ dotnet run | ||
Publishing! | ||
----------------------------------- | ||
MyNotificationHandler - 6ae7d56b-8a2f-404c-a24b-c5df1e6691d2 | ||
System.Exception: Something went wrong! | ||
at MyNotificationHandler.Handle(Notification notification, CancellationToken cancellationToken) in /home/martin/code/private/Mediator/samples/basic/NotificationPublisher/Program.cs:line 79 | ||
at MyNotificationPublisher.Publish[TNotification](NotificationHandlers`1 handlers, TNotification notification, CancellationToken cancellationToken) in /home/martin/code/private/Mediator/samples/basic/NotificationPublisher/Program.cs:line 46 | ||
----------------------------------- | ||
Finished publishing! | ||
``` | ||
|
Oops, something went wrong.