dotnet-cqrs/Svrnty.CQRS.Abstractions/Discovery/CommandMeta.cs
2025-11-10 03:40:25 -05:00

51 lines
1.7 KiB
C#

using System;
using System.Reflection;
using Svrnty.CQRS.Abstractions.Attributes;
namespace Svrnty.CQRS.Abstractions.Discovery;
public sealed class CommandMeta : ICommandMeta
{
private readonly string _name;
private readonly string _lowerCamelCaseName;
public CommandMeta(Type commandType, Type serviceType, Type commandResultType)
{
CommandType = commandType;
ServiceType = serviceType;
CommandResultType = commandResultType;
// Cache reflection and computed values once in constructor
var nameAttribute = commandType.GetCustomAttribute<CommandNameAttribute>();
_name = nameAttribute?.Name ?? commandType.Name.Replace("Command", string.Empty);
_lowerCamelCaseName = ComputeLowerCamelCaseName(_name);
}
public CommandMeta(Type commandType, Type serviceType)
{
CommandType = commandType;
ServiceType = serviceType;
// Cache reflection and computed values once in constructor
var nameAttribute = commandType.GetCustomAttribute<CommandNameAttribute>();
_name = nameAttribute?.Name ?? commandType.Name.Replace("Command", string.Empty);
_lowerCamelCaseName = ComputeLowerCamelCaseName(_name);
}
private static string ComputeLowerCamelCaseName(string name)
{
if (string.IsNullOrEmpty(name))
return name;
var firstLetter = char.ToLowerInvariant(name[0]);
return $"{firstLetter}{name.Substring(1)}";
}
public string Name => _name;
public Type CommandType { get; }
public Type ServiceType { get; }
public Type CommandResultType { get; }
public string LowerCamelCaseName => _lowerCamelCaseName;
}