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
18 changes: 10 additions & 8 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<Project>
<ItemGroup>
<PackageVersion Include="Ardalis.GuardClauses" Version="5.0.0" />
<PackageVersion Include="Ardalis.Specification" Version="9.2.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
<PackageVersion Include="FluentAssertions" Version="8.4.0" />
<PackageVersion Include="MediatR" Version="12.5.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.6" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageVersion Include="Ardalis.Specification" Version="9.3.1" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="AwesomeAssertions" Version="9.2.0" />
<PackageVersion Include="Mediator.Abstractions" Version="3.0.1" />
<PackageVersion Include="Mediator.SourceGenerator" Version="3.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.10" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.1" />
<PackageVersion Include="Shouldly" Version="4.3.0" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
</Project>
7 changes: 4 additions & 3 deletions src/Ardalis.SharedKernel/Ardalis.SharedKernel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
<RepositoryUrl>https://github.com/ardalis/Ardalis.SharedKernel</RepositoryUrl>
<PackageTags>DDD;Shared Kernel;SharedKernel;Domain-Driven Design;Repository;Specification;ValueObject;Value Object;Ardalis;Clean;Clean Architecture;Clean Architecture Template</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<Version>3.0.1</Version>
<Version>4.0.0</Version>
<PackageReleaseNotes>
* Net9
* Swapped out MediatR for Mediator Souregenerator and Mediator.Abstractions
* Updated dependencies to latest versions
</PackageReleaseNotes>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
Expand All @@ -26,7 +27,7 @@
<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" />
<PackageReference Include="Ardalis.Specification" />
<PackageReference Include="MediatR" />
<PackageReference Include="Mediator.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
</ItemGroup>

Expand Down
5 changes: 2 additions & 3 deletions src/Ardalis.SharedKernel/DomainEventBase.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using MediatR;
using Mediator;

namespace Ardalis.SharedKernel;

/// <summary>
/// A base type for domain events. Depends on MediatR INotification.
/// A base type for domain events. Depends on Mediator INotification.
/// Includes DateOccurred which is set on creation.
/// </summary>
public abstract class DomainEventBase : INotification
{
public DateTime DateOccurred { get; protected set; } = DateTime.UtcNow;
}

11 changes: 0 additions & 11 deletions src/Ardalis.SharedKernel/ICommand.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/Ardalis.SharedKernel/ICommandHandler.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/Ardalis.SharedKernel/IQuery.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/Ardalis.SharedKernel/IQueryHandler.cs

This file was deleted.

37 changes: 11 additions & 26 deletions src/Ardalis.SharedKernel/LoggingBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,33 @@
using System.Diagnostics;
using System.Reflection;
using Ardalis.GuardClauses;
using MediatR;
using Mediator;
using Microsoft.Extensions.Logging;

namespace Ardalis.SharedKernel;

/// <summary>
/// Adds logging for all requests in MediatR pipeline.
/// Adds logging for all requests in Mediator pipeline.
/// Configure by adding the service with a scoped lifetime
///
/// Example for Autofac:
/// builder
/// .RegisterType&lt;Mediator&gt;()
/// .As&lt;IMediator&gt;()
/// .InstancePerLifetimeScope();
///
/// builder
/// .RegisterGeneric(typeof(LoggingBehavior&lt;,&gt;))
/// .As(typeof(IPipelineBehavior&lt;,&gt;))
/// .InstancePerLifetimeScope();
///
/// </summary>
/// <typeparam name="TRequest"></typeparam>
/// <typeparam name="TResponse"></typeparam>
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
public class LoggingBehavior<TRequest, TResponse>(ILogger<LoggingBehavior<TRequest, TResponse>> logger)
: IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly ILogger<Mediator> _logger;
private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger = logger;

