-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring the message bus implementation. (#10)
* 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
Showing
55 changed files
with
961 additions
and
1,175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
src/Apworks.Integration.AspNetCore/Configuration/CommandConsumerConfigurator.cs
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
src/Apworks.Integration.AspNetCore/Configuration/EventConsumerConfigurator.cs
This file was deleted.
Oops, something went wrong.
394 changes: 0 additions & 394 deletions
394
src/Apworks.Integration.AspNetCore/IntegrationExtensions.cs
Large diffs are not rendered by default.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
...Apworks.Integration.AspNetCore/Messaging/ServiceProviderMessageHandlerExecutionContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.