Skip to content

Commit

Permalink
Refactoring the message bus implementation. (#10)
Browse files Browse the repository at this point in the history
* Removed the route key in both publish and subscribe function invocations.

* Involving the MessageHandlerManager implementation. Also introduced the MemoryBasedMessageHandlerManager and MessageHandlerProvider that will provide the message handlers as services via an IoC container.

* Implementing MessageHandlerProvider, under unit testing.

* Fixed the issue in ResolveHandler method.

* Fixed some bugs in the MessageHandlerProvider implementation.

* Refactoring the interface definition and the implementation of the message consumers. Updated related integration tests.

* Updated the xunit dependency version.

* Merges the refactored message bus implementation. (#9)

* Added the webtest project to test the CQRS architecture.

* Fixed the issue that the event handler will be invoked twice. This was because that the CommandConsumer was resolved twice, and the .NET event subscription in the ctor was executed twice. Involved the domainRepository and is going to track the object lifetime.

* Fixed the message handler disposing issue by involving the child IoC container. Came out the "Message Handler Execution Context" design pattern.

* Code adjustment on the MessageBus implementation.

* Refactoring the Subscribe method definition in the IEventSubscriber interface.

* Fixed the failed unit tests.

* Fixed the unit test issues.

* Used the signed assemblies for MongoDB C# driver. Removed the sync Handle method from the IMessageHandler definition.

* Removed the web api testing project.
  • Loading branch information
daxnet committed Dec 12, 2017
1 parent e4b3878 commit 7f9eec2
Show file tree
Hide file tree
Showing 55 changed files with 961 additions and 1,175 deletions.
2 changes: 1 addition & 1 deletion Apworks.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2008
VisualStudioVersion = 15.0.27130.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Apworks", "src\Apworks\Apworks.csproj", "{9D0B1FC5-57E6-4A60-8AFD-CDCAEC271A3A}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<PackageReference Include="Hal" Version="1.0.13" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>

Expand Down

This file was deleted.

This file was deleted.

394 changes: 0 additions & 394 deletions src/Apworks.Integration.AspNetCore/IntegrationExtensions.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Apworks.Messaging;
using Apworks.Utilities;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Apworks.Integration.AspNetCore.Messaging
{
public sealed class ServiceProviderMessageHandlerExecutionContext : MemoryBasedMessageHandlerExecutionContext
{
private readonly IServiceCollection registry;
private readonly Func<IServiceCollection, IServiceProvider> serviceProviderFactory;

public ServiceProviderMessageHandlerExecutionContext(IServiceCollection registry,
Func<IServiceCollection, IServiceProvider> serviceProviderFactory = null)
{
this.registry = registry;
if (serviceProviderFactory == null)
{
this.serviceProviderFactory = sc => registry.BuildServiceProvider();
}
else
{
this.serviceProviderFactory = serviceProviderFactory;
}
}

public override void RegisterHandler(Type messageType, Type handlerType)
{
base.RegisterHandler(messageType, handlerType);

this.registry.AddTransient(handlerType);
}

public override async Task HandleMessageAsync(IMessage message,
CancellationToken cancellationToken = default(CancellationToken))
{
var messageType = message.GetType();

// Checks if the handlers of the current message have already been registered.
if (registrations.TryGetValue(messageType, out List<Type> handlerTypes) &&
handlerTypes?.Count > 0)
{
var serviceProvider = this.serviceProviderFactory(this.registry);

// Creates the child scope for the message handlers so that the dependencies can be disposed
// after the message has been handled.
using (var childScope = serviceProvider.CreateScope())
{
foreach(var handlerType in handlerTypes)
{
var handler = (IMessageHandler)childScope.ServiceProvider.GetService(handlerType);
if (handler.CanHandle(messageType))
{
await handler.HandleAsync(message, cancellationToken);
}
}
}
}
}
}
}
31 changes: 0 additions & 31 deletions src/Apworks.Messaging.RabbitMQ/CommandBus.cs

This file was deleted.

31 changes: 0 additions & 31 deletions src/Apworks.Messaging.RabbitMQ/EventBus.cs

This file was deleted.

179 changes: 0 additions & 179 deletions src/Apworks.Messaging.RabbitMQ/MessageBus.cs

This file was deleted.

0 comments on commit 7f9eec2

Please sign in to comment.