public LoggingBehavior(ILogger<Mediator> logger)
{
_logger = logger;
}

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
public async ValueTask<TResponse> Handle(
TRequest request,
MessageHandlerDelegate<TRequest, TResponse> next,
CancellationToken cancellationToken)
{
Guard.Against.Null(request);
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Handling {RequestName}", typeof(TRequest).Name);

// Reflection! Could be a performance concern
Type myType = request.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
foreach (PropertyInfo prop in props)
Expand All @@ -53,11 +39,10 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe

var sw = Stopwatch.StartNew();

var response = await next();
var response = await next(request, cancellationToken);

_logger.LogInformation("Handled {RequestName} with {Response} in {ms} ms", typeof(TRequest).Name, response, sw.ElapsedMilliseconds);
sw.Stop();
return response;
}
}

8 changes: 4 additions & 4 deletions src/Ardalis.SharedKernel/MediatRDomainEventDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using MediatR;
using Mediator;
using Microsoft.Extensions.Logging;

namespace Ardalis.SharedKernel;

public class MediatRDomainEventDispatcher : IDomainEventDispatcher
public class MediatorDomainEventDispatcher : IDomainEventDispatcher
{
private readonly IMediator _mediator;
private readonly ILogger<MediatRDomainEventDispatcher> _logger;
private readonly ILogger<MediatorDomainEventDispatcher> _logger;

public MediatRDomainEventDispatcher(IMediator mediator, ILogger<MediatRDomainEventDispatcher> logger)
public MediatorDomainEventDispatcher(IMediator mediator, ILogger<MediatorDomainEventDispatcher> logger)
{
_mediator = mediator;
_logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="AwesomeAssertions" />
<PackageReference Include="Mediator.SourceGenerator" PrivateAssets="all" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="Shouldly" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Xunit;
using FluentAssertions;
using Mediator;

namespace Ardalis.SharedKernel.UnitTests.DomainEventBaseTests;

public class DomainEventBase_Constructor
public class DomainEventBase_Constructor : INotificationHandler<DomainEventBase_Constructor.TestDomainEvent>
{
private class TestDomainEvent : DomainEventBase { }
public class TestDomainEvent : DomainEventBase { }

[Fact]
public void SetsDateOccurredToCurrentDateTime()
Expand All @@ -20,4 +19,9 @@ public void SetsDateOccurredToCurrentDateTime()
domainEvent.DateOccurred.Should().BeOnOrAfter(beforeCreation);
domainEvent.DateOccurred.Should().BeOnOrBefore(DateTime.UtcNow);
}

public ValueTask Handle(TestDomainEvent notification, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using FluentAssertions;
using Xunit;
using Mediator;

namespace Ardalis.SharedKernel.UnitTests.EntityBaseTests;

public class EntityBase_AddDomainEvent
public class EntityBase_AddDomainEvent : INotificationHandler<EntityBase_AddDomainEvent.TestDomainEvent>
{
private class TestDomainEvent : DomainEventBase { }
public class TestDomainEvent : DomainEventBase { }

private class TestEntity : EntityBase
{
Expand All @@ -29,4 +28,9 @@ public void AddsDomainEventToEntity()
entity.DomainEvents.Should().HaveCount(1);
entity.DomainEvents.Should().AllBeOfType<TestDomainEvent>();
}

public ValueTask Handle(EntityBase_AddDomainEvent.TestDomainEvent notification, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
2 changes: 2 additions & 0 deletions tests/Ardalis.SharedKernel.UnitTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
global using Xunit;
global using AwesomeAssertions;
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using FluentAssertions;
using MediatR;
using Mediator;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Ardalis.SharedKernel.UnitTests.MediatRDomainEventDispatcherTests;
namespace Ardalis.SharedKernel.UnitTests.MediatorDomainEventDispatcherTests;

public class DispatchAndClearEvents
public class DispatchAndClearEvents : INotificationHandler<DispatchAndClearEvents.TestDomainEvent>
{
private class TestDomainEvent : DomainEventBase { }
public class TestDomainEvent : DomainEventBase { }
private class TestEntity : EntityBase
{
public void AddTestDomainEvent()
Expand All @@ -23,7 +21,7 @@ public async Task CallsPublishAndClearDomainEvents()
{
// Arrange
var mediatorMock = new Mock<IMediator>();
var domainEventDispatcher = new MediatRDomainEventDispatcher(mediatorMock.Object, NullLogger<MediatRDomainEventDispatcher>.Instance);
var domainEventDispatcher = new MediatorDomainEventDispatcher(mediatorMock.Object, NullLogger<MediatorDomainEventDispatcher>.Instance);
var entity = new TestEntity();
entity.AddTestDomainEvent();

Expand All @@ -34,4 +32,9 @@ public async Task CallsPublishAndClearDomainEvents()
mediatorMock.Verify(m => m.Publish(It.IsAny<DomainEventBase>(), It.IsAny<CancellationToken>()), Times.Once);
entity.DomainEvents.Should().BeEmpty();
}

public ValueTask Handle(TestDomainEvent notification, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using FluentAssertions;
using MediatR;
using Mediator;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Ardalis.SharedKernel.UnitTests.MediatRDomainEventDispatcherTests;

public class DispatchAndClearEventsWithGuidId
public class DispatchAndClearEventsWithGuidId : INotificationHandler<DispatchAndClearEventsWithGuidId.TestDomainEvent>
{
private class TestDomainEvent : DomainEventBase { }
public class TestDomainEvent : DomainEventBase { }
private class TestEntity : EntityBase<Guid>
{
public void AddTestDomainEvent()
Expand All @@ -23,7 +22,7 @@ public async Task CallsPublishAndClearDomainEvents()
{
// Arrange
var mediatorMock = new Mock<IMediator>();
var domainEventDispatcher = new MediatRDomainEventDispatcher(mediatorMock.Object, NullLogger<MediatRDomainEventDispatcher>.Instance);
var domainEventDispatcher = new MediatorDomainEventDispatcher(mediatorMock.Object, NullLogger<MediatorDomainEventDispatcher>.Instance);
var entity = new TestEntity();
entity.AddTestDomainEvent();

Expand All @@ -34,4 +33,9 @@ public async Task CallsPublishAndClearDomainEvents()
mediatorMock.Verify(m => m.Publish(It.IsAny<DomainEventBase>(), It.IsAny<CancellationToken>()), Times.Once);
entity.DomainEvents.Should().BeEmpty();
}

public ValueTask Handle(DispatchAndClearEventsWithGuidId.TestDomainEvent notification, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using FluentAssertions;
using MediatR;
using Mediator;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Ardalis.SharedKernel.UnitTests.MediatRDomainEventDispatcherTests;

public class DispatchAndClearEventsWithMixedIds
public class DispatchAndClearEventsWithMixedIds : INotificationHandler<DispatchAndClearEventsWithMixedIds.TestDomainEvent>
{
private class TestDomainEvent : DomainEventBase { }
public class TestDomainEvent : DomainEventBase { }
public readonly record struct StronglyTyped { }

private class TestEntity : EntityBase
Expand Down Expand Up @@ -41,7 +40,7 @@ public async Task CallsPublishAndClearDomainEventsWithStronglyTypedId()
{
// Arrange
var mediatorMock = new Mock<IMediator>();
var domainEventDispatcher = new MediatRDomainEventDispatcher(mediatorMock.Object, NullLogger<MediatRDomainEventDispatcher>.Instance);
var domainEventDispatcher = new MediatorDomainEventDispatcher(mediatorMock.Object, NullLogger<MediatorDomainEventDispatcher>.Instance);
var entity = new TestEntity();
var entityGuid = new TestEntityGuid();
var entityStronglyTyped = new TestEntityStronglyTyped();
Expand All @@ -58,4 +57,9 @@ public async Task CallsPublishAndClearDomainEventsWithStronglyTypedId()
entityGuid.DomainEvents.Should().BeEmpty();
entityStronglyTyped.DomainEvents.Should().BeEmpty();
}

public ValueTask Handle(TestDomainEvent notification, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
Loading