| | 1 | | using System.Reflection; |
| | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | 3 | | using Microsoft.Extensions.Logging; |
| | 4 | |
|
| | 5 | | namespace NLightning.Application; |
| | 6 | |
|
| | 7 | | using Channels.Handlers; |
| | 8 | | using Channels.Handlers.Interfaces; |
| | 9 | | using Channels.Managers; |
| | 10 | | using Domain.Bitcoin.Interfaces; |
| | 11 | | using Domain.Channels.Interfaces; |
| | 12 | | using Domain.Node.Interfaces; |
| | 13 | | using Domain.Protocol.Interfaces; |
| | 14 | | using Infrastructure.Bitcoin.Wallet.Interfaces; |
| | 15 | | using Node.Managers; |
| | 16 | | using Protocol.Factories; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Extension methods for setting up application services in an IServiceCollection. |
| | 20 | | /// </summary> |
| | 21 | | public 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) |
| 0 | 31 | | services.AddSingleton<IChannelManager>(sp => |
| 0 | 32 | | { |
| 0 | 33 | | var blockchainMonitor = sp.GetRequiredService<IBlockchainMonitor>(); |
| 0 | 34 | | var channelMemoryRepository = sp.GetRequiredService<IChannelMemoryRepository>(); |
| 0 | 35 | | var lightningSigner = sp.GetRequiredService<ILightningSigner>(); |
| 0 | 36 | | var loggerFactory = sp.GetRequiredService<ILoggerFactory>(); |
| 0 | 37 | | return new ChannelManager(blockchainMonitor, channelMemoryRepository, |
| 0 | 38 | | loggerFactory.CreateLogger<ChannelManager>(), lightningSigner, sp); |
| 0 | 39 | | }); |
| 0 | 40 | | services.AddSingleton<IMessageFactory, MessageFactory>(); |
| 0 | 41 | | services.AddSingleton<IPeerManager, PeerManager>(); |
| | 42 | |
|
| | 43 | | // Automatically register all channel message handlers |
| 0 | 44 | | services.AddChannelMessageHandlers(); |
| | 45 | |
|
| | 46 | | // Add scoped services |
| 0 | 47 | | services.AddScoped<FundingConfirmedHandler>(); |
| | 48 | |
|
| 0 | 49 | | return services; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Registers all classes that implement IChannelMessageHandler<T> from the current assembly |
| | 54 | | /// </summary> |
| | 55 | | private static void AddChannelMessageHandlers(this IServiceCollection services) |
| | 56 | | { |
| 0 | 57 | | var assembly = Assembly.GetExecutingAssembly(); |
| | 58 | |
|
| | 59 | | // Find all types that implement IChannelMessageHandler<> |
| 0 | 60 | | var handlerTypes = assembly |
| 0 | 61 | | .GetTypes() |
| 0 | 62 | | .Where(type => type is { IsClass: true, IsAbstract: false }) |
| 0 | 63 | | .Where(type => type.GetInterfaces() |
| 0 | 64 | | .Any(i => i.IsGenericType |
| 0 | 65 | | && i.GetGenericTypeDefinition() == |
| 0 | 66 | | typeof(IChannelMessageHandler<>))) |
| 0 | 67 | | .ToArray(); |
| | 68 | |
|
| 0 | 69 | | foreach (var handlerType in handlerTypes) |
| | 70 | | { |
| | 71 | | // Get the interface this handler implements |
| 0 | 72 | | var handlerInterface = handlerType |
| 0 | 73 | | .GetInterfaces() |
| 0 | 74 | | .First(i => i.IsGenericType |
| 0 | 75 | | && i.GetGenericTypeDefinition() == typeof(IChannelMessageHandler<>)); |
| | 76 | |
|
| 0 | 77 | | services.AddScoped(handlerInterface, handlerType); |
| | 78 | | } |
| 0 | 79 | | } |
| | 80 | | } |