Refactor: add StoredProcedureBuilder for SQL calls

Introduce StoredProcedureBuilder to centralize and simplify the construction of SQL stored procedure calls and parameter lists. Refactor DeleteObjectProcedureHandler, InsertObjectProcedureHandler, and UpdateObjectProcedureHandler to use this utility, replacing manual StringBuilder and parameter management. Improves code readability, reduces duplication, and standardizes parameter handling, including output parameters.
This commit is contained in:
2026-03-27 15:11:16 +01:00
parent 70dc52139d
commit b66a49f74d
4 changed files with 174 additions and 178 deletions

View File

@@ -6,6 +6,7 @@ using Microsoft.Extensions.Options;
using ReC.Application.Common.Exceptions;
using ReC.Application.Common.Options;
using ReC.Application.Common.Procedures.UpdateProcedure.Dto;
using System.Data;
using System.Text;
namespace ReC.Application.Common.Procedures.UpdateProcedure;
@@ -37,88 +38,68 @@ public class UpdateObjectProcedureHandler(IRepository repo, IOptionsMonitor<SqlE
{
public async Task<int> Handle(UpdateObjectProcedure request, CancellationToken cancel)
{
var execSql = new StringBuilder("EXEC @RC = [dbo].[PRREC_UPDATE_OBJECT]");
var parameters = new List<SqlParameter>();
var separator = ' ';
void Add(string name, object? value, System.Data.SqlDbType? dbType = null)
{
if (value is null) return;
execSql.AppendLine($"{separator}@{name} = @{name}");
separator = ',';
if (dbType.HasValue)
parameters.Add(new SqlParameter($"@{name}", dbType.Value) { Value = value });
else
parameters.Add(new SqlParameter($"@{name}", value));
}
Add("pENTITY", request.Entity);
Add("pGUID", request.Id);
Add("pCHANGED_WHO", request.ChangedWho);
Add("pCHANGED_WHEN", DateTime.UtcNow);
Add("pACTION_PROFILE_ID", request.Action.ProfileId);
Add("pACTION_ACTIVE", request.Action.Active);
Add("pACTION_SEQUENCE", request.Action.Sequence, System.Data.SqlDbType.TinyInt);
Add("pACTION_ENDPOINT_ID", request.Action.EndpointId);
Add("pACTION_ENDPOINT_AUTH_ID", request.Action.EndpointAuthId);
Add("pACTION_ENDPOINT_PARAMS_ID", request.Action.EndpointParamsId, System.Data.SqlDbType.SmallInt);
Add("pACTION_SQL_CONNECTION_ID", request.Action.SqlConnectionId, System.Data.SqlDbType.SmallInt);
Add("pACTION_TYPE_ID", request.Action.TypeId, System.Data.SqlDbType.TinyInt);
Add("pACTION_PRE_SQL", request.Action.PreSql);
Add("pACTION_HEADER_SQL", request.Action.HeaderSql);
Add("pACTION_BODY_SQL", request.Action.BodySql);
Add("pACTION_POST_SQL", request.Action.PostSql);
Add("pACTION_ERROR_ACTION_ID", request.Action.ErrorActionId, System.Data.SqlDbType.TinyInt);
Add("pENDPOINT_ACTIVE", request.Endpoint.Active);
Add("pENDPOINT_DESCRIPTION", request.Endpoint.Description);
Add("pENDPOINT_URI", request.Endpoint.Uri);
Add("pENDPOINT_AUTH_ACTIVE", request.EndpointAuth.Active);
Add("pENDPOINT_AUTH_DESCRIPTION", request.EndpointAuth.Description);
Add("pENDPOINT_AUTH_TYPE_ID", request.EndpointAuth.TypeId, System.Data.SqlDbType.TinyInt);
Add("pENDPOINT_AUTH_API_KEY", request.EndpointAuth.ApiKey);
Add("pENDPOINT_AUTH_API_VALUE", request.EndpointAuth.ApiValue);
Add("pENDPOINT_AUTH_API_KEY_ADD_TO_ID", request.EndpointAuth.ApiKeyAddToId);
Add("pENDPOINT_AUTH_TOKEN", request.EndpointAuth.Token);
Add("pENDPOINT_AUTH_USERNAME", request.EndpointAuth.Username);
Add("pENDPOINT_AUTH_PASSWORD", request.EndpointAuth.Password);
Add("pENDPOINT_AUTH_DOMAIN", request.EndpointAuth.Domain);
Add("pENDPOINT_AUTH_WORKSTATION", request.EndpointAuth.Workstation);
Add("pENDPOINT_PARAMS_ACTIVE", request.EndpointParams.Active);
Add("pENDPOINT_PARAMS_DESCRIPTION", request.EndpointParams.Description);
Add("pENDPOINT_PARAMS_GROUP_ID", request.EndpointParams.GroupId, System.Data.SqlDbType.SmallInt);
Add("pENDPOINT_PARAMS_SEQUENCE", request.EndpointParams.Sequence, System.Data.SqlDbType.TinyInt);
Add("pENDPOINT_PARAMS_KEY", request.EndpointParams.Key);
Add("pENDPOINT_PARAMS_VALUE", request.EndpointParams.Value);
Add("pPROFILE_ACTIVE", request.Profile.Active);
Add("pPROFILE_TYPE_ID", request.Profile.TypeId, System.Data.SqlDbType.TinyInt);
Add("pPROFILE_MANDANTOR", request.Profile.Mandantor);
Add("pPROFILE_NAME", request.Profile.Name);
Add("pPROFILE_DESCRIPTION", request.Profile.Description);
Add("pPROFILE_LOG_LEVEL_ID", request.Profile.LogLevelId, System.Data.SqlDbType.TinyInt);
Add("pPROFILE_LANGUAGE_ID", request.Profile.LanguageId, System.Data.SqlDbType.SmallInt);
Add("pPROFILE_FIRST_RUN", request.Profile.FirstRun);
Add("pPROFILE_LAST_RUN", request.Profile.LastRun);
Add("pPROFILE_LAST_RESULT", request.Profile.LastResult);
Add("pRESULT_ACTION_ID", request.Result.ActionId);
Add("pRESULT_STATUS_ID", request.Result.StatusId, System.Data.SqlDbType.SmallInt);
Add("pRESULT_HEADER", request.Result.Header);
Add("pRESULT_BODY", request.Result.Body);
var sp = new StoredProcedureBuilder("EXEC @RC = [dbo].[PRREC_UPDATE_OBJECT]")
.Add("pENTITY", request.Entity)
.Add("pGUID", request.Id)
.Add("pCHANGED_WHO", request.ChangedWho)
.Add("pCHANGED_WHEN", DateTime.UtcNow)
.Add("pACTION_PROFILE_ID", request.Action.ProfileId)
.Add("pACTION_ACTIVE", request.Action.Active)
.Add("pACTION_SEQUENCE", request.Action.Sequence, SqlDbType.TinyInt)
.Add("pACTION_ENDPOINT_ID", request.Action.EndpointId)
.Add("pACTION_ENDPOINT_AUTH_ID", request.Action.EndpointAuthId)
.Add("pACTION_ENDPOINT_PARAMS_ID", request.Action.EndpointParamsId, SqlDbType.SmallInt)
.Add("pACTION_SQL_CONNECTION_ID", request.Action.SqlConnectionId, SqlDbType.SmallInt)
.Add("pACTION_TYPE_ID", request.Action.TypeId, SqlDbType.TinyInt)
.Add("pACTION_PRE_SQL", request.Action.PreSql)
.Add("pACTION_HEADER_SQL", request.Action.HeaderSql)
.Add("pACTION_BODY_SQL", request.Action.BodySql)
.Add("pACTION_POST_SQL", request.Action.PostSql)
.Add("pACTION_ERROR_ACTION_ID", request.Action.ErrorActionId, SqlDbType.TinyInt)
.Add("pENDPOINT_ACTIVE", request.Endpoint.Active)
.Add("pENDPOINT_DESCRIPTION", request.Endpoint.Description)
.Add("pENDPOINT_URI", request.Endpoint.Uri)
.Add("pENDPOINT_AUTH_ACTIVE", request.EndpointAuth.Active)
.Add("pENDPOINT_AUTH_DESCRIPTION", request.EndpointAuth.Description)
.Add("pENDPOINT_AUTH_TYPE_ID", request.EndpointAuth.TypeId, SqlDbType.TinyInt)
.Add("pENDPOINT_AUTH_API_KEY", request.EndpointAuth.ApiKey)
.Add("pENDPOINT_AUTH_API_VALUE", request.EndpointAuth.ApiValue)
.Add("pENDPOINT_AUTH_API_KEY_ADD_TO_ID", request.EndpointAuth.ApiKeyAddToId)
.Add("pENDPOINT_AUTH_TOKEN", request.EndpointAuth.Token)
.Add("pENDPOINT_AUTH_USERNAME", request.EndpointAuth.Username)
.Add("pENDPOINT_AUTH_PASSWORD", request.EndpointAuth.Password)
.Add("pENDPOINT_AUTH_DOMAIN", request.EndpointAuth.Domain)
.Add("pENDPOINT_AUTH_WORKSTATION", request.EndpointAuth.Workstation)
.Add("pENDPOINT_PARAMS_ACTIVE", request.EndpointParams.Active)
.Add("pENDPOINT_PARAMS_DESCRIPTION", request.EndpointParams.Description)
.Add("pENDPOINT_PARAMS_GROUP_ID", request.EndpointParams.GroupId, SqlDbType.SmallInt)
.Add("pENDPOINT_PARAMS_SEQUENCE", request.EndpointParams.Sequence, SqlDbType.TinyInt)
.Add("pENDPOINT_PARAMS_KEY", request.EndpointParams.Key)
.Add("pENDPOINT_PARAMS_VALUE", request.EndpointParams.Value)
.Add("pPROFILE_ACTIVE", request.Profile.Active)
.Add("pPROFILE_TYPE_ID", request.Profile.TypeId, SqlDbType.TinyInt)
.Add("pPROFILE_MANDANTOR", request.Profile.Mandantor)
.Add("pPROFILE_NAME", request.Profile.Name)
.Add("pPROFILE_DESCRIPTION", request.Profile.Description)
.Add("pPROFILE_LOG_LEVEL_ID", request.Profile.LogLevelId, SqlDbType.TinyInt)
.Add("pPROFILE_LANGUAGE_ID", request.Profile.LanguageId, SqlDbType.SmallInt)
.Add("pPROFILE_FIRST_RUN", request.Profile.FirstRun)
.Add("pPROFILE_LAST_RUN", request.Profile.LastRun)
.Add("pPROFILE_LAST_RESULT", request.Profile.LastResult)
.Add("pRESULT_ACTION_ID", request.Result.ActionId)
.Add("pRESULT_STATUS_ID", request.Result.StatusId, SqlDbType.SmallInt)
.Add("pRESULT_HEADER", request.Result.Header)
.Add("pRESULT_BODY", request.Result.Body);
var sql = new StringBuilder()
.AppendLine("DECLARE @RC SMALLINT = 0;")
.Append(execSql).AppendLine(";")
.Append(sp.BuildSql()).AppendLine(";")
.AppendLine("SELECT @RC;")
.ToString();
try
{
var result = await repo.ExecuteQueryRawAsync(sql, parameters.ToArray(), cancel);
var result = await repo.ExecuteQueryRawAsync(sql, sp.BuildParameters(), cancel);
if (result > 0)
{