< Summary - Combined Code Coverage

Information
Class: NLightning.Application.DependencyInjection
Assembly: NLightning.Application
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Application/DependencyInjection.cs
Tag: 36_15743069263
Line coverage
0%
Covered lines: 0
Uncovered lines: 30
Coverable lines: 30
Total lines: 80
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddApplicationServices(...)100%210%
AddChannelMessageHandlers(...)0%110100%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Application/DependencyInjection.cs

#LineLine coverage
 1using System.Reflection;
 2using Microsoft.Extensions.DependencyInjection;
 3using Microsoft.Extensions.Logging;
 4
 5namespace NLightning.Application;
 6
 7using Channels.Handlers;
 8using Channels.Handlers.Interfaces;
 9using Channels.Managers;
 10using Domain.Bitcoin.Interfaces;
 11using Domain.Channels.Interfaces;
 12using Domain.Node.Interfaces;
 13using Domain.Protocol.Interfaces;
 14using Infrastructure.Bitcoin.Wallet.Interfaces;
 15using Node.Managers;
 16using Protocol.Factories;
 17
 18/// <summary>
 19/// Extension methods for setting up application services in an IServiceCollection.
 20/// </summary>
 21public static class DependencyInjection
 22{
 23    /// <summary>
 24    /// Adds application layer services to the specified IServiceCollection.
 25    /// </summary>
 26    /// <param name="services">The IServiceCollection to add services to.</param>
 27    /// <returns>The same service collection so that multiple calls can be chained.</returns>
 28    public static IServiceCollection AddApplicationServices(this IServiceCollection services)
 29    {
 30        // Singleton services (one instance throughout the application)
 031        services.AddSingleton<IChannelManager>(sp =>
 032        {
 033            var blockchainMonitor = sp.GetRequiredService<IBlockchainMonitor>();
 034            var channelMemoryRepository = sp.GetRequiredService<IChannelMemoryRepository>();
 035            var lightningSigner = sp.GetRequiredService<ILightningSigner>();
 036            var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
 037            return new ChannelManager(blockchainMonitor, channelMemoryRepository,
 038                                      loggerFactory.CreateLogger<ChannelManager>(), lightningSigner, sp);
 039        });
 040        services.AddSingleton<IMessageFactory, MessageFactory>();
 041        services.AddSingleton<IPeerManager, PeerManager>();
 42
 43        // Automatically register all channel message handlers
 044        services.AddChannelMessageHandlers();
 45
 46        // Add scoped services
 047        services.AddScoped<FundingConfirmedHandler>();
 48
 049        return services;
 50    }
 51
 52    /// <summary>
 53    /// Registers all classes that implement IChannelMessageHandler&lt;T&gt; from the current assembly
 54    /// </summary>
 55    private static void AddChannelMessageHandlers(this IServiceCollection services)
 56    {
 057        var assembly = Assembly.GetExecutingAssembly();
 58
 59        // Find all types that implement IChannelMessageHandler<>
 060        var handlerTypes = assembly
 061                          .GetTypes()
 062                          .Where(type => type is { IsClass: true, IsAbstract: false })
 063                          .Where(type => type.GetInterfaces()
 064                                             .Any(i => i.IsGenericType
 065                                                    && i.GetGenericTypeDefinition() ==
 066                                                       typeof(IChannelMessageHandler<>)))
 067                          .ToArray();
 68
 069        foreach (var handlerType in handlerTypes)
 70        {
 71            // Get the interface this handler implements
 072            var handlerInterface = handlerType
 073                                  .GetInterfaces()
 074                                  .First(i => i.IsGenericType
 075                                           && i.GetGenericTypeDefinition() == typeof(IChannelMessageHandler<>));
 76
 077            services.AddScoped(handlerInterface, handlerType);
 78        }
 079    }
 80}