Skip to content

Commit adde252

Browse files
committed
Switched to use Mediator
1 parent 2671942 commit adde252

16 files changed

Lines changed: 113 additions & 143 deletions

Directory.Packages.props

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<Project>
2-
<ItemGroup>
3-
<PackageVersion Include="Ardalis.GuardClauses" Version="5.0.0" />
4-
<PackageVersion Include="Ardalis.Specification" Version="8.0.0" />
5-
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
6-
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
7-
<PackageVersion Include="MediatR" Version="12.4.1" />
8-
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
9-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
10-
<PackageVersion Include="Moq" Version="4.20.72" />
11-
<PackageVersion Include="xunit" Version="2.9.2" />
12-
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
13-
</ItemGroup>
2+
<ItemGroup>
3+
<PackageVersion Include="Ardalis.GuardClauses" Version="5.0.0" />
4+
<PackageVersion Include="Ardalis.Specification" Version="8.0.0" />
5+
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
6+
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
7+
<PackageVersion Include="Mediator.Abstractions" Version="3.0.1" />
8+
<PackageVersion Include="Mediator.SourceGenerator" Version="3.0.1" />
9+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
10+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
11+
<PackageVersion Include="Moq" Version="4.20.72" />
12+
<PackageVersion Include="xunit" Version="2.9.2" />
13+
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
14+
</ItemGroup>
1415
</Project>

src/NimblePros.SharedKernel/DomainEventBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using MediatR;
1+
using Mediator;
22

33
namespace NimblePros.SharedKernel;
44

55
/// <summary>
6-
/// A base type for domain events. Depends on MediatR INotification.
6+
/// A base type for domain events. Depends on Mediator INotification.
77
/// Includes DateOccurred which is set on creation.
88
/// </summary>
99
public abstract class DomainEventBase : INotification

src/NimblePros.SharedKernel/ICommand.cs

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

src/NimblePros.SharedKernel/ICommandHandler.cs

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

src/NimblePros.SharedKernel/IQuery.cs

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

src/NimblePros.SharedKernel/IQueryHandler.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,47 @@
11
using System.Diagnostics;
22
using System.Reflection;
33
using Ardalis.GuardClauses;
4-
using MediatR;
54
using Microsoft.Extensions.Logging;
5+
using Mediator;
66

77
namespace NimblePros.SharedKernel;
88

9-
/// <summary>
10-
/// Adds logging for all requests in MediatR pipeline.
11-
/// Configure by adding the service with a scoped lifetime
12-
///
13-
/// Example for Autofac:
14-
/// builder
15-
/// .RegisterType&lt;Mediator&gt;()
16-
/// .As&lt;IMediator&gt;()
17-
/// .InstancePerLifetimeScope();
18-
///
19-
/// builder
20-
/// .RegisterGeneric(typeof(LoggingBehavior&lt;,&gt;))
21-
/// .As(typeof(IPipelineBehavior&lt;,&gt;))
22-
/// .InstancePerLifetimeScope();
23-
///
24-
/// </summary>
25-
/// <typeparam name="TRequest"></typeparam>
26-
/// <typeparam name="TResponse"></typeparam>
279
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
28-
where TRequest : IRequest<TResponse>
10+
where TRequest : IRequest<TResponse>
2911
{
30-
private readonly ILogger<Mediator> _logger;
12+
private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;
3113

32-
public LoggingBehavior(ILogger<Mediator> logger)
33-
{
34-
_logger = logger;
35-
}
36-
37-
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
38-
{
39-
Guard.Against.Null(request);
40-
if (_logger.IsEnabled(LogLevel.Information))
14+
public LoggingBehavior(ILogger<LoggingBehavior<TRequest, TResponse>> logger)
4115
{
42-
_logger.LogInformation("Handling {RequestName}", typeof(TRequest).Name);
43-
44-
// Reflection! Could be a performance concern
45-
Type myType = request.GetType();
46-
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
47-
foreach (PropertyInfo prop in props)
48-
{
49-
object? propValue = prop?.GetValue(request, null);
50-
_logger.LogInformation("Property {Property} : {@Value}", prop?.Name, propValue);
51-
}
16+
_logger = logger;
5217
}
5318

54-
var sw = Stopwatch.StartNew();
55-
56-
var response = await next();
57-
58-
_logger.LogInformation("Handled {RequestName} with {Response} in {ms} ms", typeof(TRequest).Name, response, sw.ElapsedMilliseconds);
59-
sw.Stop();
60-
return response;
61-
}
19+
public async ValueTask<TResponse> Handle(
20+
TRequest request,
21+
MessageHandlerDelegate<TRequest, TResponse> next,
22+
CancellationToken cancellationToken)
23+
{
24+
Guard.Against.Null(request);
25+
if (_logger.IsEnabled(LogLevel.Information))
26+
{
27+
_logger.LogInformation("Handling {RequestName}", typeof(TRequest).Name);
28+
29+
Type myType = request.GetType();
30+
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
31+
foreach (PropertyInfo prop in props)
32+
{
33+
object? propValue = prop?.GetValue(request, null);
34+
_logger.LogInformation("Property {Property} : {@Value}", prop?.Name, propValue);
35+
}
36+
}
37+
38+
var sw = Stopwatch.StartNew();
39+
40+
var response = await next(request, cancellationToken);
41+
42+
_logger.LogInformation("Handled {RequestName} with {Response} in {ms} ms", typeof(TRequest).Name, response, sw.ElapsedMilliseconds);
43+
sw.Stop();
44+
return response;
45+
}
6246
}
6347

