Skip to content

Commit

Permalink
Merge pull request #90 from wemogy/feat/remote-processing
Browse files Browse the repository at this point in the history
Feat/remote processing
  • Loading branch information
SebastianKuesters authored Jun 25, 2024
2 parents a45aacc + 188caf3 commit 8aa8073
Show file tree
Hide file tree
Showing 94 changed files with 2,043 additions and 598 deletions.
4 changes: 2 additions & 2 deletions .github/actions/dotnet/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ inputs:
runs:
using: "composite"
steps:
- uses: actions/setup-dotnet@v3
- uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x
- name: Restore dependencies
working-directory: src
shell: bash
Expand Down
279 changes: 171 additions & 108 deletions src/Wemogy.Cqrs.sln

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Wemogy.CQRS.Commands.Abstractions;

namespace Wemogy.CQRS.UnitTests.AssemblyA.Commands.PrintHelloAssemblyA;

public class PrintHelloAssemblyACommand : ICommand
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Threading.Tasks;
using Wemogy.CQRS.Commands.Abstractions;

namespace Wemogy.CQRS.UnitTests.AssemblyA.Commands.PrintHelloAssemblyA;

public class PrintHelloAssemblyACommandHandler : ICommandHandler<PrintHelloAssemblyACommand>
{
public static int CallCount { get; private set; }

public Task HandleAsync(PrintHelloAssemblyACommand command)
{
Console.WriteLine("Hello from Assembly A!");
CallCount++;
return Task.CompletedTask;
}
}
11 changes: 11 additions & 0 deletions src/core/Wemogy.CQRS.UnitTests.AssemblyA/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.Extensions.DependencyInjection;

namespace Wemogy.CQRS.UnitTests.AssemblyA;

