Skip to content

Commit 9b5d01e

Browse files
committed
Introduced new GetReturnCode() extension method
Simple internal extension method for centralizing logic for retrieving the return code and making sure it's converted to an integer.
1 parent ac6c2ba commit 9b5d01e

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

OnTopic.Data.Sql/SqlCommandExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
\=============================================================================================================================*/
66
using System;
77
using System.Data;
8+
using System.Globalization;
89
using System.Text;
910
using Microsoft.Data.SqlClient;
1011
using OnTopic.Internal.Diagnostics;
@@ -19,6 +20,22 @@ namespace OnTopic.Data.Sql {
1920
/// </summary>
2021
internal static class SqlCommandExtensions {
2122

23+
/*==========================================================================================================================
24+
| METHOD: GET RETURN CODE
25+
\-------------------------------------------------------------------------------------------------------------------------*/
26+
/// <summary>
27+
/// Retrieves the return code from a stored procedure.
28+
/// </summary>
29+
/// <param name="command">The SQL command object.</param>
30+
internal static int GetReturnCode(this SqlCommand command, string sqlParameter = "ReturnCode") {
31+
Contract.Assume<InvalidOperationException>(
32+
command.Parameters.Contains($"@{sqlParameter}"),
33+
$"The call to the {command.CommandText} stored procedure did not return the expected 'ReturnCode' parameter."
34+
);
35+
var returnCode = command.Parameters[$"@{sqlParameter}"].Value.ToString();
36+
return Int32.Parse(returnCode, CultureInfo.InvariantCulture);
37+
}
38+
2239
/*==========================================================================================================================
2340
| METHOD: ADD OUTPUT PARAMETER
2441
\-------------------------------------------------------------------------------------------------------------------------*/

OnTopic.Data.Sql/SqlTopicRepository.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,7 @@ public override Topic Load(string? topicKey = null, bool isRecursive = true) {
349349
/*----------------------------------------------------------------------------------------------------------------------
350350
| Process return value
351351
\---------------------------------------------------------------------------------------------------------------------*/
352-
Contract.Assume<InvalidOperationException>(
353-
command.Parameters["@ReturnCode"] != null,
354-
"The call to the GetTopicID stored procedure did not return the expected 'ReturnCode' parameter."
355-
);
356-
topicId = Int32.Parse(command.Parameters["@ReturnCode"].Value.ToString(), CultureInfo.InvariantCulture);
352+
topicId = command.GetReturnCode();
357353

358354
}
359355

@@ -777,13 +773,7 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
777773
/*----------------------------------------------------------------------------------------------------------------------
778774
| Process return value
779775
\---------------------------------------------------------------------------------------------------------------------*/
780-
Contract.Assume<InvalidOperationException>(
781-
command.Parameters["@ReturnCode"] != null,
782-
"The call to the CreateTopic stored procedure did not return the expected 'ReturnCode' parameter."
783-
);
784-
returnVal = Int32.Parse(command.Parameters["@ReturnCode"].Value.ToString(), CultureInfo.InvariantCulture);
785-
786-
topic.Id = returnVal;
776+
topic.Id = command.GetReturnCode();
787777

788778
Contract.Assume<InvalidOperationException>(
789779
topic.Id > 0,

0 commit comments

Comments
 (0)