From 2e674ad5321fef5bea8ab2a5bef6f31b39d5e44e Mon Sep 17 00:00:00 2001 From: Eduard Tomas Date: Wed, 22 Mar 2017 14:23:25 +0100 Subject: [PATCH] Replaced Tuple by C#7 multiple return statement Replaced private set only used in ctor by readonly prop --- .../EventBus/Events/IntegrationEvent.cs | 2 +- .../EventBusRabbitMQ/EventBusRabbitMQ.cs | 72 ++++++++++--------- .../EventBusRabbitMQ/EventBusRabbitMQ.csproj | 1 + 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs b/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs index 11185764e..e1802704e 100644 --- a/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs +++ b/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs @@ -11,6 +11,6 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events Id = Guid.NewGuid(); } - public Guid Id { get; private set; } + public Guid Id { get; } } } diff --git a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs index e31c43396..b850c4eef 100644 --- a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs +++ b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs @@ -21,7 +21,8 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ private readonly Dictionary> _handlers; private readonly List _eventTypes; - private Tuple _connection; + private IModel _model; + private IConnection _connection; private string _queueName; @@ -86,15 +87,15 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ _handlers.Remove(eventName); var eventType = _eventTypes.Single(e => e.Name == eventName); _eventTypes.Remove(eventType); - _connection.Item1.QueueUnbind(queue: _queueName, + _model.QueueUnbind(queue: _queueName, exchange: _brokerName, routingKey: eventName); if (_handlers.Keys.Count == 0) { _queueName = string.Empty; - _connection.Item1.Dispose(); - _connection.Item2.Dispose(); + _model.Dispose(); + _connection.Dispose(); } } @@ -103,48 +104,51 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ public void Dispose() { - if (_connection != null) - { - _handlers.Clear(); - _connection.Item1.Dispose(); - _connection.Item2.Dispose(); - } + _handlers.Clear(); + _model?.Dispose(); + _connection?.Dispose(); } private IModel GetChannel() { - if (_connection != null) + if (_model != null) { - return _connection.Item1; + return _model; } else { - var factory = new ConnectionFactory() { HostName = _connectionString }; - var connection = factory.CreateConnection(); - var channel = connection.CreateModel(); + ((_model, _connection) = CreateConnection(); + return _model; + } + } - channel.ExchangeDeclare(exchange: _brokerName, - type: "direct"); - if (string.IsNullOrEmpty(_queueName)) - { - _queueName = channel.QueueDeclare().QueueName; - } - var consumer = new EventingBasicConsumer(channel); - consumer.Received += async (model, ea) => - { - var eventName = ea.RoutingKey; - var message = Encoding.UTF8.GetString(ea.Body); - - await ProcessEvent(eventName, message); - }; - channel.BasicConsume(queue: _queueName, - noAck: true, - consumer: consumer); - _connection = new Tuple(channel, connection); + private (IModel model, IConnection connection) CreateConnection() + { + var factory = new ConnectionFactory() { HostName = _connectionString }; + var con = factory.CreateConnection(); + var channel = con.CreateModel(); - return _connection.Item1; + channel.ExchangeDeclare(exchange: _brokerName, + type: "direct"); + if (string.IsNullOrEmpty(_queueName)) + { + _queueName = channel.QueueDeclare().QueueName; } + + var consumer = new EventingBasicConsumer(channel); + consumer.Received += async (model, ea) => + { + var eventName = ea.RoutingKey; + var message = Encoding.UTF8.GetString(ea.Body); + + await ProcessEvent(eventName, message); + }; + channel.BasicConsume(queue: _queueName, + noAck: true, + consumer: consumer); + + return (channel, con); } private async Task ProcessEvent(string eventName, string message) diff --git a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.csproj b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.csproj index 6e399a627..cf36a2222 100644 --- a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.csproj +++ b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.csproj @@ -9,6 +9,7 @@ +