Update namespaces to Common.Dto for DTO-related files

Refactored PlaceholderExtensions, DtoMappingProfile, and InvokeActionTests
to use the ReC.Application.Common.Dto namespace instead of
ReC.Application.Common.Behaviors.Action. Updated using directives and
namespaces to improve code organization for DTO-related logic.
This commit is contained in:
2026-03-26 16:52:54 +01:00
parent bd78ada686
commit b3dfdd1e5c
3 changed files with 2 additions and 3 deletions

View File

@@ -1,4 +1,3 @@
using ReC.Application.Common.Behaviors.Action;
using ReC.Domain.Views;
namespace ReC.Application.Common.Dto;

View File

@@ -0,0 +1,63 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
using System.Text.RegularExpressions;
using ReC.Application.Common.Exceptions;
namespace ReC.Application.Common.Dto;
public static partial class PlaceholderExtensions
{
[GeneratedRegex(@"\{#[^#]+#[^}]+\}")]
private static partial Regex PlaceholderRegex();
/// <summary>
/// Replaces placeholders in the format <c>{#ANY_STRING#COLUMN_NAME}</c> with the corresponding
/// property value resolved via <see cref="GetValueByColumnName{T}"/> from the provided objects.
/// Values are converted to SQL-compatible string representations.
/// </summary>
/// <exception cref="PlaceholderResolutionException">
/// Thrown when a placeholder's column name cannot be resolved from any of the provided objects.
/// </exception>
public static string ReplacePlaceholders(this string str, params object?[] objects)
{
return PlaceholderRegex().Replace(str, match =>
{
var placeholder = match.Value;
var inner = placeholder[2..^1]; // remove {# and }
var lastHash = inner.LastIndexOf('#');
var columnName = inner[(lastHash + 1)..];
foreach (var obj in objects)
{
if (obj is null)
continue;
var value = obj.GetValueByColumnName(columnName);
if (value is not null)
return ToSqlLiteral(value);
}
throw new PlaceholderResolutionException(placeholder, columnName, str);
});
}
private static string ToSqlLiteral(object value) => value switch
{
bool b => b ? "TRUE" : "FALSE",
DateTime dt => dt.ToString("yyyy-MM-dd HH:mm:ss"),
DateTimeOffset dto => dto.ToString("yyyy-MM-dd HH:mm:ss zzz"),
_ => value.ToString() ?? string.Empty
};
/// <summary>
/// Gets the value of a property by its column name defined in <see cref="ColumnAttribute"/>.
/// Returns <c>null</c> if no property with the given column name exists.
/// </summary>
public static object? GetValueByColumnName<T>(this T obj, string columnName) where T : class
{
var property = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
.FirstOrDefault(p => p.GetCustomAttribute<ColumnAttribute>()?.Name == columnName);
return property?.GetValue(obj);
}
}