< Summary - Syki

Information
Class: Syki.Back.Configs.CommandConfigs
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Configs/CommandConfigs.cs
Tag: 56_26538939494
Line coverage
92%
Covered lines: 23
Uncovered lines: 2
Coverable lines: 25
Total lines: 50
Line coverage: 92%
Branch coverage
78%
Covered branches: 11
Total branches: 14
Branch coverage: 78.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
AddCommandConfigs(...)83.33%121290.9%
GetCommandType(...)50%22100%

File(s)

/home/runner/work/syki/syki/Back/Configs/CommandConfigs.cs

#LineLine coverage
 1namespace Syki.Back.Configs;
 2
 3public static class CommandConfigs
 4{
 25    private static readonly Dictionary<string, Type> _commandTypes = [];
 6
 7    public static void AddCommandConfigs(this WebApplicationBuilder builder)
 8    {
 29        var assemblies = AppDomain.CurrentDomain.GetAssemblies()
 32810            .Where(s => s.FullName!.StartsWith("Back"))
 211            .ToList();
 12
 413        var commandTypes = assemblies.SelectMany(s => s.GetTypes())
 214814            .Where(t => t.IsAssignableTo(typeof(ICommand)) && !t.IsInterface)
 215            .ToList();
 16
 1617        foreach (var type in commandTypes)
 18        {
 619            _commandTypes[type.Name] = type;
 20        }
 21
 422        var handlerTypes = assemblies.SelectMany(s => s.GetTypes())
 329823            .Where(t => !t.IsInterface && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == 
 224            .ToList();
 25
 226        var handledCommandTypes = new HashSet<Type>();
 27
 1628        foreach (var type in handlerTypes)
 29        {
 630            var handlerInterface = type.GetInterfaces()
 1231                .First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandHandler<>));
 32
 633            handledCommandTypes.Add(handlerInterface.GetGenericArguments()[0]);
 634            builder.Services.AddTransient(handlerInterface, type);
 35        }
 36
 237        var commandsWithoutHandler = commandTypes.Except(handledCommandTypes).ToList();
 238        if (commandsWithoutHandler.Count > 0)
 39        {
 040            var names = string.Join(", ", commandsWithoutHandler.Select(t => t.Name));
 041            throw new InvalidOperationException($"Commands without a registered ICommandHandler<T>: {names}");
 42        }
 243    }
 44
 45    public static Type GetCommandType(string typeName)
 46    {
 447        return _commandTypes.TryGetValue(typeName, out var type) ? type
 448            : throw new InvalidOperationException($"Command type '{typeName}' not found in registry.");
 49    }
 50}