From 6d01d7ada00a5e5e82554596b4ce5098e73e8a5d Mon Sep 17 00:00:00 2001 From: SzymonPobiega Date: Wed, 13 Nov 2019 12:16:48 +0100 Subject: [PATCH] Remove duplication from QueueCreator --- .../Receiving/QueueCreator.cs | 45 +++---------------- 1 file changed, 6 insertions(+), 39 deletions(-) diff --git a/src/NServiceBus.SqlServer/Receiving/QueueCreator.cs b/src/NServiceBus.SqlServer/Receiving/QueueCreator.cs index fb4bafa31..372fa62df 100644 --- a/src/NServiceBus.SqlServer/Receiving/QueueCreator.cs +++ b/src/NServiceBus.SqlServer/Receiving/QueueCreator.cs @@ -24,27 +24,22 @@ public async Task CreateQueueIfNecessary(QueueBindings queueBindings, string ide { foreach (var receivingAddress in queueBindings.ReceivingAddresses) { - await CreateQueue(addressTranslator.Parse(receivingAddress), connection, transaction, createMessageBodyColumn).ConfigureAwait(false); + await CreateQueue(SqlConstants.CreateQueueText, addressTranslator.Parse(receivingAddress), connection, transaction, createMessageBodyColumn).ConfigureAwait(false); } foreach (var sendingAddress in queueBindings.SendingAddresses) { - await CreateQueue(addressTranslator.Parse(sendingAddress), connection, transaction, createMessageBodyColumn).ConfigureAwait(false); + await CreateQueue(SqlConstants.CreateQueueText, addressTranslator.Parse(sendingAddress), connection, transaction, createMessageBodyColumn).ConfigureAwait(false); } - transaction.Commit(); - } - - using (var connection = await connectionFactory.OpenNewConnection().ConfigureAwait(false)) - using (var transaction = connection.BeginTransaction()) - { - await CreateDelayedMessageQueue(connection, transaction, createMessageBodyColumn).ConfigureAwait(false); + + await CreateQueue(SqlConstants.CreateDelayedMessageStoreText, delayedQueueAddress, connection, transaction, createMessageBodyColumn).ConfigureAwait(false); transaction.Commit(); } } - static async Task CreateQueue(CanonicalQueueAddress canonicalQueueAddress, SqlConnection connection, SqlTransaction transaction, bool createMessageBodyColumn) + static async Task CreateQueue(string creationScript, CanonicalQueueAddress canonicalQueueAddress, SqlConnection connection, SqlTransaction transaction, bool createMessageBodyColumn) { - var sql = string.Format(SqlConstants.CreateQueueText, canonicalQueueAddress.QualifiedTableName, canonicalQueueAddress.QuotedCatalogName); + var sql = string.Format(creationScript, canonicalQueueAddress.QualifiedTableName, canonicalQueueAddress.QuotedCatalogName); using (var command = new SqlCommand(sql, connection, transaction) { CommandType = CommandType.Text @@ -66,34 +61,6 @@ static async Task CreateQueue(CanonicalQueueAddress canonicalQueueAddress, SqlCo } } - async Task CreateDelayedMessageQueue(SqlConnection connection, SqlTransaction transaction, bool createMessageBodyComputedColumn) - { -#pragma warning disable 618 - var sql = string.Format(SqlConstants.CreateDelayedMessageStoreText, delayedQueueAddress.QualifiedTableName, delayedQueueAddress.QuotedCatalogName); -#pragma warning restore 618 - using (var command = new SqlCommand(sql, connection, transaction) - { - CommandType = CommandType.Text - }) - { - await command.ExecuteNonQueryAsync().ConfigureAwait(false); - } - if (createMessageBodyComputedColumn) - { -#pragma warning disable 618 - var bodyStringSql = string.Format(SqlConstants.AddMessageBodyStringColumn, delayedQueueAddress.QualifiedTableName, delayedQueueAddress.QuotedCatalogName); -#pragma warning restore 618 - using (var command = new SqlCommand(bodyStringSql, connection, transaction) - { - CommandType = CommandType.Text - }) - { - await command.ExecuteNonQueryAsync().ConfigureAwait(false); - } - - } - } - SqlConnectionFactory connectionFactory; QueueAddressTranslator addressTranslator; CanonicalQueueAddress delayedQueueAddress;