using System; using System.Collections.Generic; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ { public class MultiEventBusRabbitMQ : IMultiEventBus { private List _eventBuses; private Dictionary _tenants; public MultiEventBusRabbitMQ(List eventBuses, Dictionary tenants) { _eventBuses = eventBuses; _tenants = tenants; } public void AddEventBus(IEventBus eventBus) { _eventBuses.Add(eventBus); } public void Publish(IntegrationEvent @event) { if (@event.TenantId == 0)//System wide event? { _eventBuses.ForEach(eventBus => { eventBus.Publish(@event); }); } else { //TODO requires ALL events to have tenantId set! _tenants.TryGetValue(@event.TenantId, out String tenantName); var actualEventBus = _eventBuses.Find(e => e.GetVHost().Equals(tenantName)); actualEventBus.Publish(@event); } } public void Subscribe() where T : IntegrationEvent where TH : IIntegrationEventHandler { _eventBuses.ForEach(e => { e.Subscribe(); }); } } }