Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
68 lines (56 sloc) 2.68 KB
using DShop.Common.Handlers;
using DShop.Common.Messages;
using Microsoft.AspNetCore.Builder;
using RawRabbit;
using System;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
namespace DShop.Common.RabbitMq
{
public class BusSubscriber : IBusSubscriber
{
private readonly IBusClient _busClient;
private readonly IServiceProvider _serviceProvider;
private readonly string _defaultNamespace;
public BusSubscriber(IApplicationBuilder app)
{
_serviceProvider = app.ApplicationServices.GetService<IServiceProvider>();
_busClient = _serviceProvider.GetService<IBusClient>();
_defaultNamespace = _serviceProvider.GetService<RabbitMqOptions>().Namespace;
}
public IBusSubscriber SubscribeCommand<TCommand>(string @namespace = null, string queueName = null)
where TCommand : ICommand
{
_busClient.SubscribeAsync<TCommand, CorrelationContext>((command, ctx) =>
{
var commandHandler = _serviceProvider.GetService<ICommandHandler<TCommand>>();
return commandHandler.HandleAsync(command, ctx);
},
ctx => ctx.UseSubscribeConfiguration(cfg =>
cfg.FromDeclaredQueue(q => q.WithName(GetQueueName<TCommand>(@namespace, queueName)))));
return this;
}
public IBusSubscriber SubscribeEvent<TEvent>(string @namespace = null, string queueName = null)
where TEvent : IEvent
{
_busClient.SubscribeAsync<TEvent, CorrelationContext>((@event, ctx) =>
{
var eventHandler = _serviceProvider.GetService<IEventHandler<TEvent>>();
return eventHandler.HandleAsync(@event, ctx);
},
ctx => ctx.UseSubscribeConfiguration(cfg =>
cfg.FromDeclaredQueue(q => q.WithName(GetQueueName<TEvent>(@namespace, queueName)))));
return this;
}
private string GetQueueName<T>(string @namespace = null, string name = null)
{
@namespace = string.IsNullOrWhiteSpace(@namespace)
? (string.IsNullOrWhiteSpace(_defaultNamespace) ? string.Empty : _defaultNamespace)
: @namespace;
var separatedNamespace = string.IsNullOrWhiteSpace(@namespace) ? string.Empty : $"{@namespace}.";
return (string.IsNullOrWhiteSpace(name)
? $"{Assembly.GetEntryAssembly().GetName().Name}/{separatedNamespace}{typeof(T).Name.Underscore()}"
: $"{name}/{separatedNamespace}{typeof(T).Name.Underscore()}").ToLowerInvariant();
}
}
}