Skip to content

Commit 3c6326e

Browse files
committed
Add notifications sample
1 parent ea69cc8 commit 3c6326e

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

Justfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
alias b := build
2+
alias t := test
3+
4+
build:
5+
dotnet build Mediator.sln
6+
7+
test:
8+
dotnet test Mediator.sln

Mediator.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCoreIndirect.Applicat
102102
EndProject
103103
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCoreIndirect.Implementations", "samples\ASPNET_Core_Indirect\AspNetCoreIndirect.Implementations\AspNetCoreIndirect.Implementations.csproj", "{7F7DB4D8-3D94-4648-99FA-E7E06A913FCD}"
104104
EndProject
105+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Notifications", "samples\Notifications\Notifications.csproj", "{78D25AC1-4DDD-4049-8B26-D287931905E5}"
106+
EndProject
105107
Global
106108
GlobalSection(SolutionConfigurationPlatforms) = preSolution
107109
Debug|Any CPU = Debug|Any CPU
@@ -448,6 +450,18 @@ Global
448450
{7F7DB4D8-3D94-4648-99FA-E7E06A913FCD}.Release|x64.Build.0 = Release|Any CPU
449451
{7F7DB4D8-3D94-4648-99FA-E7E06A913FCD}.Release|x86.ActiveCfg = Release|Any CPU
450452
{7F7DB4D8-3D94-4648-99FA-E7E06A913FCD}.Release|x86.Build.0 = Release|Any CPU
453+
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
454+
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
455+
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Debug|x64.ActiveCfg = Debug|Any CPU
456+
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Debug|x64.Build.0 = Debug|Any CPU
457+
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Debug|x86.ActiveCfg = Debug|Any CPU
458+
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Debug|x86.Build.0 = Debug|Any CPU
459+
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
460+
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Release|Any CPU.Build.0 = Release|Any CPU
461+
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Release|x64.ActiveCfg = Release|Any CPU
462+
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Release|x64.Build.0 = Release|Any CPU
463+
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Release|x86.ActiveCfg = Release|Any CPU
464+
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Release|x86.Build.0 = Release|Any CPU
451465
EndGlobalSection
452466
GlobalSection(SolutionProperties) = preSolution
453467
HideSolutionNode = FALSE
@@ -483,6 +497,7 @@ Global
483497
{EBB07938-94A7-4349-9D0F-F2A6088EB42E} = {BC759BA5-C064-460E-ACB0-9B2DA12B9284}
484498
{DB9A7862-14B1-4DAC-878C-D6B02038F068} = {BC759BA5-C064-460E-ACB0-9B2DA12B9284}
485499
{7F7DB4D8-3D94-4648-99FA-E7E06A913FCD} = {BC759BA5-C064-460E-ACB0-9B2DA12B9284}
500+
{78D25AC1-4DDD-4049-8B26-D287931905E5} = {D3569CDD-7E19-429E-B9AD-75CC05F6C4AA}
486501
EndGlobalSection
487502
GlobalSection(ExtensibilityGlobals) = postSolution
488503
SolutionGuid = {D45B5457-4190-49B6-BF89-7FA5F4C8ABE2}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
7+
<ImplicitUsings>disable</ImplicitUsings>
8+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
9+
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
14+
<None Include="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(DotNetVersion)" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\..\src\Mediator.SourceGenerator.Implementation\Mediator.SourceGenerator.Implementation.csproj" OutputItemType="Analyzer" />
23+
<ProjectReference Include="..\..\src\Mediator.SourceGenerator.Roslyn38\Mediator.SourceGenerator.Roslyn38.csproj" OutputItemType="Analyzer" />
24+
<ProjectReference Include="..\..\src\Mediator\Mediator.csproj" />
25+
</ItemGroup>
26+
27+
<!--<ItemGroup>
28+
<PackageReference Include="Mediator.SourceGenerator" Version="2.0.*-*">
29+
<PrivateAssets>all</PrivateAssets>
30+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
31+
</PackageReference>
32+
<PackageReference Include="Mediator.Abstractions" Version="2.0.*-*" />
33+
</ItemGroup>-->
34+
35+
<ItemGroup>
36+
<!--<CompilerVisibleProperty Include="Mediator_AttachDebugger" />-->
37+
</ItemGroup>
38+
39+
</Project>

samples/Notifications/Program.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Mediator;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using System;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
var services = new ServiceCollection();
8+
9+
services.AddMediator();
10+
11+
var serviceProvider = services.BuildServiceProvider();
12+
13+
var mediator = serviceProvider.GetRequiredService<IMediator>();
14+
15+
var id = Guid.NewGuid();
16+
var notification = new Notification(id);
17+
18+
Console.WriteLine("Publishing!");
19+
Console.WriteLine("-----------------------------------");
20+
21+
await mediator.Publish(notification);
22+
23+
Console.WriteLine("-----------------------------------");
24+
Console.WriteLine("Finished publishing!");
25+
26+
return 0;
27+
28+
//
29+
// Here are the types used
30+
//
31+
32+
public sealed record Notification(Guid Id) : INotification;
33+
34+
public sealed class GenericNotificationHandler<TNotification> : INotificationHandler<TNotification>
35+
where TNotification : INotification
36+
{
37+
public ValueTask Handle(TNotification notification, CancellationToken cancellationToken)
38+
{
39+
if (notification is Notification concrete)
40+
Console.WriteLine($"{GetType().Name} - {concrete.Id}");
41+
return default;
42+
}
43+
}
44+
45+
public sealed class CatchAllNotificationHandler : INotificationHandler<INotification>
46+
{
47+
public ValueTask Handle(INotification notification, CancellationToken cancellationToken)
48+
{
49+
if (notification is Notification concrete)
50+
Console.WriteLine($"{GetType().Name} - {concrete.Id}");
51+
return default;
52+
}
53+
}
54+
55+
public sealed class ConcreteNotificationHandler : INotificationHandler<Notification>
56+
{
57+
public ValueTask Handle(Notification notification, CancellationToken cancellationToken)
58+
{
59+
Console.WriteLine($"{GetType().Name} - {notification.Id}");
60+
return default;
61+
}
62+
}

samples/Notifications/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Notifications
2+
3+
Simple showcase of using notifications and various notification-handlers.
4+
5+
### Build and run
6+
7+
```pwsh
8+
$ dotnet run
9+
Publishing!
10+
-----------------------------------
11+
CatchAllNotificationHandler - e9c1302b-0eab-4193-8445-ab70a93265d6
12+
ConcreteNotificationHandler - e9c1302b-0eab-4193-8445-ab70a93265d6
13+
GenericNotificationHandler`1 - e9c1302b-0eab-4193-8445-ab70a93265d6
14+
-----------------------------------
15+
Finished publishing!
16+
```

0 commit comments

Comments
 (0)