src/NimblePros.SharedKernel/MediatRDomainEventDispatcher.cs renamed to src/NimblePros.SharedKernel/MediatorDomainEventDispatcher.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using MediatR;
1+
using Mediator;
22
using Microsoft.Extensions.Logging;
33

44
namespace NimblePros.SharedKernel;
55

6-
public class MediatRDomainEventDispatcher : IDomainEventDispatcher
6+
public class MediatorDomainEventDispatcher : IDomainEventDispatcher
77
{
88
private readonly IMediator _mediator;
9-
private readonly ILogger<MediatRDomainEventDispatcher> _logger;
9+
private readonly ILogger<MediatorDomainEventDispatcher> _logger;
1010

11-
public MediatRDomainEventDispatcher(IMediator mediator, ILogger<MediatRDomainEventDispatcher> logger)
11+
public MediatorDomainEventDispatcher(IMediator mediator, ILogger<MediatorDomainEventDispatcher> logger)
1212
{
1313
_mediator = mediator;
1414
_logger = logger;

src/NimblePros.SharedKernel/NimblePros.SharedKernel.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
<RepositoryUrl>https://github.com/NimblePros/SharedKernelSample</RepositoryUrl>
1414
<PackageTags>Ardalis;NimblePros;DDD;Shared Kernel;SharedKernel;Domain-Driven Design;Repository;Specification;ValueObject;Value Object;Clean;Clean Architecture;Clean Architecture Template</PackageTags>
1515
<PackageIcon>icon.png</PackageIcon>
16-
<Version>2.1.1</Version>
16+
<Version>3.0.0</Version>
1717
<PackageReleaseNotes>
18-
* Updated packages
18+
* Namespace from Ardalis to NimblePros
1919
</PackageReleaseNotes>
2020
<PublishRepositoryUrl>true</PublishRepositoryUrl>
2121
<EmbedUntrackedSources>true</EmbedUntrackedSources>
@@ -26,7 +26,7 @@
2626
<ItemGroup>
2727
<PackageReference Include="Ardalis.GuardClauses" />
2828
<PackageReference Include="Ardalis.Specification" />
29-
<PackageReference Include="MediatR" />
29+
<PackageReference Include="Mediator.Abstractions" />
3030
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
3131
</ItemGroup>
3232

tests/NimblePros.SharedKernel.UnitTests/DomainEventBaseTests/DomainEventBase_Constructor.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using Xunit;
22
using FluentAssertions;
3+
using Mediator;
34

45
namespace NimblePros.SharedKernel.UnitTests.DomainEventBaseTests;
56

6-
public class DomainEventBase_Constructor
7+
public class DomainEventBase_Constructor : INotificationHandler<DomainEventBase_Constructor.TestDomainEvent>
78
{
8-
private class TestDomainEvent : DomainEventBase { }
9+
public class TestDomainEvent : DomainEventBase { }
910

1011
[Fact]
1112
public void SetsDateOccurredToCurrentDateTime()
@@ -20,4 +21,9 @@ public void SetsDateOccurredToCurrentDateTime()
2021
domainEvent.DateOccurred.Should().BeOnOrAfter(beforeCreation);
2122
domainEvent.DateOccurred.Should().BeOnOrBefore(DateTime.UtcNow);
2223
}
24+
25+
public ValueTask Handle(TestDomainEvent notification, CancellationToken cancellationToken)
26+
{
27+
throw new NotImplementedException();
28+
}
2329
}

0 commit comments

Comments
 (0)