Skip to content

Commit

Permalink
Updated packages. (#15)
Browse files Browse the repository at this point in the history
* Started re-factoring.

* Updated packages.
  • Loading branch information
mgernand authored Apr 20, 2022
1 parent 5660125 commit b4081af
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 44 deletions.
1 change: 0 additions & 1 deletion Fluxera.Entity.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".items", ".items", "{75AC41
.gitignore = .gitignore
azure-pipelines.yml = azure-pipelines.yml
GitVersion.yml = GitVersion.yml
global.json = global.json
icon.png = icon.png
LICENSE = LICENSE
README.md = README.md
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ trigger:

variables:
BuildConfiguration: Release
DotNetCoreVersion: 6.0.201
DotNetCoreVersion: 6.0.x

stages:
- stage: BuildAndTest
Expand Down
5 changes: 0 additions & 5 deletions global.json

This file was deleted.

21 changes: 21 additions & 0 deletions src/Fluxera.Entity/DomainEvents/DomainEventExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Fluxera.Entity.DomainEvents
{
using System;
using System.Linq;
using System.Reflection;
using Fluxera.Guards;

public static class DomainEventExtensions
{
public static bool IsDomainEventHandler(this Type type)
{
Guard.Against.Null(type, nameof(type));

bool isEventHandler = type.GetInterfaces().Any(x => x.GetTypeInfo().IsGenericType
&& ((x.GetGenericTypeDefinition() == typeof(IDomainEventHandler<>))
|| (x.GetGenericTypeDefinition() == typeof(ICommittedDomainEventHandler<>))));

return isEventHandler;
}
}
}
26 changes: 13 additions & 13 deletions src/Fluxera.Entity/DomainEvents/DomainEventHandlerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public DomainEventHandlerBuilder AddDomainEventHandlers(IEnumerable<Assembly> as
{
assemblies ??= Enumerable.Empty<Assembly>();

foreach (Assembly assembly in assemblies)
foreach(Assembly assembly in assemblies)
{
this.AddDomainEventHandlers(assembly);
}
Expand All @@ -38,15 +38,15 @@ public DomainEventHandlerBuilder AddDomainEventHandlers(IEnumerable<Assembly> as

public DomainEventHandlerBuilder AddDomainEventHandlers(Func<IEnumerable<Assembly>> assembliesFactory)
{
assembliesFactory ??= (Enumerable.Empty<Assembly>);
assembliesFactory ??= Enumerable.Empty<Assembly>;

IEnumerable<Assembly> assemblies = assembliesFactory.Invoke();
this.AddDomainEventHandlers(assemblies);

return this;
}

public DomainEventHandlerBuilder AddDomainEventHandlers<TEventHandler>()
public DomainEventHandlerBuilder AddDomainEventHandler<TEventHandler>()
{
this.AddDomainEventHandlers(typeof(TEventHandler));

Expand All @@ -57,7 +57,7 @@ public DomainEventHandlerBuilder AddDomainEventHandlers(IEnumerable<Type> types)
{
types ??= Enumerable.Empty<Type>();

foreach (Type type in types)
foreach(Type type in types)
{
this.AddDomainEventHandlers(type);
}
Expand All @@ -67,9 +67,9 @@ public DomainEventHandlerBuilder AddDomainEventHandlers(IEnumerable<Type> types)

public DomainEventHandlerBuilder AddDomainEventHandlers(Func<IEnumerable<Type>> typesFactory)
{
Guard.Against.Null(services, nameof(services));
Guard.Against.Null(this.services, nameof(this.services));

typesFactory ??= (Enumerable.Empty<Type>);
typesFactory ??= Enumerable.Empty<Type>;

IEnumerable<Type> types = typesFactory.Invoke();
this.AddDomainEventHandlers(types);
Expand All @@ -91,16 +91,16 @@ public DomainEventHandlerBuilder AddDomainEventHandlers(Type type)
Guard.Against.Null(type, nameof(type));

bool isEventHandler = type.GetInterfaces().Any(x => x.GetTypeInfo().IsGenericType
&& (x.GetGenericTypeDefinition() == typeof(IDomainEventHandler<>)
|| x.GetGenericTypeDefinition() == typeof(ICommittedDomainEventHandler<>)));
&& ((x.GetGenericTypeDefinition() == typeof(IDomainEventHandler<>))
|| (x.GetGenericTypeDefinition() == typeof(ICommittedDomainEventHandler<>))));

if (isEventHandler && !type.GetTypeInfo().IsAbstract && !type.GetTypeInfo().IsInterface)
if(isEventHandler && !type.GetTypeInfo().IsAbstract && !type.GetTypeInfo().IsInterface)
{
IEnumerable<Type> eventHandlerInterfaceTypes = type.GetInterfaces().Where(x => x.GetTypeInfo().IsGenericType
&& (x.GetGenericTypeDefinition() == typeof(IDomainEventHandler<>)
|| x.GetGenericTypeDefinition() == typeof(ICommittedDomainEventHandler<>)));
&& ((x.GetGenericTypeDefinition() == typeof(IDomainEventHandler<>))
|| (x.GetGenericTypeDefinition() == typeof(ICommittedDomainEventHandler<>))));

foreach (Type eventHandlerInterfaceType in eventHandlerInterfaceTypes)
foreach(Type eventHandlerInterfaceType in eventHandlerInterfaceTypes)
{
this.services.AddTransient(eventHandlerInterfaceType, type);
}
Expand All @@ -109,4 +109,4 @@ public DomainEventHandlerBuilder AddDomainEventHandlers(Type type)
return this;
}
}
}
}
3 changes: 3 additions & 0 deletions src/Fluxera.Entity/DomainEvents/IDomainEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
[PublicAPI]
public interface IDomainEvent
{
/// <summary>
/// Gets the name of the event.
/// </summary>
string DisplayName => this.GetType().Name;
}
}
14 changes: 10 additions & 4 deletions src/Fluxera.Entity/DomainEvents/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
namespace Fluxera.Entity.DomainEvents
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Guards;
using Fluxera.Guards;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

/// <summary>
/// Extensions methods for the <see cref="IServiceCollection" /> type.
/// </summary>
[PublicAPI]
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds the domain events services.
/// </summary>
/// <param name="services"></param>
/// <param name="configureHandlers"></param>
/// <returns></returns>
public static IServiceCollection AddDomainEvents(this IServiceCollection services, Action<DomainEventHandlerBuilder> configureHandlers)
{
Guard.Against.Null(services, nameof(services));
Expand Down
6 changes: 3 additions & 3 deletions src/Fluxera.Entity/Fluxera.Entity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fluxera.Guards" Version="6.0.11" />
<PackageReference Include="GitVersion.MsBuild" Version="5.9.0">
<PackageReference Include="Fluxera.Guards" Version="6.0.13" />
<PackageReference Include="GitVersion.MsBuild" Version="5.10.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" />
<PackageReference Include="JetBrains.Annotations" Version="2022.1.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
Expand Down
31 changes: 15 additions & 16 deletions tests/Fluxera.Entity.UnitTests/DomainEventsTests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
namespace Fluxera.Entity.UnitTests
{
using System.Threading.Tasks;
using DomainEvents;
using EmployeeAggregate;
using FluentAssertions;
using Fluxera.Entity.DomainEvents;
using Fluxera.Entity.UnitTests.EmployeeAggregate;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;

[TestFixture]
public class DomainEventsTests
{
private ServiceProvider serviceProvider;

[SetUp]
public void SetUp()
{
Expand All @@ -21,36 +19,38 @@ public void SetUp()
services.AddDomainEvents(builder =>
{
builder
.AddDomainEventHandlers<SalaryRaisedEventHandler>()
.AddDomainEventHandlers<AdditionalSalaryRaisedEventHandler>()
.AddDomainEventHandlers<SalaryRaisedCommittedEventHandler>();
.AddDomainEventHandler<SalaryRaisedEventHandler>()
.AddDomainEventHandler<AdditionalSalaryRaisedEventHandler>()
.AddDomainEventHandler<SalaryRaisedCommittedEventHandler>();
});

this.serviceProvider = services.BuildServiceProvider();
}

private ServiceProvider serviceProvider;

[Test]
public async Task ShouldExecuteDomainHandlers()
public async Task ShouldExecuteCommittedDomainHandlers()
{
IDomainEventDispatcher dispatcher = this.serviceProvider.GetRequiredService<IDomainEventDispatcher>();

SalaryRaisedEvent salaryRaisedEvent = new SalaryRaisedEvent(100_000);
await dispatcher.DispatchAsync(salaryRaisedEvent);
await dispatcher.DispatchCommittedAsync(salaryRaisedEvent);

salaryRaisedEvent.HandlerNames.Count.Should().Be(2);
salaryRaisedEvent.HandlerNames.Should().Contain(nameof(SalaryRaisedEventHandler), nameof(AdditionalSalaryRaisedEventHandler));
salaryRaisedEvent.HandlerNames.Count.Should().Be(1);
salaryRaisedEvent.HandlerNames.Should().Contain(nameof(SalaryRaisedCommittedEventHandler));
}

[Test]
public async Task ShouldExecuteCommittedDomainHandlers()
public async Task ShouldExecuteDomainHandlers()
{
IDomainEventDispatcher dispatcher = this.serviceProvider.GetRequiredService<IDomainEventDispatcher>();

SalaryRaisedEvent salaryRaisedEvent = new SalaryRaisedEvent(100_000);
await dispatcher.DispatchCommittedAsync(salaryRaisedEvent);
await dispatcher.DispatchAsync(salaryRaisedEvent);

salaryRaisedEvent.HandlerNames.Count.Should().Be(1);
salaryRaisedEvent.HandlerNames.Should().Contain(nameof(SalaryRaisedCommittedEventHandler));
salaryRaisedEvent.HandlerNames.Count.Should().Be(2);
salaryRaisedEvent.HandlerNames.Should().Contain(nameof(SalaryRaisedEventHandler), nameof(AdditionalSalaryRaisedEventHandler));
}

[Test]
Expand All @@ -76,7 +76,6 @@ public async Task ShouldExecuteDomainHandlersFromEntity()
((SalaryRaisedEvent)domainEvent).HandlerNames.Count.Should().Be(2);
((SalaryRaisedEvent)domainEvent).HandlerNames.Should().Contain(nameof(SalaryRaisedEventHandler), nameof(AdditionalSalaryRaisedEventHandler));
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.5.1" />
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
Expand Down

0 comments on commit b4081af

Please sign in to comment.