Replaced Tuple by C#7 multiple return statement

Replaced private set only used in ctor by readonly prop
This commit is contained in:
Eduard Tomas 2017-03-22 14:23:25 +01:00
parent 696610ed36
commit 2e674ad532
3 changed files with 43 additions and 38 deletions

View File

@ -11,6 +11,6 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events
Id = Guid.NewGuid(); Id = Guid.NewGuid();
} }
public Guid Id { get; private set; } public Guid Id { get; }
} }
} }

View File

@ -21,7 +21,8 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
private readonly Dictionary<string, List<IIntegrationEventHandler>> _handlers; private readonly Dictionary<string, List<IIntegrationEventHandler>> _handlers;
private readonly List<Type> _eventTypes; private readonly List<Type> _eventTypes;
private Tuple<IModel, IConnection> _connection; private IModel _model;
private IConnection _connection;
private string _queueName; private string _queueName;
@ -86,15 +87,15 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
_handlers.Remove(eventName); _handlers.Remove(eventName);
var eventType = _eventTypes.Single(e => e.Name == eventName); var eventType = _eventTypes.Single(e => e.Name == eventName);
_eventTypes.Remove(eventType); _eventTypes.Remove(eventType);
_connection.Item1.QueueUnbind(queue: _queueName, _model.QueueUnbind(queue: _queueName,
exchange: _brokerName, exchange: _brokerName,
routingKey: eventName); routingKey: eventName);
if (_handlers.Keys.Count == 0) if (_handlers.Keys.Count == 0)
{ {
_queueName = string.Empty; _queueName = string.Empty;
_connection.Item1.Dispose(); _model.Dispose();
_connection.Item2.Dispose(); _connection.Dispose();
} }
} }
@ -103,50 +104,53 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
public void Dispose() public void Dispose()
{ {
if (_connection != null) _handlers.Clear();
{ _model?.Dispose();
_handlers.Clear(); _connection?.Dispose();
_connection.Item1.Dispose();
_connection.Item2.Dispose();
}
} }
private IModel GetChannel() private IModel GetChannel()
{ {
if (_connection != null) if (_model != null)
{ {
return _connection.Item1; return _model;
} }
else else
{ {
var factory = new ConnectionFactory() { HostName = _connectionString }; ((_model, _connection) = CreateConnection();
var connection = factory.CreateConnection(); return _model;
var channel = connection.CreateModel();
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<IModel, IConnection>(channel, connection);
return _connection.Item1;
} }
} }
private (IModel model, IConnection connection) CreateConnection()
{
var factory = new ConnectionFactory() { HostName = _connectionString };
var con = factory.CreateConnection();
var channel = con.CreateModel();
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) private async Task ProcessEvent(string eventName, string message)
{ {
if (_handlers.ContainsKey(eventName)) if (_handlers.ContainsKey(eventName))

View File

@ -9,6 +9,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="RabbitMQ.Client" Version="4.1.1" /> <PackageReference Include="RabbitMQ.Client" Version="4.1.1" />
<PackageReference Include="System.ValueTuple" Version="4.3.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>