public static class DependencyInjection
{
public static void AddAssemblyA(this IServiceCollection services)
{
services.AddCQRS();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Wemogy.CQRS.Commands.Abstractions;

namespace Wemogy.CQRS.UnitTests.AssemblyB.Commands.PrintHelloAssemblyB;

public class PrintHelloAssemblyBCommand : ICommand
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Threading.Tasks;
using Wemogy.CQRS.Commands.Abstractions;

namespace Wemogy.CQRS.UnitTests.AssemblyB.Commands.PrintHelloAssemblyB;

public class PrintHelloAssemblyBCommandHandler : ICommandHandler<PrintHelloAssemblyBCommand>
{
public static int CallCount { get; private set; }

public Task HandleAsync(PrintHelloAssemblyBCommand command)
{
Console.WriteLine("Hello from Assembly B!");
CallCount++;
return Task.CompletedTask;
}
}
11 changes: 11 additions & 0 deletions src/core/Wemogy.CQRS.UnitTests.AssemblyB/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.Extensions.DependencyInjection;

namespace Wemogy.CQRS.UnitTests.AssemblyB;

public static class DependencyInjection
{
public static void AddAssemblyB(this IServiceCollection services)
{
services.AddCQRS();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Wemogy.CQRS\Wemogy.CQRS.csproj" />
</ItemGroup>

</Project>
43 changes: 43 additions & 0 deletions src/core/Wemogy.CQRS.UnitTests/DependencyInjectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Wemogy.CQRS.Commands.Abstractions;
using Wemogy.CQRS.UnitTests.AssemblyA;
using Wemogy.CQRS.UnitTests.AssemblyA.Commands.PrintHelloAssemblyA;
using Wemogy.CQRS.UnitTests.AssemblyB;
using Wemogy.CQRS.UnitTests.AssemblyB.Commands.PrintHelloAssemblyB;
using Wemogy.CQRS.UnitTests.TestApplication.Commands.TrackUserLogin;
using Xunit;

namespace Wemogy.CQRS.UnitTests;

public class DependencyInjectionTests
{
[Fact]
public async Task CallingAddCQRSMultipleTimesInDifferentAssembliesShouldWork()
{
// Arrange
var serviceCollection = new ServiceCollection();
serviceCollection.AddCQRS();
serviceCollection.AddAssemblyA();
serviceCollection.AddAssemblyB();
var serviceProvider = serviceCollection.BuildServiceProvider();
var commands = serviceProvider.GetRequiredService<ICommands>();
var helloAssemblyACommand = new PrintHelloAssemblyACommand();
var helloAssemblyBCommand = new PrintHelloAssemblyBCommand();
var trackUserLoginCommand = new TrackUserLoginCommand("test-user-id");

// Act
var trackUserLoginCommandException = await Record.ExceptionAsync(() => commands.RunAsync(trackUserLoginCommand));
var helloAssemblyACommandException = await Record.ExceptionAsync(() => commands.RunAsync(helloAssemblyACommand));
var helloAssemblyBCommandException = await Record.ExceptionAsync(() => commands.RunAsync(helloAssemblyBCommand));

// Assert
TrackUserLoginCommandHandler.CallCount.Should().Be(1);
trackUserLoginCommandException.Should().BeNull();
PrintHelloAssemblyACommandHandler.CallCount.Should().Be(1);
helloAssemblyACommandException.Should().BeNull();
PrintHelloAssemblyBCommandHandler.CallCount.Should().Be(1);
helloAssemblyBCommandException.Should().BeNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Linq;
using System.Reflection;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Wemogy.CQRS.Commands.Abstractions;
using Wemogy.CQRS.Extensions;
using Wemogy.CQRS.UnitTests.TestApplication.Commands.CreateUser;
using Wemogy.CQRS.UnitTests.TestApplication.Commands.KitchenSinkWithoutResult;
using Wemogy.CQRS.UnitTests.TestApplication.Commands.KitchenSinkWithResult;
using Wemogy.CQRS.UnitTests.TestApplication.Commands.RecalculateTotalUserCount;
using Xunit;

namespace Wemogy.CQRS.UnitTests.Extensions;

public class ServiceCollectionExtensionsTests
{
[Fact]
public void AddImplementationsOfInterfaceScoped_ShouldWork()
{
// Arrange
var serviceCollection = new ServiceCollection();
var assemblies = new[]
{
Assembly.GetExecutingAssembly(),
Assembly.GetCallingAssembly()
}.ToList();

// Act
serviceCollection.AddImplementationsOfGenericInterfaceScoped(
typeof(ICommandPreProcessor<>),
assemblies);

// Assert
var serviceProvider = serviceCollection.BuildServiceProvider();
serviceProvider.GetServices<ICommandPreProcessor<KitchenSinkWithoutResultCommand>>()
.Should().HaveCount(2);
serviceProvider.GetServices<ICommandPreProcessor<CreateUserCommand>>()
.Should().HaveCount(1);
serviceProvider.GetServices<ICommandPreProcessor<RecalculateTotalUserCountCommand>>()
.Should().HaveCount(0);
}

[Fact]
public void AddImplementationsOfInterfaceScoped_ShouldAddOnlyOnceIfCalledManyTimes()
{
// Arrange
var serviceCollection = new ServiceCollection();
var assemblies = new[]
{
Assembly.GetExecutingAssembly(),
Assembly.GetCallingAssembly()
}.ToList();

// Act
serviceCollection.AddImplementationsOfGenericInterfaceScoped(
typeof(ICommandPreProcessor<>),
assemblies);
serviceCollection.AddImplementationsOfGenericInterfaceScoped(
typeof(ICommandPreProcessor<>),
assemblies);
serviceCollection.AddImplementationsOfGenericInterfaceScoped(
typeof(ICommandPreProcessor<>),
assemblies);

// Assert
var serviceProvider = serviceCollection.BuildServiceProvider();
serviceProvider.GetServices<ICommandPreProcessor<KitchenSinkWithoutResultCommand>>()
.Should().HaveCount(2);
serviceProvider.GetServices<ICommandPreProcessor<CreateUserCommand>>()
.Should().HaveCount(1);
serviceProvider.GetServices<ICommandPreProcessor<RecalculateTotalUserCountCommand>>()
.Should().HaveCount(0);
}

[Fact]
public void AddImplementationsOfInterfaceScoped_ShouldWorkForInterfaceWithTwoGenerics()
{
// Arrange
var serviceCollection = new ServiceCollection();
var assemblies = new[]
{
Assembly.GetExecutingAssembly(),
Assembly.GetCallingAssembly()
}.ToList();

// Act
serviceCollection.AddImplementationsOfGenericInterfaceScoped(
typeof(ICommandHandler<,>),
assemblies);

// Assert
var serviceProvider = serviceCollection.BuildServiceProvider();
serviceProvider.GetServices<ICommandHandler<KitchenSinkWithResultCommand, int>>()
.Should().ContainSingle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using Wemogy.Core.Errors.Exceptions;
using Wemogy.CQRS.Queries.Abstractions;
using Wemogy.CQRS.UnitTests.AssemblyA;
using Wemogy.CQRS.UnitTests.AssemblyA.Queries;
using Wemogy.CQRS.UnitTests.TestApplication;
using Wemogy.CQRS.UnitTests.TestApplication.Queries.GetUser;
Expand Down Expand Up @@ -75,7 +76,8 @@ public async Task QueryAsync_ShouldSupportMultipleAssemblyDefinitions()
{
// Arrange
var serviceCollection = new ServiceCollection();
serviceCollection.AddTestApplication();
serviceCollection.AddCQRS();
serviceCollection.AddAssemblyA();
var serviceProvider = serviceCollection.BuildServiceProvider();
var queries = serviceProvider.GetRequiredService<IQueries>();
var getUserQuery = new GetUserActivityQuery();
Expand Down
12 changes: 6 additions & 6 deletions src/core/Wemogy.CQRS.UnitTests/SetupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public void SetupShould_WorkCorrectly()
var serviceProvider = serviceCollection.BuildServiceProvider();

// Commands
serviceProvider.GetRequiredService<List<ICommandValidator<CreateUserCommand>>>().Should().HaveCount(1);
serviceProvider.GetRequiredService<List<ICommandAuthorization<CreateUserCommand>>>().Should().HaveCount(1);
serviceProvider.GetRequiredService<List<ICommandPreProcessor<CreateUserCommand>>>().Should().HaveCount(1);
serviceProvider.GetRequiredService<List<ICommandPostProcessor<CreateUserCommand, User>>>().Should()
serviceProvider.GetRequiredService<IEnumerable<ICommandValidator<CreateUserCommand>>>().Should().HaveCount(1);
serviceProvider.GetRequiredService<IEnumerable<ICommandAuthorization<CreateUserCommand>>>().Should().HaveCount(1);
serviceProvider.GetRequiredService<IEnumerable<ICommandPreProcessor<CreateUserCommand>>>().Should().HaveCount(1);
serviceProvider.GetRequiredService<IEnumerable<ICommandPostProcessor<CreateUserCommand, User>>>().Should()
.HaveCount(1);
serviceProvider.GetService<ICommandHandler<CreateUserCommand, User>>().Should().NotBeNull();
serviceProvider.GetService<PreProcessingRunner<CreateUserCommand>>().Should().NotBeNull();
Expand All @@ -40,8 +40,8 @@ public void SetupShould_WorkCorrectly()
serviceProvider.GetService<ICommands>().Should().NotBeNull();

// Queries
serviceProvider.GetRequiredService<List<IQueryValidator<GetUserQuery>>>().Should().HaveCount(1);
serviceProvider.GetRequiredService<List<IQueryAuthorization<GetUserQuery>>>().Should().HaveCount(1);
serviceProvider.GetRequiredService<IEnumerable<IQueryValidator<GetUserQuery>>>().Should().HaveCount(1);
serviceProvider.GetRequiredService<IEnumerable<IQueryAuthorization<GetUserQuery>>>().Should().HaveCount(1);
serviceProvider.GetService<IQueries>().Should().NotBeNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Wemogy.CQRS.Commands.Abstractions;

namespace Wemogy.CQRS.UnitTests.TestApplication.Commands.KitchenSinkWithResult;

public class KitchenSinkWithResultCommand : ICommand<int>
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Threading.Tasks;
using Wemogy.CQRS.Commands.Abstractions;

namespace Wemogy.CQRS.UnitTests.TestApplication.Commands.KitchenSinkWithResult;

public class KitchenSinkWithResultCommandHandler : ICommandHandler<KitchenSinkWithResultCommand, int>
{
public int CalledCount { get; private set; }

public Task<int> HandleAsync(KitchenSinkWithResultCommand command)
{
CalledCount++;
return Task.FromResult(42);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Wemogy.CQRS.Commands.Abstractions;

namespace Wemogy.CQRS.UnitTests.TestApplication.Commands.KitchenSinkWithoutResult;

public class KitchenSinkWithoutResultCommand : ICommand
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Threading.Tasks;
using Wemogy.CQRS.Commands.Abstractions;

namespace Wemogy.CQRS.UnitTests.TestApplication.Commands.KitchenSinkWithoutResult;

public class KitchenSinkWithoutResultCommandHandler : ICommandHandler<KitchenSinkWithoutResultCommand>
{
public int CalledCount { get; private set; }

public Task HandleAsync(KitchenSinkWithoutResultCommand command)
{
CalledCount++;
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading.Tasks;
using Wemogy.CQRS.Commands.Abstractions;

namespace Wemogy.CQRS.UnitTests.TestApplication.Commands.KitchenSinkWithoutResult;

public class KitchenSinkWithoutResultCommandPreProcessor1 : ICommandPreProcessor<KitchenSinkWithoutResultCommand>
{
public int CalledCount { get; private set; }
public Task ProcessAsync(KitchenSinkWithoutResultCommand command)
{
CalledCount++;
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading.Tasks;
using Wemogy.CQRS.Commands.Abstractions;

namespace Wemogy.CQRS.UnitTests.TestApplication.Commands.KitchenSinkWithoutResult;

public class KitchenSinkWithoutResultCommandPreProcessor2 : ICommandPreProcessor<KitchenSinkWithoutResultCommand>
{
public int CalledCount { get; private set; }
public Task ProcessAsync(KitchenSinkWithoutResultCommand command)
{
CalledCount++;
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ namespace Wemogy.CQRS.UnitTests.TestApplication.Commands.TrackUserLogin;

public class TrackUserLoginCommandHandler : ICommandHandler<TrackUserLoginCommand>
{
public static int CallCount { get; private set; }

public Task HandleAsync(TrackUserLoginCommand command)
{
CallCount++;
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
<ItemGroup>
<ProjectReference Include="../Wemogy.CQRS/Wemogy.CQRS.csproj" />
<ProjectReference Include="..\Wemogy.CQRS.UnitTests.AssemblyA\Wemogy.CQRS.UnitTests.AssemblyA.csproj" />
<ProjectReference Include="..\Wemogy.CQRS.UnitTests.AssemblyB\Wemogy.CQRS.UnitTests.AssemblyB.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;
using Wemogy.CQRS.Common.ValueObjects;

namespace Wemogy.CQRS.Abstractions;

public interface ICommandQueryDependencyResolver
{
List<CommandQueryDependency> ResolveDependencies();
}
Loading

0 comments on commit 8aa8073

Please sign in to comment.